KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > ConsistencyToken


1 /*
2  * Derby - class org.apache.derby.impl.drda.ConsistencyToken
3  *
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements. See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16  * implied. See the License for the specific language governing
17  * permissions and limitations under the License.
18  *
19  */

20
21 package org.apache.derby.impl.drda;
22
23 /**
24  * Class which represents an RDB Package Consistency Token.
25  */

26 final class ConsistencyToken {
27     /** Byte array representation of the token. */
28     private final byte[] bytes;
29     /** Cached hash code. */
30     private int hash = 0;
31
32     /**
33      * Create a new <code>ConsistencyToken</code> instance.
34      *
35      * @param bytes byte array representing the token
36      */

37     ConsistencyToken(byte[] bytes) {
38         this.bytes = bytes;
39     }
40
41     /**
42      * Get the byte array representation of the consistency token.
43      *
44      * @return a <code>byte[]</code> value
45      */

46     public byte[] getBytes() {
47         return bytes;
48     }
49
50     /**
51      * Check whether this object is equal to another object.
52      *
53      * @param o another object
54      * @return true if the objects are equal
55      */

56     public boolean equals(Object JavaDoc o) {
57         if (!(o instanceof ConsistencyToken)) return false;
58         ConsistencyToken ct = (ConsistencyToken) o;
59         int len = bytes.length;
60         if (len != ct.bytes.length) return false;
61         for (int i = 0; i < len; ++i) {
62             if (bytes[i] != ct.bytes[i]) return false;
63         }
64         return true;
65     }
66
67     /**
68      * Calculate the hash code.
69      *
70      * @return hash code
71      */

72     public int hashCode() {
73         // ConsistencyToken objects might be kept for a long time and are
74
// frequently used as keys in hash tables. Therefore, it is a good idea
75
// to cache their hash codes.
76
int h = hash;
77         if (h == 0) {
78             // The hash code has not been calculated yet (or perhaps the hash
79
// code actually is 0). Calculate a new one and cache it. No
80
// synchronization is needed since reads and writes of 32-bit
81
// primitive values are guaranteed to be atomic. See The
82
// "Double-Checked Locking is Broken" Declaration for details.
83
int len = bytes.length;
84             for (int i = 0; i < len; ++i) {
85                 h ^= bytes[i];
86             }
87             hash = h;
88         }
89         return h;
90     }
91
92     /**
93      * Return a string representation of the consistency token by
94      * converting it to a <code>BigInteger</code> value. (For
95      * debugging only.)
96      *
97      * @return a <code>String</code> value
98      */

99     public String JavaDoc toString() {
100         return new java.math.BigInteger JavaDoc(bytes).toString();
101     }
102 }
103
Popular Tags