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.
nullvalues 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 theHashCalcusing the variousfeed(..)methods. The resulting hash code can be retrieved withhashCode().
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)HashCalcfeed(boolean value)Feeds abooleanto the computed hash code.HashCalcfeed(boolean[] array)Feeds abooleanarray to the computed hash code.HashCalcfeed(byte value)Feeds abyteto the computed hash code.HashCalcfeed(byte[] array)Feeds abytearray to the computed hash code.HashCalcfeed(char value)Feeds acharto the computed hash code.HashCalcfeed(double value)Feeds adoubleto the computed hash code.HashCalcfeed(double[] array)Feeds adoublearray to the computed hash code.HashCalcfeed(float value)Feeds afloatto the computed hash code.HashCalcfeed(float[] array)Feeds afloatarray to the computed hash code.HashCalcfeed(int value)Feeds aintto the computed hash code.HashCalcfeed(int[] array)Feeds aintarray to the computed hash code.HashCalcfeed(long value)Feeds along valueto the computed hash code.HashCalcfeed(long[] array)Feeds alongarray to the computed hash code.HashCalcfeed(short value)Feeds ashortto the computed hash code.HashCalcfeed(short[] array)Feeds ashortarray to the computed hash code.HashCalcfeed(Object object)Feeds anObjectto the computed hash code.HashCalcfeed(Object[] array)Feeds anObjectarray to the computed hash code.HashCalcfeedShallow(Object[] array)Feeds anObjectarray to the computed hash code.inthashCode()Returns the computed hash code.
-
-
-
Constructor Detail
-
HashCalc
public HashCalc(Class<?> forClass)
Constructs a newHashCalcfor 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 abooleanto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(char value)
Feeds acharto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(byte value)
Feeds abyteto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(short value)
Feeds ashortto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(int value)
Feeds aintto 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 valueto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(float value)
Feeds afloatto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(double value)
Feeds adoubleto the computed hash code.- Parameters:
value- value to be included in the hash code- Returns:
- this
HashCalc
-
feed
public HashCalc feed(Object object)
Feeds anObjectto 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 abooleanarray 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 abytearray 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 ashortarray 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 aintarray 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 alongarray 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 afloatarray 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 adoublearray 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 anObjectarray 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 someinstanceofs.- Parameters:
array- array to be included in the hash code- Returns:
- this
HashCalc
-
feedShallow
public HashCalc feedShallow(Object[] array)
Feeds anObjectarray 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.
-
-