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
FieldsModifier and TypeFieldDescriptionstatic final intThe 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 intAn initial value for ahashCode, to which we add contributions from fields. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic inthash(int seed, boolean b) static inthash(int seed, char c) static inthash(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 inthash(int seed, float f) float support done viaFloat.floatToIntBits(float)which allows the encoding of a float as an int.static inthash(int seed, int i) Used for ints, bytes and shortsstatic inthash(int seed, long l) long support done by shifting by 32 (using unsigned shift)static intois 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 ofhashCodevalues.- 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
ois a possibly-null object field, and possibly an array. Ifois an array, then each element may be a primitive or a possibly-null object.
-