- java.lang.Object
-
- com.pervasive.datarush.commons.util.HashUtil
-
public final class HashUtil extends Object
Utility methods for hashing different data types.These hashes are based on C code placed in the public domain by Bob Jenkins. They are advertised as strong enough to allow the use of
hash & mask
instead ofhash % prime
for computing a hash bucket.
-
-
Field Summary
Fields Modifier and Type Field Description static int
NULL_HASH_CODE
An arbitrary nonzero constant for the hash code of null valued tokens.
-
Constructor Summary
Constructors Constructor Description HashUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
binaryHashCode(byte[] bytes)
Hash a binary value using Bob Jenkins' "One-at-a-Time Hash".static int
binaryHashCode(byte[] bytes, int offset, int len)
Hash a binary value using Bob Jenkins' "One-at-a-Time Hash".static int
booleanHashCode(boolean b)
Hashes a boolean value using the same algorithm asintHashCode(int)
.static int
charHashCode(char c)
Hashes a character value using the same algorithm asintHashCode(int)
.static int
doubleHashCode(double d)
Hashes a double value using the same algorithm aslongHashCode(long)
.static int
finalMix(int a, int b, int c)
Replicates the computation of Bob Jenkins' "final" macro.static int
floatHashCode(float f)
Hashes a float value using the same algorithm asintHashCode(int)
.static int
intHashCode(int a)
Hash a binary value using Bob Jenkins' "4-byte Integer Hashing".static int
longHashCode(long l)
Hashes a long value.static int
objectHashCode(Object o)
Hashes an object usingObject.hashCode()
.static int
timestampHashCode(long epochSecs, int nanos)
Hashes a timestamp decomposed in seconds and nanos parts.
-
-
-
Field Detail
-
NULL_HASH_CODE
public static final int NULL_HASH_CODE
An arbitrary nonzero constant for the hash code of null valued tokens.- See Also:
- Constant Field Values
-
-
Method Detail
-
binaryHashCode
public static final int binaryHashCode(byte[] bytes)
Hash a binary value using Bob Jenkins' "One-at-a-Time Hash".- Parameters:
bytes
- the value to hash- Returns:
- the hash code
-
binaryHashCode
public static final int binaryHashCode(byte[] bytes, int offset, int len)
Hash a binary value using Bob Jenkins' "One-at-a-Time Hash".- Parameters:
bytes
- the value to hashoffset
- start offsetlen
- number of bytes to code- Returns:
- the hash code
-
intHashCode
public static final int intHashCode(int a)
Hash a binary value using Bob Jenkins' "4-byte Integer Hashing".- Parameters:
a
- the value to hash- Returns:
- the hash code
-
charHashCode
public static final int charHashCode(char c)
Hashes a character value using the same algorithm asintHashCode(int)
.- Parameters:
c
- the value to hash- Returns:
- the hash code
-
booleanHashCode
public static final int booleanHashCode(boolean b)
Hashes a boolean value using the same algorithm asintHashCode(int)
.- Parameters:
b
- the value to hash- Returns:
- the hash code
-
doubleHashCode
public static final int doubleHashCode(double d)
Hashes a double value using the same algorithm aslongHashCode(long)
.- Parameters:
d
- the value to hash- Returns:
- the hash code
-
floatHashCode
public static final int floatHashCode(float f)
Hashes a float value using the same algorithm asintHashCode(int)
.- Parameters:
f
- the value to hash- Returns:
- the hash code
-
longHashCode
public static final int longHashCode(long l)
Hashes a long value.- Parameters:
l
- the value to hash- Returns:
- the hash code
-
objectHashCode
public static final int objectHashCode(Object o)
Hashes an object usingObject.hashCode()
. Null values are given their own hash code.- Parameters:
o
- the value to hash- Returns:
- the hash code
-
timestampHashCode
public static final int timestampHashCode(long epochSecs, int nanos)
Hashes a timestamp decomposed in seconds and nanos parts. Note that time zone offset is deliberately not included. Equal timestamp values in different time zones must have the same hashCode.- Parameters:
epochSecs
- the seconds-since-epoch portion of the timestampnanos
- the nanoseconds portion of the timestamp- Returns:
- the hash code
-
finalMix
public static final int finalMix(int a, int b, int c)
Replicates the computation of Bob Jenkins' "final" macro.- Parameters:
a
- the first input to the macrob
- the second input to the macroc
- the third input to the macro- Returns:
- the resulting mix
-
-