KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > HashCode


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software 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 GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * A hash-code generator and a collection of static hash-code generation
28  * methods.
29  *
30  * @version <tt>$Revision: 1958 $</tt>
31  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
32  */

33 public final class HashCode
34    implements Serializable JavaDoc, Cloneable JavaDoc, Comparable JavaDoc
35 {
36    /** Hashcode for a 'null' value. */
37    private static final int NULL_HASHCODE = 0;
38
39    /** Hashcode for a 'true' boolean */
40    private static final int TRUE_HASHCODE = 1231; // completely arbitrary
41

42    /** Hashcode for a 'false' boolean */
43    private static final int FALSE_HASHCODE = 1237; // completely arbitrary
44

45    /** The hash-code value. */
46    private int value;
47
48    /**
49     * Construct a new <tt>HashCode</tt> using the given <tt>int</tt> as the
50     * base value.
51     *
52     * @param value <tt>int</tt> to use as the base value.
53     */

54    public HashCode(final int value) {
55       this.value = value;
56    }
57
58    /**
59     * Construct a new <tt>HashCode</tt>.
60     */

61    public HashCode() {
62       this(0);
63    }
64
65    /**
66     * Add the hash-code of the given value.
67     *
68     * @param b Value to get hash-code from.
69     * @return <i>This</i> <tt>HashCode</tt>.
70     */

71    public HashCode add(final boolean b) {
72       value ^= generate(b);
73       return this;
74    }
75
76    /**
77     * Add the hash-code of the given value.
78     *
79     * @param n Value to get hash-code from.
80     * @return <i>This</i> <tt>HashCode</tt>.
81     */

82    public HashCode add(final byte n) {
83       value ^= n;
84       return this;
85    }
86
87    /**
88     * Add the hash-code of the given value.
89     *
90     * @param n Value to get hash-code from.
91     * @return <i>This</i> <tt>HashCode</tt>.
92     */

93    public HashCode add(final char n) {
94       value ^= n;
95       return this;
96    }
97
98    /**
99     * Add the hash-code of the given value.
100     *
101     * @param n Value to get hash-code from.
102     * @return <i>This</i> <tt>HashCode</tt>.
103     */

104    public HashCode add(final short n) {
105       value ^= n;
106       return this;
107    }
108
109    /**
110     * Add the hash-code of the given value.
111     *
112     * @param n Value to get hash-code from.
113     * @return <i>This</i> <tt>HashCode</tt>.
114     */

115    public HashCode add(final int n) {
116       value ^= n;
117       return this;
118    }
119
120    /**
121     * Add the hash-code of the given value.
122     *
123     * @param n Value to get hash-code from.
124     * @return <i>This</i> <tt>HashCode</tt>.
125     */

126    public HashCode add(final long n) {
127       value ^= generate(n);
128       return this;
129    }
130
131    /**
132     * Add the hash-code of the given value.
133     *
134     * @param f Value to get hash-code from.
135     * @return <i>This</i> <tt>HashCode</tt>.
136     */

137    public HashCode add(final float f) {
138       value ^= generate(f);
139       return this;
140    }
141
142    /**
143     * Add the hash-code of the given value.
144     *
145     * @param f Value to get hash-code from.
146     * @return <i>This</i> <tt>HashCode</tt>.
147     */

148    public HashCode add(final double f) {
149       value ^= generate(f);
150       return this;
151    }
152
153    /**
154     * Add the hash-code of the given object.
155     *
156     * @param obj Value to get hash-code from.
157     * @return <i>This</i> <tt>HashCode</tt>.
158     */

159    public HashCode add(final Object JavaDoc obj) {
160       value ^= generate(obj);
161       return this;
162    }
163
164    /**
165     * Get the hash-code.
166     *
167     * @return Hash-code.
168     */

169    public int hashCode() {
170       return (int)value;
171    }
172
173    /**
174     * Compares this object with the specified <tt>int</tt> for order.
175     *
176     * @param other Value to compare with.
177     * @return A negative integer, zero, or a positive integer as
178     * this object is less than, equal to, or greater than
179     * the specified object.
180     */

181    public int compareTo(final int other) {
182       return (value < other) ? -1 : (value == other) ? 0 : 1;
183    }
184
185    /**
186     * Compares this object with the specified object for order.
187     *
188     * @param other Value to compare with.
189     * @return A negative integer, zero, or a positive integer as
190     * this object is less than, equal to, or greater than
191     * the specified object.
192     *
193     * @throws ClassCastException Object is not a <tt>HashCode</tt>.
194     */

195    public int compareTo(final Object JavaDoc obj) throws ClassCastException JavaDoc {
196       HashCode hashCode = (HashCode)obj;
197       return compareTo(hashCode.value);
198    }
199
200    /**
201     * Test the equality of this <tt>HashCode</tt> and another object.
202     *
203     * @param obj Object to test equality with.
204     * @return True if object is equal.
205     */

206    public boolean equals(final Object JavaDoc obj) {
207       if (obj == this) return true;
208
209       if (obj != null && obj.getClass() == getClass()) {
210          return value == ((HashCode)obj).value;
211       }
212
213       return false;
214    }
215
216    /**
217     * Return a string representation of this <tt>HashCode</tt>.
218     *
219     * @return A string representation of this <tt>HashCode</tt>.
220     */

221    public String JavaDoc toString() {
222       return String.valueOf(value);
223    }
224
225    /**
226     * Return a cloned copy of this <tt>HashCode</tt>.
227     *
228     * @return Cloned <tt>HashCode</tt>.
229     */

230    public Object JavaDoc clone() {
231       try {
232          return super.clone();
233       }
234       catch (CloneNotSupportedException JavaDoc e) {
235          throw new InternalError JavaDoc();
236       }
237    }
238
239    /////////////////////////////////////////////////////////////////////////
240
// Generation Methods //
241
/////////////////////////////////////////////////////////////////////////
242

243    /**
244     * Generate a hash code for a boolean value.
245     *
246     * @param value Boolean value to generate hash code from.
247     * @return Hash code.
248     */

249    public static int generate(final boolean value) {
250       return value ? TRUE_HASHCODE : FALSE_HASHCODE;
251    }
252
253    /**
254     * Generate a hash code for a long value.
255     *
256     * @param value Long value to generate hash code from.
257     * @return Hash code.
258     */

259    public static int generate(final long value) {
260       return (int)(value ^ (value >> 32));
261    }
262
263    /**
264     * Generate a hash code for a double value.
265     *
266     * @param value Double value to generate hash code from.
267     * @return Hash code.
268     */

269    public static int generate(final double value) {
270       return generate(Double.doubleToLongBits(value));
271    }
272
273    /**
274     * Generate a hash code for a float value.
275     *
276     * @param value Float value to generate hash code from.
277     * @return Hash code.
278     */

279    public static int generate(final float value) {
280       return Float.floatToIntBits(value);
281    }
282
283    /**
284     * Generate a hash code for a byte array.
285     *
286     * @param bytes An array of bytes to generate a hash code from.
287     * @return Hash code.
288     */

289    public static int generate(final byte[] bytes) {
290       int hashcode = 0;
291
292       for (int i=0; i<bytes.length; i++) {
293          hashcode <<= 1;
294          hashcode ^= bytes[i];
295       }
296
297       return hashcode;
298    }
299
300    /**
301     * Generate a hash code for an object array.
302     *
303     * <p>Does not handle nested primitive array elements.
304     *
305     * @param array Array to generate hashcode for.
306     * @param deep True to traverse elements which are arrays to
307     * determine the elements hash code.
308     * @return Hash code.
309     */

310    public static int generate(final Object JavaDoc array[], final boolean deep) {
311       int hashcode = 0;
312
313       for (int i=0; i<array.length; i++) {
314          if (deep && (array[i] instanceof Object JavaDoc[])) {
315             hashcode ^= generate((Object JavaDoc[])array[i], true);
316          }
317          else {
318             hashcode ^= array[i].hashCode();
319          }
320       }
321
322       return hashcode;
323    }
324
325    /**
326     * Generate a shallow hash code for an object array.
327     *
328     * @param array Array to generate hashcode for.
329     * @return Hash code.
330     */

331    public static int generate(final Object JavaDoc array[]) {
332       return generate(array, false);
333    }
334
335    /**
336     * Generate a hash code for an object.
337     *
338     * @param obj Object to generate hashcode for.
339     * @return Hash code.
340     */

341    public static int generate(final Object JavaDoc obj) {
342       if (obj != null) {
343          return obj.hashCode();
344       }
345
346       return NULL_HASHCODE;
347    }
348 }
349
Popular Tags