1 22 package org.jboss.util; 23 24 import java.io.Serializable ; 25 26 33 public final class HashCode 34 implements Serializable , Cloneable , Comparable 35 { 36 37 private static final int NULL_HASHCODE = 0; 38 39 40 private static final int TRUE_HASHCODE = 1231; 42 43 private static final int FALSE_HASHCODE = 1237; 45 46 private int value; 47 48 54 public HashCode(final int value) { 55 this.value = value; 56 } 57 58 61 public HashCode() { 62 this(0); 63 } 64 65 71 public HashCode add(final boolean b) { 72 value ^= generate(b); 73 return this; 74 } 75 76 82 public HashCode add(final byte n) { 83 value ^= n; 84 return this; 85 } 86 87 93 public HashCode add(final char n) { 94 value ^= n; 95 return this; 96 } 97 98 104 public HashCode add(final short n) { 105 value ^= n; 106 return this; 107 } 108 109 115 public HashCode add(final int n) { 116 value ^= n; 117 return this; 118 } 119 120 126 public HashCode add(final long n) { 127 value ^= generate(n); 128 return this; 129 } 130 131 137 public HashCode add(final float f) { 138 value ^= generate(f); 139 return this; 140 } 141 142 148 public HashCode add(final double f) { 149 value ^= generate(f); 150 return this; 151 } 152 153 159 public HashCode add(final Object obj) { 160 value ^= generate(obj); 161 return this; 162 } 163 164 169 public int hashCode() { 170 return (int)value; 171 } 172 173 181 public int compareTo(final int other) { 182 return (value < other) ? -1 : (value == other) ? 0 : 1; 183 } 184 185 195 public int compareTo(final Object obj) throws ClassCastException { 196 HashCode hashCode = (HashCode)obj; 197 return compareTo(hashCode.value); 198 } 199 200 206 public boolean equals(final Object 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 221 public String toString() { 222 return String.valueOf(value); 223 } 224 225 230 public Object clone() { 231 try { 232 return super.clone(); 233 } 234 catch (CloneNotSupportedException e) { 235 throw new InternalError (); 236 } 237 } 238 239 243 249 public static int generate(final boolean value) { 250 return value ? TRUE_HASHCODE : FALSE_HASHCODE; 251 } 252 253 259 public static int generate(final long value) { 260 return (int)(value ^ (value >> 32)); 261 } 262 263 269 public static int generate(final double value) { 270 return generate(Double.doubleToLongBits(value)); 271 } 272 273 279 public static int generate(final float value) { 280 return Float.floatToIntBits(value); 281 } 282 283 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 310 public static int generate(final Object 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 [])) { 315 hashcode ^= generate((Object [])array[i], true); 316 } 317 else { 318 hashcode ^= array[i].hashCode(); 319 } 320 } 321 322 return hashcode; 323 } 324 325 331 public static int generate(final Object array[]) { 332 return generate(array, false); 333 } 334 335 341 public static int generate(final Object obj) { 342 if (obj != null) { 343 return obj.hashCode(); 344 } 345 346 return NULL_HASHCODE; 347 } 348 } 349 | Popular Tags |