static final class JsMessage.Hash
extends java.lang.Object
The hash takes a byte array representing arbitrary data (a number, String, or Object) and turns it into a small, hopefully unique, number. There are additional convenience functions which hash int, long, and String types.
Note: this hash has weaknesses in the two most-significant key bits and in the three least-significant seed bits. The weaknesses are small and practically speaking, will not affect the distribution of hash values. Still, it would be good practice not to choose seeds 0, 1, 2, 3, ..., n to yield n, independent hash functions. Use pseudo-random seeds instead.
This code is based on the work of Craig Silverstein and Sanjay Ghemawat in, then forked from com.google.common.
The original code for the hash function is courtesy Bob Jenkins.
TODO(anatol): Add stream hashing functionality.
Modifier and Type | Field and Description |
---|---|
private static long |
CONSTANT64
Hash constant (64 bit)
|
private static long |
SEED64
Default hash seed (64 bit)
|
Modifier | Constructor and Description |
---|---|
private |
Hash() |
Modifier and Type | Method and Description |
---|---|
private static long |
hash64(byte[] value,
int offset,
int length,
long seed)
Hash byte array to a 64 bit value using the supplied seed.
|
private static long |
hash64(byte[] value,
long seed)
Hash byte array to a 64 bit value using the supplied seed.
|
(package private) static long |
hash64(java.lang.String value)
Hash a string to a 64 bit value.
|
private static long |
hash64(java.lang.String value,
long seed)
Hash a string to a 64 bit value using the supplied seed.
|
private static long |
mix64(long a,
long b,
long c)
Mixes longs a, b, and c, and returns the final value of c.
|
private static long |
word64At(byte[] bytes,
int offset) |
private static final long SEED64
private static final long CONSTANT64
static long hash64(@Nullable java.lang.String value)
value
- the string to hashprivate static long hash64(@Nullable java.lang.String value, long seed)
value
- the string to hashseed
- the seedprivate static long hash64(byte[] value, long seed)
value
- the bytes to hashseed
- the seedprivate static long hash64(byte[] value, int offset, int length, long seed)
value
- the bytes to hashoffset
- the starting position of value where bytes are
used for the hash computationlength
- number of bytes of value that are used for the
hash computationseed
- the seedprivate static long word64At(byte[] bytes, int offset)
private static long mix64(long a, long b, long c)