Package org.biojava.nbio.core.util
Class Hashcoder
- java.lang.Object
-
- org.biojava.nbio.core.util.Hashcoder
-
public class Hashcoder extends Object
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 intpublic int hashCode() { int result = Hashcoder.SEED; result = Hashcoder.hash(result, this.getName()); result = Hashcoder.hash(result, this.getAge()); return result; }
- Author:
- ayates
-
-
Field Summary
Fields Modifier and Type Field Description static int
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 resultstatic int
SEED
An initial value for ahashCode
, to which we add contributions from fields.
-
Constructor Summary
Constructors Constructor Description Hashcoder()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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
hash(int seed, Object o)
o
is a possibly-null object field, and possibly an array.
-
-
-
Field Detail
-
SEED
public static final int SEED
An initial value for ahashCode
, to which we add contributions from fields. Using a non-zero value decreases collisions ofhashCode
values.- See Also:
- Constant Field Values
-
PRIME
public static final int 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:
- Constant Field Values
-
-
Constructor Detail
-
Hashcoder
public Hashcoder()
-
-
Method Detail
-
hash
public static int hash(int seed, boolean b)
-
hash
public static int hash(int seed, char c)
-
hash
public static int hash(int seed, int i)
Used for ints, bytes and shorts
-
hash
public static int hash(int seed, long l)
long support done by shifting by 32 (using unsigned shift)
-
hash
public static int hash(int seed, float f)
float support done viaFloat.floatToIntBits(float)
which allows the encoding of a float as an int. We can then hash as normal.
-
hash
public 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.
-
-