de.goerntkai.toolbox.misc
Class HashCodeUtil

java.lang.Object
  extended byde.goerntkai.toolbox.misc.HashCodeUtil

public abstract class HashCodeUtil
extends java.lang.Object

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;
 }
 

Version:
$Id: HashCodeUtil.java,v 1.3 2007/04/29 17:54:03 GoerntKai Exp $
Author:
Kai Görnt

Field Summary
static int SEED
          An initial value for a hashCode, to which is added contributions from fields.
 
Method Summary
static int hash(int aSeed, boolean aBoolean)
          Booleans.
static int hash(int aSeed, char aChar)
          Characters.
static int hash(int aSeed, double aDouble)
          Doubles.
static int hash(int aSeed, float aFloat)
          Floats.
static int hash(int aSeed, int aInt)
          Integers.
static int hash(int aSeed, long aLong)
          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
Method Detail

hash

public static int hash(int aSeed,
                       boolean aBoolean)
Booleans.

Parameters:
aSeed - for the code
aBoolean - to create the code for
Returns:
hashcode for a boolean

hash

public static int hash(int aSeed,
                       char aChar)
Characters.

Parameters:
aSeed - for the code
aChar - to create the code for
Returns:
hashcode for a boolean

hash

public static int hash(int aSeed,
                       double aDouble)
Doubles.

Parameters:
aSeed - for the code
aDouble - to create the code for
Returns:
hashcode for a boolean

hash

public static int hash(int aSeed,
                       float aFloat)
Floats.

Parameters:
aSeed - for the code
aFloat - to create the code for
Returns:
hashcode for a boolean

hash

public static int hash(int aSeed,
                       int aInt)
Integers.

Parameters:
aSeed - for the code
aInt - to create the code for
Returns:
hashcode for a boolean

hash

public static int hash(int aSeed,
                       long aLong)
Longs.

Parameters:
aSeed - for the code
aLong - to create the code for
Returns:
hashcode for a boolean

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 - for the code
aObject - to create the code for
Returns:
hashcode for a boolean


Copyright © 2004-2007 Kai Görnt. All Rights Reserved.