|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.jlf.util.Hash
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 |
public static final int PRIME
Constructor Detail |
public Hash()
Method Detail |
public static int hashCode(int source, boolean x)
public static int hashCode(int source, int x)
public static int hashCode(int source, long x)
public static int hashCode(int source, float x)
public static int hashCode(int source, double x)
public static int hashCode(int source, java.lang.Object x)
public static int hashCode(int source, java.lang.Object[] x)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |