Package com.webmethods.caf.wsclient.util
Class HashCodeUtil
java.lang.Object
com.webmethods.caf.wsclient.util.HashCodeUtil
The following utility class allows simple construction of an effective
hashCode method. It is based on the recommendations of Effective Java,
by Joshua Bloch.
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
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) Generate a hash for a boolean valuestatic int
hash
(int aSeed, char aChar) Generate a hash for a char valuestatic int
hash
(int aSeed, double aDouble) Generate a hash for a double valuestatic int
hash
(int aSeed, float aFloat) Generate a hash for a float valuestatic int
hash
(int aSeed, int aInt) Generate a hash for an integer value Note: byte and short are handled by this method, through implicit conversion.static int
hash
(int aSeed, long aLong) Generate a hash for a long valuestatic int
Generate a hash for an Object instance
-
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) Generate a hash for a boolean value- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aBoolean
- boolean value to hash- Returns:
- the aggregated hash code
-
hash
public static int hash(int aSeed, char aChar) Generate a hash for a char value- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aChar
- char value to hash- Returns:
- the aggregated hash code
-
hash
public static int hash(int aSeed, int aInt) Generate a hash for an integer value Note: byte and short are handled by this method, through implicit conversion.- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aInt
- integer value to hash- Returns:
- the aggregated hash code
-
hash
public static int hash(int aSeed, long aLong) Generate a hash for a long value- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aLong
- long value to hash- Returns:
- the aggregated hash code
-
hash
public static int hash(int aSeed, float aFloat) Generate a hash for a float value- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aFloat
- float value to hash- Returns:
- the aggregated hash code
-
hash
public static int hash(int aSeed, double aDouble) Generate a hash for a double value- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aDouble
- double value to hash- Returns:
- the aggregated hash code
-
hash
Generate a hash for an Object instance- Parameters:
aSeed
- the initial hash value seed, used to aggregate a hash for multiple objects (see class description)aObject
- the Object instance to hash, this can include a null object or an array of objects- Returns:
- the aggregated hash code
-