001: /*
002: * Derby - class org.apache.derby.impl.drda.ConsistencyToken
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
016: * implied. See the License for the specific language governing
017: * permissions and limitations under the License.
018: *
019: */
020:
021: package org.apache.derby.impl.drda;
022:
023: /**
024: * Class which represents an RDB Package Consistency Token.
025: */
026: final class ConsistencyToken {
027: /** Byte array representation of the token. */
028: private final byte[] bytes;
029: /** Cached hash code. */
030: private int hash = 0;
031:
032: /**
033: * Create a new <code>ConsistencyToken</code> instance.
034: *
035: * @param bytes byte array representing the token
036: */
037: ConsistencyToken(byte[] bytes) {
038: this .bytes = bytes;
039: }
040:
041: /**
042: * Get the byte array representation of the consistency token.
043: *
044: * @return a <code>byte[]</code> value
045: */
046: public byte[] getBytes() {
047: return bytes;
048: }
049:
050: /**
051: * Check whether this object is equal to another object.
052: *
053: * @param o another object
054: * @return true if the objects are equal
055: */
056: public boolean equals(Object o) {
057: if (!(o instanceof ConsistencyToken))
058: return false;
059: ConsistencyToken ct = (ConsistencyToken) o;
060: int len = bytes.length;
061: if (len != ct.bytes.length)
062: return false;
063: for (int i = 0; i < len; ++i) {
064: if (bytes[i] != ct.bytes[i])
065: return false;
066: }
067: return true;
068: }
069:
070: /**
071: * Calculate the hash code.
072: *
073: * @return hash code
074: */
075: public int hashCode() {
076: // ConsistencyToken objects might be kept for a long time and are
077: // frequently used as keys in hash tables. Therefore, it is a good idea
078: // to cache their hash codes.
079: int h = hash;
080: if (h == 0) {
081: // The hash code has not been calculated yet (or perhaps the hash
082: // code actually is 0). Calculate a new one and cache it. No
083: // synchronization is needed since reads and writes of 32-bit
084: // primitive values are guaranteed to be atomic. See The
085: // "Double-Checked Locking is Broken" Declaration for details.
086: int len = bytes.length;
087: for (int i = 0; i < len; ++i) {
088: h ^= bytes[i];
089: }
090: hash = h;
091: }
092: return h;
093: }
094:
095: /**
096: * Return a string representation of the consistency token by
097: * converting it to a <code>BigInteger</code> value. (For
098: * debugging only.)
099: *
100: * @return a <code>String</code> value
101: */
102: public String toString() {
103: return new java.math.BigInteger(bytes).toString();
104: }
105: }
|