org.deduced.utilities
Class HashCodeUtilities

java.lang.Object
  extended by org.deduced.utilities.HashCodeUtilities

public final class HashCodeUtilities
extends java.lang.Object

Collected methods which allow easy implementation of hashCode. Example use case:

 public int hashCode()
 {
        int result = HashCodeUtilities.SEED;
        //collect the contributions of various fields
        result = HashCodeUtilities.hash(result, fPrimitive);
        result = HashCodeUtilities.hash(result, fObject);
        result = HashCodeUtilities.hash(result, fArray);
        return result;
 }
 

Author:
Steve McDuff

Field Summary
private static int INITIAL_PRIME_NUMBER
          a random prime number to use when merging hash codes
static int INTEGER_BIT_COUNT
          bit count of a Integer
static int SEED
          An initial value for a hashCode, to which is added contributions from fields.
 
Constructor Summary
HashCodeUtilities()
           
 
Method Summary
private static int firstTerm(int aSeed)
          modify the hash code for merging
static int hash(int aSeed, boolean aBoolean)
          merge hash code with a boolean.
static int hash(int aSeed, char aChar)
          merge hash code with a char.
static int hash(int aSeed, double aDouble)
          merge hash code with a double.
static int hash(int aSeed, float aFloat)
          merge hash code with a float.
static int hash(int aSeed, int aInt)
          merge hash code with a int.
static int hash(int aSeed, long aLong)
          merge hash code with a long.
static int hash(int aSeed, java.lang.Object aObject)
          merge hash code with a java object.
private static boolean isArray(java.lang.Object aObject)
          test if an object is an array
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INTEGER_BIT_COUNT

public static final int INTEGER_BIT_COUNT
bit count of a Integer

See Also:
Constant Field Values

SEED

public static final int SEED
An initial value for a hashCode, to which is added contributions from fields. Using a non-zero value decreases collisions of hashCode values.

See Also:
Constant Field Values

INITIAL_PRIME_NUMBER

private static final int INITIAL_PRIME_NUMBER
a random prime number to use when merging hash codes

See Also:
Constant Field Values
Constructor Detail

HashCodeUtilities

public HashCodeUtilities()
Method Detail

hash

public static int hash(int aSeed,
                       boolean aBoolean)
merge hash code with a boolean.

Parameters:
aSeed - the seed hash code
aBoolean - the boolean value
Returns:
the boolean hash code

hash

public static int hash(int aSeed,
                       char aChar)
merge hash code with a char.

Parameters:
aSeed - the seed hash code
aChar - the char value
Returns:
the merged hash code

hash

public static int hash(int aSeed,
                       int aInt)
merge hash code with a int.

Parameters:
aSeed - the seed hash code
aInt - the int value
Returns:
the merged hash code

hash

public static int hash(int aSeed,
                       long aLong)
merge hash code with a long.

Parameters:
aSeed - the seed hash code
aLong - the long value
Returns:
the merged hash code

hash

public static int hash(int aSeed,
                       float aFloat)
merge hash code with a float.

Parameters:
aSeed - the seed hash code
aFloat - the float value
Returns:
the merged hash code

hash

public static int hash(int aSeed,
                       double aDouble)
merge hash code with a double.

Parameters:
aSeed - the seed hash code
aDouble - the double value
Returns:
the merged hash code

hash

public static int hash(int aSeed,
                       java.lang.Object aObject)
merge hash code with a java object. aObject is a possibly-null object field, and possibly an array. If aObject is an array, then each element may be a primitive or a possibly-null object.

Parameters:
aSeed - the seed hash code
aObject - the object
Returns:
the merged hash code

firstTerm

private static int firstTerm(int aSeed)
modify the hash code for merging

Parameters:
aSeed - the current hash code
Returns:
the hash code ready to be merged

isArray

private static boolean isArray(java.lang.Object aObject)
test if an object is an array

Parameters:
aObject - the object to test
Returns:
true if the object is an array