Package rs.baselib.lang
Class HashCodeUtil
java.lang.Object
rs.baselib.lang.HashCodeUtil
public final class HashCodeUtil
extends java.lang.Object
Collected methods which allow easy implementation of
hashCode.
Implementation is based on the recommendations of Effective Java, by Joshua Bloch.
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;
}
- Author:
- ralph
-
Field Summary
Fields Modifier and Type Field Description static intSEEDAn initial value for ahashCode, to which is added contributions from fields. -
Constructor Summary
Constructors Constructor Description HashCodeUtil() -
Method Summary
Modifier and Type Method Description static inthash(int aSeed, boolean aBoolean)Hash booleans.static inthash(int aSeed, char aChar)Hash chars.static inthash(int aSeed, double aDouble)Hash doubles.static inthash(int aSeed, float aFloat)Hash floats.static inthash(int aSeed, int aInt)Hash ints.static inthash(int aSeed, long aLong)Hash longs.static inthash(int aSeed, java.lang.Object aObject)aObjectis a possibly-null object field, and possibly an array.
-
Field Details
-
SEED
public static final int SEEDAn initial value for ahashCode, to which is added contributions from fields. Using a non-zero value decreases collisons ofhashCodevalues.- See Also:
- Constant Field Values
-
-
Constructor Details
-
HashCodeUtil
public HashCodeUtil()
-
-
Method Details
-
hash
public static int hash(int aSeed, boolean aBoolean)Hash booleans.- Parameters:
aSeed- - a previous seedaBoolean- - the value to combine in hash- Returns:
- the combined hash code
-
hash
public static int hash(int aSeed, char aChar)Hash chars.- Parameters:
aSeed- - a previous seedaChar- - the value to combine in hash- Returns:
- the combined hash code
-
hash
public static int hash(int aSeed, int aInt)Hash ints.- Parameters:
aSeed- - a previous seedaInt- - the value to combine in hash- Returns:
- the combined hash code
-
hash
public static int hash(int aSeed, long aLong)Hash longs.- Parameters:
aSeed- - a previous seedaLong- - the value to combine in hash- Returns:
- the combined hash code
-
hash
public static int hash(int aSeed, float aFloat)Hash floats.- Parameters:
aSeed- - a previous seedaFloat- - the value to combine in hash- Returns:
- the combined hash code
-
hash
public static int hash(int aSeed, double aDouble)Hash doubles.- Parameters:
aSeed- - a previous seedaDouble- - the value to combine in hash- Returns:
- the combined hash code
-
hash
public static int hash(int aSeed, java.lang.Object aObject)aObjectis a possibly-null object field, and possibly an array. IfaObjectis an array, then each element may be a primitive or a possibly-null object.- Parameters:
aSeed- - a previous seedaObject- - the value to combine in hash- Returns:
- the combined hash code
-