KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > store > access > KeyHasher


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

21
22 package org.apache.derby.iapi.store.access;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 /**
27     Provides the ability to hash on multiple objects.
28 */

29 public class KeyHasher
30 {
31     
32     private final Object JavaDoc[] objects;
33
34     public KeyHasher(int size)
35     {
36         objects = new Object JavaDoc[size];
37     }
38
39     /**
40      * Set array element at the specified index to the specified object.
41      *
42      * @param index The specified index
43      * @param object The specified object.
44      */

45     public void setObject(int index, Object JavaDoc object)
46     {
47         objects[index] = object;
48     }
49
50     /**
51      * Get the object stored at the specified index.
52      *
53      * @param index The specified index.
54      *
55      * @return The object stored in the array element.
56      */

57     public Object JavaDoc getObject(int index)
58     {
59         return objects[index];
60     }
61
62     /**
63      * Static method to return the object to hash on.
64      * (Object stored in specifed array, if only a single
65      * object, otherwise a KeyHasher wrapping the
66      * objects to hash on.
67      * (NOTE: We optimize for in-memory hash tables, hence
68      * we only create a wrapper when needed.)
69      *
70      * @param objects The array of objects to consider
71      * @param indexes The indexes of the objects in the hash key.
72      *
73      * @return The object to hash on.
74      */

75     public static Object JavaDoc buildHashKey(Object JavaDoc[] objects,
76                                       int[] indexes)
77     {
78         // Hash on single object
79
if (indexes.length == 1)
80         {
81             return objects[indexes[0]];
82         }
83
84         // Hash on multiple objects
85
KeyHasher mh = new KeyHasher(indexes.length);
86         for (int index = 0; index < indexes.length; index++)
87         {
88             mh.setObject(index, objects[indexes[index]]);
89         }
90         return mh;
91     }
92
93     /*
94     ** Methods from java.lang.Object
95     */

96
97     public int hashCode()
98     {
99         int retval = 0;
100         for (int index = 0; index < objects.length; index++)
101         {
102             retval += objects[index].hashCode();
103         }
104
105         return retval;
106     }
107
108     public boolean equals(Object JavaDoc obj)
109     {
110         if (!(obj instanceof KeyHasher))
111             return false;
112
113         KeyHasher mh = (KeyHasher) obj;
114
115         if (mh.objects.length != objects.length)
116             return false;
117
118         for (int index = 0; index < objects.length; index++)
119         {
120             if (! (mh.objects[index].equals(objects[index])))
121             {
122                 return false;
123             }
124         }
125
126         return true;
127     }
128 }
129
Popular Tags