--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.store.access.KeyHasher\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 implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.store.access;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+/**\r
+ Provides the ability to hash on multiple objects.\r
+*/\r
+public class KeyHasher \r
+{\r
+ \r
+ private final Object[] objects;\r
+\r
+ public KeyHasher(int size)\r
+ {\r
+ objects = new Object[size];\r
+ }\r
+\r
+ /**\r
+ * Set array element at the specified index to the specified object.\r
+ *\r
+ * @param index The specified index\r
+ * @param object The specified object.\r
+ */\r
+ public void setObject(int index, Object object)\r
+ {\r
+ objects[index] = object;\r
+ }\r
+\r
+ /**\r
+ * Get the object stored at the specified index.\r
+ *\r
+ * @param index The specified index.\r
+ *\r
+ * @return The object stored in the array element.\r
+ */\r
+ public Object getObject(int index)\r
+ {\r
+ return objects[index];\r
+ }\r
+\r
+ /**\r
+ * Static method to return the object to hash on.\r
+ * (Object stored in specifed array, if only a single\r
+ * object, otherwise a KeyHasher wrapping the\r
+ * objects to hash on.\r
+ * (NOTE: We optimize for in-memory hash tables, hence\r
+ * we only create a wrapper when needed.)\r
+ *\r
+ * @param objects The array of objects to consider\r
+ * @param indexes The indexes of the objects in the hash key.\r
+ *\r
+ * @return The object to hash on.\r
+ */\r
+ public static Object buildHashKey(Object[] objects,\r
+ int[] indexes)\r
+ {\r
+ // Hash on single object\r
+ if (indexes.length == 1)\r
+ {\r
+ return objects[indexes[0]];\r
+ }\r
+\r
+ // Hash on multiple objects\r
+ KeyHasher mh = new KeyHasher(indexes.length);\r
+ for (int index = 0; index < indexes.length; index++)\r
+ {\r
+ mh.setObject(index, objects[indexes[index]]);\r
+ }\r
+ return mh;\r
+ }\r
+\r
+ /*\r
+ ** Methods from java.lang.Object\r
+ */\r
+\r
+ public int hashCode()\r
+ {\r
+ int retval = 0;\r
+ for (int index = 0; index < objects.length; index++)\r
+ {\r
+ retval += objects[index].hashCode();\r
+ }\r
+\r
+ return retval;\r
+ }\r
+\r
+ public boolean equals(Object obj)\r
+ {\r
+ if (!(obj instanceof KeyHasher))\r
+ return false;\r
+\r
+ KeyHasher mh = (KeyHasher) obj;\r
+\r
+ if (mh.objects.length != objects.length)\r
+ return false;\r
+\r
+ for (int index = 0; index < objects.length; index++)\r
+ {\r
+ if (! (mh.objects[index].equals(objects[index])))\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return true;\r
+ }\r
+}\r