KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > HashCodeUtils


1 /*
2  * Copyright (c) 2005 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: HashCodeUtils.java,v 1.3 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.lang.reflect.Array JavaDoc;
25
26 /**
27  * Collection of methods making implementation of hashcode easier.
28  *
29  * Example how hashCode can be implemented using this method:
30  *
31  * public int hashCode()
32  * {
33  * int iResult = HashCodeUtils.SEED;
34  * iResult = HashCodeUtils.hash(iResult, primitiveValue);
35  * iResult = HashCodeUtils.hash(iResult, object);
36  * iResult = HashCodeUtils.hash(iResult, array);
37  * return iResult;
38  * }
39  *
40  * This code was inspired by class published at
41  * http://www.javapractices.com/Topic28.cjp
42  *
43  * @version $Id: HashCodeUtils.java,v 1.3 2007/01/07 06:14:00 bastafidli Exp $
44  * @author Miro Halas
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.1 2005/07/29 07:36:24 bastafidli
47  */

48 public final class HashCodeUtils
49 {
50    // Constants ////////////////////////////////////////////////////////////////
51

52    /**
53     * An initial value to decreases collisons of computed values.
54     */

55    public static final int SEED = 15;
56
57    /**
58     * Constant to add to seed or previous term.
59     */

60    public static final int ODD_PRIME_NUMBER = 37;
61
62    // Constructors /////////////////////////////////////////////////////////////
63

64    /**
65     * Private constructor since this class cannot be instantiated
66     */

67    private HashCodeUtils(
68    )
69    {
70       // Do nothing
71
}
72    
73    // Public methods ///////////////////////////////////////////////////////////
74

75    /**
76     * Hash for booleans.
77     *
78     * @param iSeed - previous hashcode contributor or seed value
79     * @param bValue - value contributing to hashcode
80     * @return int - computed hashcode contributor
81     */

82    public static int hash(
83       int iSeed,
84       boolean bValue
85    )
86    {
87       return firstTerm(iSeed) + (bValue ? 1 : 0);
88    }
89
90    /**
91     * Hash for chars.
92     *
93     * @param iSeed - previous hashcode contributor or seed value
94     * @param cValue - value contributing to hashcode
95     * @return int - computed hashcode contributor
96     */

97    public static int hash(
98       int iSeed,
99       char cValue
100    )
101    {
102       return firstTerm(iSeed) + (int)cValue;
103    }
104
105    /**
106     * Hash for ints. Byte and short are handled by this method, through implicit
107     * conversion.
108     *
109     * @param iSeed - previous hashcode contributor or seed value
110     * @param iValue - value contributing to hashcode
111     * @return int - computed hashcode contributor
112     */

113    public static int hash(
114       int iSeed,
115       int iValue
116    )
117    {
118       return firstTerm(iSeed) + iValue;
119    }
120
121    /**
122     * Hash for longs.
123     *
124     * @param iSeed - previous hashcode contributor or seed value
125     * @param lValue - value contributing to hashcode
126     * @return int - computed hashcode contributor
127     */

128    public static int hash(
129       int iSeed,
130       long lValue
131    )
132    {
133       return firstTerm(iSeed) + (int)(lValue ^ (lValue >>> 32));
134    }
135
136    /**
137     * Hash for floats.
138     *
139     * @param iSeed - previous hashcode contributor or seed value
140     * @param fValue - value contributing to hashcode
141     * @return int - computed hashcode contributor
142     */

143    public static int hash(
144       int iSeed,
145       float fValue
146    )
147    {
148       return hash(iSeed, Float.floatToIntBits(fValue));
149    }
150
151    /**
152     * Hash for doubles.
153     *
154     * @param iSeed - previous hashcode contributor or seed value
155     * @param dValue - value contributing to hashcode
156     * @return int - computed hashcode contributor
157     */

158    public static int hash(
159       int iSeed,
160       double dValue
161    )
162    {
163       return hash(iSeed, Double.doubleToLongBits(dValue));
164    }
165
166    /**
167     * Hash for objects .
168     *
169     * @param iSeed - previous hashcode contributor or seed value
170     * @param oValue - value contributing to hashcode. It is a possibly null
171     * object field, and possibly an array. If it is an array,
172     * then each element may be a primitive or a possibly null
173     * object.
174     * @return int - computed hashcode contributor
175     */

176    public static int hash(
177       int iSeed,
178       Object JavaDoc oValue
179    )
180    {
181       int result = iSeed;
182       if (oValue == null)
183       {
184          result = hash(result, 0);
185       }
186       else if (!isArray(oValue))
187       {
188          result = hash(result, oValue.hashCode());
189       }
190       else
191       {
192          int length = Array.getLength(oValue);
193          for (int idx = 0; idx < length; ++idx)
194          {
195             Object JavaDoc item = Array.get(oValue, idx);
196             // recursive call!
197
result = hash(result, item);
198          }
199       }
200       return result;
201    }
202    
203    // Helper methods ///////////////////////////////////////////////////////////
204

205    /**
206     * Compute first term for hashcode
207     *
208     * @param iSeed - value to use to compute first temr
209     * @return int - hashcode contributor
210     */

211    protected static int firstTerm(
212       int iSeed
213    )
214    {
215       return ODD_PRIME_NUMBER * iSeed;
216    }
217
218    /**
219     * Test if object is an array
220     *
221     * @param oObject - object to test
222     * @return boolean
223     */

224    protected static boolean isArray(
225       Object JavaDoc oObject
226    )
227    {
228       return oObject.getClass().isArray();
229    }
230 }
231
Popular Tags