xml2fol
Class HashCodeUtil

java.lang.Object
  extended by xml2fol.HashCodeUtil

public final class HashCodeUtil
extends java.lang.Object

The following utility class allows simple construction of an effective hashCode method. It is based on the recommendations of Effective Java, by Joshua Bloch. http://www.javapractices.com/topic/TopicAction.do?Id=28 Collected methods which allow easy implementation of hashCode. Example use case:

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


Field Summary
static int SEED
          An initial value for a hashCode, to which is added contributions from fields.
 
Constructor Summary
HashCodeUtil()
           
 
Method Summary
static int hash(int aSeed, boolean aBoolean)
          Produces hash for booleans.
static int hash(int aSeed, char aChar)
          Produces hash for chars.
static int hash(int aSeed, double aDouble)
          Produces hash for doubles.
static int hash(int aSeed, float aFloat)
          Produces hash for floats.
static int hash(int aSeed, int aInt)
          Produces hash for integers.
static int hash(int aSeed, long aLong)
          Produces hash for longs.
static int hash(int aSeed, java.lang.Object aObject)
          aObject is a possibly-null object field, and possibly an array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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 collisons of hashCode values.

See Also:
Constant Field Values
Constructor Detail

HashCodeUtil

public HashCodeUtil()
Method Detail

hash

public static int hash(int aSeed,
                       boolean aBoolean)
Produces hash for booleans.

Parameters:
aSeed - - The seed value
aBoolean - - The value to hash
Returns:
- A hashvalue

hash

public static int hash(int aSeed,
                       char aChar)
Produces hash for chars.

Parameters:
aSeed - - The seed value
aChar - - The value to hash
Returns:
- A hashvalue

hash

public static int hash(int aSeed,
                       int aInt)
Produces hash for integers.

Parameters:
aSeed - - The seed value
aInt - - The value to hash
Returns:
- A hashvalue

hash

public static int hash(int aSeed,
                       long aLong)
Produces hash for longs.

Parameters:
aSeed - - The seed value
aLong - - The value to hash
Returns:
- A hashvalue

hash

public static int hash(int aSeed,
                       float aFloat)
Produces hash for floats.

Parameters:
aSeed - - The seed value
aFloat - - The value to hash
Returns:
- A hashvalue

hash

public static int hash(int aSeed,
                       double aDouble)
Produces hash for doubles.

Parameters:
aSeed - - The seed value
aDouble - - The value to hash
Returns:
- A hashvalue

hash

public static int hash(int aSeed,
                       java.lang.Object aObject)
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 value
aObject- - The value to hash
Returns:
- A hashvalue