org.jlf.util
Class Hash

java.lang.Object
  |
  +--org.jlf.util.Hash

public class Hash
extends java.lang.Object

The Hash class is a utility class to help you properly implement the hashCode() method when you override the equals(Object) method. This code was taken from the Mark Davis "Durable Java" article in the April 2000 issue of JavaReport, via http://www.javareport.com.

To use this class, see the following example of a hashCode() override method:


 public int hashCode()
 {
     int result = super.hashCode(); // Use this if your object is a subclass
                                    // of anything but Object!

     //int result = 0;              // Use this if your object is a direct
                                    // subclass of Object!

     result = Hash.hashCode(result,primitiveField);
     result = Hash.hashCode(result,flagField);
     result = Hash.hashCode(result,longField);
     result = Hash.hashCode(result,doubleField);
     result = Hash.hashCode(result,objectField);
     result = Hash.hashCode(result,maybeNullField);
     result = Hash.hashCode(result,arrayField);
     return result;
 }
 


Field Summary
static int PRIME
          A large prime number is needed to provide a good hash distribution.
 
Constructor Summary
Hash()
           
 
Method Summary
static int hashCode(int source, boolean x)
          Use this method to add a boolean to your hashCode result.
static int hashCode(int source, double x)
          Use this method to add a double to your hashCode result.
static int hashCode(int source, float x)
          Use this method to add a float to your hashCode result.
static int hashCode(int source, int x)
          Use this method to add an int to your hashCode result.
static int hashCode(int source, long x)
          Use this method to add a long to your hashCode result.
static int hashCode(int source, java.lang.Object x)
          Use this method to add an arbitrary Object to your hashCode result.
static int hashCode(int source, java.lang.Object[] x)
          Use this method to add an array of Objects to your hashCode result.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRIME

public static final int PRIME
A large prime number is needed to provide a good hash distribution.

See Also:
Constant Field Values
Constructor Detail

Hash

public Hash()
Method Detail

hashCode

public static int hashCode(int source,
                           boolean x)
Use this method to add a boolean to your hashCode result.


hashCode

public static int hashCode(int source,
                           int x)
Use this method to add an int to your hashCode result.


hashCode

public static int hashCode(int source,
                           long x)
Use this method to add a long to your hashCode result.


hashCode

public static int hashCode(int source,
                           float x)
Use this method to add a float to your hashCode result.


hashCode

public static int hashCode(int source,
                           double x)
Use this method to add a double to your hashCode result.


hashCode

public static int hashCode(int source,
                           java.lang.Object x)
Use this method to add an arbitrary Object to your hashCode result.


hashCode

public static int hashCode(int source,
                           java.lang.Object[] x)
Use this method to add an array of Objects to your hashCode result. Note that this is a brute force approach, using every element of the array.