bb.util
Class HashUtil

java.lang.Object
  extended by bb.util.HashUtil

public final class HashUtil
extends Object

Provides various static utility methods for dealing with hashes.

This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).

Author:
Brent Boyer

Nested Class Summary
static class HashUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private static int prime1
          A 31-bit prime number.
private static int prime2
          A 31-bit prime number.
 
Constructor Summary
private HashUtil()
          This private constructor suppresses the default (public) constructor, ensuring non-instantiability.
 
Method Summary
static int enhance(int h)
          Attempts to return a very high quality hash function on h (i.e. one that is uniformly distributed among all possible int values, .
static int enhanceFallback1(int h)
          Returns a quick to compute hash of the input h.
private static int enhanceFallback2(int h)
          Returns a quick to compute hash of the input h.
private static int enhanceFallback3(int h)
          Returns a quick to compute hash of the input h.
private static int enhanceFallback4(int h)
           
static int hash(double d)
          Returns a high quality hash for the double arg d.
static int hash(long l)
          Returns a high quality hash for the long arg l.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

prime1

private static final int prime1
A 31-bit prime number.

See Also:
Constant Field Values

prime2

private static final int prime2
A 31-bit prime number.

See Also:
Constant Field Values
Constructor Detail

HashUtil

private HashUtil()
This private constructor suppresses the default (public) constructor, ensuring non-instantiability.

Method Detail

enhance

public static int enhance(int h)
Attempts to return a very high quality hash function on h (i.e. one that is uniformly distributed among all possible int values, .

This method is needed if h is initially a poor quality hash. Prime example: Integer.hashCode simply returns the int value, which is an extremely bad hash.

The implementation here first attempts to use the extremely strong SHA-1 hash algorithm on h. If any problem occurs (e.g. the algorithm is unavailable), enhanceFallback4(h) is returned.

See Also:
Sun documentation on MessageDigest, Sun documentation on MessageDigest, Rabin fingerprint hash method, My forum posting

enhanceFallback1

public static int enhanceFallback1(int h)
Returns a quick to compute hash of the input h. Only used by enhance as a fallback algorithm in the event of a problem.


enhanceFallback2

private static int enhanceFallback2(int h)
Returns a quick to compute hash of the input h. Only used by enhance as a fallback algorithm in the event of a problem.


enhanceFallback3

private static int enhanceFallback3(int h)
Returns a quick to compute hash of the input h. Only used by enhance as a fallback algorithm in the event of a problem.


enhanceFallback4

private static int enhanceFallback4(int h)

hash

public static int hash(double d)
Returns a high quality hash for the double arg d.


hash

public static int hash(long l)
Returns a high quality hash for the long arg l.