--- /dev/null
+/*\r
+ * Derby - class org.apache.derby.impl.drda.ConsistencyToken\r
+ *\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\r
+ * implied. See the License for the specific language governing\r
+ * permissions and limitations under the License.\r
+ *\r
+ */\r
+\r
+package org.apache.derby.impl.drda;\r
+\r
+/**\r
+ * Class which represents an RDB Package Consistency Token.\r
+ */\r
+final class ConsistencyToken {\r
+ /** Byte array representation of the token. */\r
+ private final byte[] bytes;\r
+ /** Cached hash code. */\r
+ private int hash = 0;\r
+\r
+ /**\r
+ * Create a new <code>ConsistencyToken</code> instance.\r
+ *\r
+ * @param bytes byte array representing the token\r
+ */\r
+ ConsistencyToken(byte[] bytes) {\r
+ this.bytes = bytes;\r
+ }\r
+\r
+ /**\r
+ * Get the byte array representation of the consistency token.\r
+ *\r
+ * @return a <code>byte[]</code> value\r
+ */\r
+ public byte[] getBytes() {\r
+ return bytes;\r
+ }\r
+\r
+ /**\r
+ * Check whether this object is equal to another object.\r
+ *\r
+ * @param o another object\r
+ * @return true if the objects are equal\r
+ */\r
+ public boolean equals(Object o) {\r
+ if (!(o instanceof ConsistencyToken)) return false;\r
+ ConsistencyToken ct = (ConsistencyToken) o;\r
+ int len = bytes.length;\r
+ if (len != ct.bytes.length) return false;\r
+ for (int i = 0; i < len; ++i) {\r
+ if (bytes[i] != ct.bytes[i]) return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Calculate the hash code.\r
+ *\r
+ * @return hash code\r
+ */\r
+ public int hashCode() {\r
+ // ConsistencyToken objects might be kept for a long time and are\r
+ // frequently used as keys in hash tables. Therefore, it is a good idea\r
+ // to cache their hash codes.\r
+ int h = hash;\r
+ if (h == 0) {\r
+ // The hash code has not been calculated yet (or perhaps the hash\r
+ // code actually is 0). Calculate a new one and cache it. No\r
+ // synchronization is needed since reads and writes of 32-bit\r
+ // primitive values are guaranteed to be atomic. See The\r
+ // "Double-Checked Locking is Broken" Declaration for details.\r
+ int len = bytes.length;\r
+ for (int i = 0; i < len; ++i) {\r
+ h ^= bytes[i];\r
+ }\r
+ hash = h;\r
+ }\r
+ return h;\r
+ }\r
+\r
+ /**\r
+ * Return a string representation of the consistency token by\r
+ * converting it to a <code>BigInteger</code> value. (For\r
+ * debugging only.)\r
+ *\r
+ * @return a <code>String</code> value\r
+ */\r
+ public String toString() {\r
+ return new java.math.BigInteger(bytes).toString();\r
+ }\r
+}\r