Package org.biojava.nbio.core.util
Class Hashcoder
java.lang.Object
org.biojava.nbio.core.util.Hashcoder
Contains helper methods for generating a HashCode without having to resort to
the commons lang hashcode builders.
Example where the property name is a String and the property age is an int
public int hashCode() { int result = Hashcoder.SEED; result = Hashcoder.hash(result, this.getName()); result = Hashcoder.hash(result, this.getAge()); return result; }
- Author:
- ayates
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The prime number used to multiply any calculated hashcode seed by i.e. result = PRIME*result + c Where result is the result of the previous calculation (at first this will be seed) and c is the calculated int to add to resultstatic final int
An initial value for ahashCode
, to which we add contributions from fields. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
hash
(int seed, boolean b) static int
hash
(int seed, char c) static int
hash
(int seed, double d) double support which is done using the same techinque as float hashing except we convert to a long not to an int.static int
hash
(int seed, float f) float support done viaFloat.floatToIntBits(float)
which allows the encoding of a float as an int.static int
hash
(int seed, int i) Used for ints, bytes and shortsstatic int
hash
(int seed, long l) long support done by shifting by 32 (using unsigned shift)static int
o
is a possibly-null object field, and possibly an array.
-
Field Details
-
SEED
An initial value for ahashCode
, to which we add contributions from fields. Using a non-zero value decreases collisions ofhashCode
values.- See Also:
-
PRIME
The prime number used to multiply any calculated hashcode seed by i.e. result = PRIME*result + c Where result is the result of the previous calculation (at first this will be seed) and c is the calculated int to add to result- See Also:
-
-
Constructor Details
-
Hashcoder
public Hashcoder()
-
-
Method Details
-
hash
-
hash
-
hash
Used for ints, bytes and shorts -
hash
long support done by shifting by 32 (using unsigned shift) -
hash
float support done viaFloat.floatToIntBits(float)
which allows the encoding of a float as an int. We can then hash as normal. -
hash
double support which is done using the same techinque as float hashing except we convert to a long not to an int. -
hash
o
is a possibly-null object field, and possibly an array. Ifo
is an array, then each element may be a primitive or a possibly-null object.
-