org.opensubsystems.core.util
Class HashCodeUtils

java.lang.Object
  extended by org.opensubsystems.core.util.HashCodeUtils

public final class HashCodeUtils
extends java.lang.Object

Collection of methods making implementation of hashcode easier. Example how hashCode can be implemented using this method: public int hashCode() { int iResult = HashCodeUtils.SEED; iResult = HashCodeUtils.hash(iResult, primitiveValue); iResult = HashCodeUtils.hash(iResult, object); iResult = HashCodeUtils.hash(iResult, array); return iResult; } This code was inspired by class published at http://www.javapractices.com/Topic28.cjp

Version:
$Id: HashCodeUtils.java,v 1.3 2007/01/07 06:14:00 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.1 2005/07/29 07:36:24 bastafidli

Field Summary
static int ODD_PRIME_NUMBER
          Constant to add to seed or previous term.
static int SEED
          An initial value to decreases collisons of computed values.
 
Method Summary
protected static int firstTerm(int iSeed)
          Compute first term for hashcode
static int hash(int iSeed, boolean bValue)
          Hash for booleans.
static int hash(int iSeed, char cValue)
          Hash for chars.
static int hash(int iSeed, double dValue)
          Hash for doubles.
static int hash(int iSeed, float fValue)
          Hash for floats.
static int hash(int iSeed, int iValue)
          Hash for ints.
static int hash(int iSeed, long lValue)
          Hash for longs.
static int hash(int iSeed, java.lang.Object oValue)
          Hash for objects .
protected static boolean isArray(java.lang.Object oObject)
          Test if object is 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 to decreases collisons of computed values.

See Also:
Constant Field Values

ODD_PRIME_NUMBER

public static final int ODD_PRIME_NUMBER
Constant to add to seed or previous term.

See Also:
Constant Field Values
Method Detail

hash

public static int hash(int iSeed,
                       boolean bValue)
Hash for booleans.

Parameters:
iSeed - - previous hashcode contributor or seed value
bValue - - value contributing to hashcode
Returns:
int - computed hashcode contributor

hash

public static int hash(int iSeed,
                       char cValue)
Hash for chars.

Parameters:
iSeed - - previous hashcode contributor or seed value
cValue - - value contributing to hashcode
Returns:
int - computed hashcode contributor

hash

public static int hash(int iSeed,
                       int iValue)
Hash for ints. Byte and short are handled by this method, through implicit conversion.

Parameters:
iSeed - - previous hashcode contributor or seed value
iValue - - value contributing to hashcode
Returns:
int - computed hashcode contributor

hash

public static int hash(int iSeed,
                       long lValue)
Hash for longs.

Parameters:
iSeed - - previous hashcode contributor or seed value
lValue - - value contributing to hashcode
Returns:
int - computed hashcode contributor

hash

public static int hash(int iSeed,
                       float fValue)
Hash for floats.

Parameters:
iSeed - - previous hashcode contributor or seed value
fValue - - value contributing to hashcode
Returns:
int - computed hashcode contributor

hash

public static int hash(int iSeed,
                       double dValue)
Hash for doubles.

Parameters:
iSeed - - previous hashcode contributor or seed value
dValue - - value contributing to hashcode
Returns:
int - computed hashcode contributor

hash

public static int hash(int iSeed,
                       java.lang.Object oValue)
Hash for objects .

Parameters:
iSeed - - previous hashcode contributor or seed value
oValue - - value contributing to hashcode. It is a possibly null object field, and possibly an array. If it is an array, then each element may be a primitive or a possibly null object.
Returns:
int - computed hashcode contributor

firstTerm

protected static int firstTerm(int iSeed)
Compute first term for hashcode

Parameters:
iSeed - - value to use to compute first temr
Returns:
int - hashcode contributor

isArray

protected static boolean isArray(java.lang.Object oObject)
Test if object is an array

Parameters:
oObject - - object to test
Returns:
boolean


Copyright © 2003 - 2006 OpenSubsystems s.r.o.