Class HashCalc
- java.lang.Object
-
- de.aristaflow.adept2.util.HashCalc
-
public class HashCalc extends Object
A disposable class to facilitate the calculation of hash codes, i.e. this class is intended to be used in implementations ofObject.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 theHashCalc
using the variousfeed(..)
methods. The resulting hash code can be retrieved withhashCode()
.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
HashCalc
feed(boolean value)
Feeds aboolean
to the computed hash code.HashCalc
feed(boolean[] array)
Feeds aboolean
array to the computed hash code.HashCalc
feed(byte value)
Feeds abyte
to the computed hash code.HashCalc
feed(byte[] array)
Feeds abyte
array to the computed hash code.HashCalc
feed(char value)
Feeds achar
to the computed hash code.HashCalc
feed(double value)
Feeds adouble
to the computed hash code.HashCalc
feed(double[] array)
Feeds adouble
array to the computed hash code.HashCalc
feed(float value)
Feeds afloat
to the computed hash code.HashCalc
feed(float[] array)
Feeds afloat
array to the computed hash code.HashCalc
feed(int value)
Feeds aint
to the computed hash code.HashCalc
feed(int[] array)
Feeds aint
array to the computed hash code.HashCalc
feed(long value)
Feeds along value
to the computed hash code.HashCalc
feed(long[] array)
Feeds along
array to the computed hash code.HashCalc
feed(short value)
Feeds ashort
to the computed hash code.HashCalc
feed(short[] array)
Feeds ashort
array to the computed hash code.HashCalc
feed(Object object)
Feeds anObject
to the computed hash code.HashCalc
feed(Object[] array)
Feeds anObject
array to the computed hash code.HashCalc
feedShallow(Object[] array)
Feeds anObject
array to the computed hash code.int
hashCode()
Returns the computed hash code.
-
-
-
Constructor Detail
-
HashCalc
public HashCalc(Class<?> forClass)
Constructs a newHashCalc
for an object of the specified class.- Parameters:
forClass
- the type of object for which to calculate a hash code
-
-
Method Detail
-
feed
public HashCalc feed(boolean value)
Feeds aboolean
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(char value)
Feeds achar
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(byte value)
Feeds abyte
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(short value)
Feeds ashort
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(int value)
Feeds aint
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.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(long value)
Feeds along value
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(float value)
Feeds afloat
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(double value)
Feeds adouble
to the computed hash code.- Parameters:
value
- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(Object object)
Feeds anObject
to the computed hash code. This is done by getting the object's own hash code.- Parameters:
object
- object to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(boolean[] array)
Feeds aboolean
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(boolean[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(byte[] array)
Feeds abyte
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(byte[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(short[] array)
Feeds ashort
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(short[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(int[] array)
Feeds aint
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(int[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(long[] array)
Feeds along
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(long[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(float[] array)
Feeds afloat
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(float[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(double[] array)
Feeds adouble
array to the computed hash code. The arrays hash code is acquired usingArrays.hashCode(double[])
.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(Object[] array)
Feeds anObject
array to the computed hash code. The array's deep (!) hash code is acquired usingArrays.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, usingfeedShallow(Object[])
may save someinstanceof
s.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
feedShallow
public HashCalc feedShallow(Object[] array)
Feeds anObject
array to the computed hash code. The array's shallow (!) hash code is acquired usingArrays.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.- Parameters:
array
- array to be included in the hash code- Returns:
- this
HashCalc
-
hashCode
public int hashCode()
Returns the computed hash code.
-
-