2. HashCode utilities.
- By Mark Davis
- March 14, 2000
Listing 2. HashCode utilities.
final class Utility {
static final int hashCode(int source, boolean x) {
return PRIME*source + (x ? 1 : 0);
}
static final int hashCode(int source, int x) {
return PRIME*source + x;
}
static final int hashCode(int source, long x) {
return PRIME*source + (int)(PRIME*(x >>> 32)
+ (x & 0xFFFFFFFF));
}
static final int hashCode(int source, float x) {
return hashCode(source, x == 0.0F ? 0 : Float.floatToIntBits(x));
}
static final int hashCode(int source, double x) {
return hashCode(source, x == 0.0 ? 0L :
Double.doubleToLongBits(x));
}
static final int hashCode(int source, Object x) {
return (x == null) ? 0 : PRIME*source + x.hashCode();
}
static final int hashCode(int source, Object[] x) {
for (int i = 0; i < x.length; ++i) {
source = hashCode(source, x[i]);
}
return source;
}
// repeat for arrays of primitives,
// arrays of arrays,... as required
}