Package rs.baselib.lang
Class HashCodeUtil
java.lang.Object
rs.baselib.lang.HashCodeUtil
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
FieldsModifier and TypeFieldDescriptionstatic final int
An initial value for ahashCode
, to which is added contributions from fields. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
hash
(int aSeed, boolean aBoolean) Hash booleans.static int
hash
(int aSeed, char aChar) Hash chars.static int
hash
(int aSeed, double aDouble) Hash doubles.static int
hash
(int aSeed, float aFloat) Hash floats.static int
hash
(int aSeed, int aInt) Hash ints.static int
hash
(int aSeed, long aLong) Hash longs.static int
aObject
is 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 ofhashCode
values.- See Also:
-
-
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
aObject
is a possibly-null object field, and possibly an array. IfaObject
is 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
-