public class HashCalc
extends java.lang.Object
Object.hashCode().
This class knows how to deal with primitive types and objects, but also
arrays (of primitive types and objects), even nested ones. It also deals with
order, i.e. feeding it the same values in a different order will also produce
a different hash code. null values don't have to be treated specially
either.
The initial hash code is based on the class name provided in the constructor.
Arbitrary values (usually those that are also queried in
Object.equals(Object)) can then be fed to the HashCalc using
the various feed(..) methods. The resulting hash code can be
retrieved with hashCode().
| Constructor and Description |
|---|
HashCalc(java.lang.Class<?> forClass)
Constructs a new
HashCalc for an object of the specified class. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(java.lang.Object obj) |
static boolean |
equals(java.lang.Object first,
java.lang.Object second)
Compares two values for equality.
|
HashCalc |
feed(boolean value)
Feeds a
boolean to the computed hash code. |
HashCalc |
feed(boolean[] array)
Feeds a
boolean array to the computed hash code. |
HashCalc |
feed(byte value)
Feeds a
byte to the computed hash code. |
HashCalc |
feed(byte[] array)
Feeds a
byte array to the computed hash code. |
HashCalc |
feed(char value)
Feeds a
char to the computed hash code. |
HashCalc |
feed(double value)
Feeds a
double to the computed hash code. |
HashCalc |
feed(double[] array)
Feeds a
double array to the computed hash code. |
HashCalc |
feed(float value)
Feeds a
float to the computed hash code. |
HashCalc |
feed(float[] array)
Feeds a
float array to the computed hash code. |
HashCalc |
feed(int value)
Feeds a
int to the computed hash code. |
HashCalc |
feed(int[] array)
Feeds a
int array to the computed hash code. |
HashCalc |
feed(long value)
Feeds a
long value to the computed hash code. |
HashCalc |
feed(long[] array)
Feeds a
long array to the computed hash code. |
HashCalc |
feed(java.lang.Object object)
Feeds an
Object to the computed hash code. |
HashCalc |
feed(java.lang.Object[] array)
Feeds an
Object array to the computed hash code. |
HashCalc |
feed(short value)
Feeds a
short to the computed hash code. |
HashCalc |
feed(short[] array)
Feeds a
short array to the computed hash code. |
HashCalc |
feedShallow(java.lang.Object[] array)
Feeds an
Object array to the computed hash code. |
int |
hashCode()
Returns the computed hash code.
|
public HashCalc(java.lang.Class<?> forClass)
HashCalc for an object of the specified class.forClass - the type of object for which to calculate a hash codepublic HashCalc feed(boolean value)
boolean to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(char value)
char to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(byte value)
byte to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(short value)
short to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(int value)
int to the computed hash code.
All other feed() methods are redirected to this method. So in order
to provide custom hash code computations only this method needs to be
overridden.
value - value to be included in the hash codeHashCalcpublic HashCalc feed(long value)
long value to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(float value)
float to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(double value)
double to the computed hash code.value - value to be included in the hash codeHashCalcpublic HashCalc feed(java.lang.Object object)
Object to the computed hash code. This is done by getting
the object's own hash code.object - object to be included in the hash codeHashCalcpublic HashCalc feed(boolean[] array)
boolean array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(boolean[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(byte[] array)
byte array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(byte[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(short[] array)
short array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(short[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(int[] array)
int array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(int[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(long[] array)
long array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(long[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(float[] array)
float array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(float[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(double[] array)
double array to the computed hash code. The arrays hash
code is acquired using Arrays.hashCode(double[]).array - array to be included in the hash codeHashCalcpublic HashCalc feed(java.lang.Object[] array)
Object array to the computed hash code. The array's deep
(!) hash code is acquired using Arrays.deepHashCode(Object[]). So
be careful if the array could potentially contain direct or indirect
references to itself! If you are absolutely sure that the array does not
contain any other arrays, using feedShallow(Object[]) may save
some instanceofs.array - array to be included in the hash codeHashCalcpublic HashCalc feedShallow(java.lang.Object[] array)
Object array to the computed hash code. The array's
shallow (!) hash code is acquired using Arrays.hashCode(Object[]).
So if the array contains other arrays, their hash code is based on their
identity rather than their content. This may be useful if the array could
potentially contain direct or indirect references to itself.array - array to be included in the hash codeHashCalcpublic int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic static boolean equals(java.lang.Object first,
java.lang.Object second)
null,
the second also has to be null. Otherwise
equals(Object) is on the first value will be returned.first - The first value which to check for equality.second - The second value which to check for equality.null and equals(Object).