Class HashCode
java.lang.Object
com.itextpdf.awt.geom.misc.HashCode
This class is a convenience method to sequentially calculate hash code of the
object based on the field values. The result depends on the order of elements
appended. The exact formula is the same as for
java.util.List.hashCode.
If you need order independent hash code just summate, multiply or XOR all
elements.
Suppose we have class:
class Thing {
long id;
String name;
float weight;
}
The hash code calculation can be expressed in 2 forms.
For maximum performance:
public int hashCode() {
int hashCode = HashCode.EMPTY_HASH_CODE;
hashCode = HashCode.combine(hashCode, id);
hashCode = HashCode.combine(hashCode, name);
hashCode = HashCode.combine(hashCode, weight);
return hashCode;
}
For convenience:
public int hashCode() {
return new HashCode().append(id).append(name).append(weight).hashCode();
}
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe hashCode value before any data is appended, equals to 1.private int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal HashCodeappend(boolean value) Appends value's hashCode to the current hashCode.final HashCodeappend(double value) Appends value's hashCode to the current hashCode.final HashCodeappend(float value) Appends value's hashCode to the current hashCode.final HashCodeappend(int value) Appends value's hashCode to the current hashCode.final HashCodeappend(long value) Appends value's hashCode to the current hashCode.final HashCodeAppends value's hashCode to the current hashCode.static intcombine(int hashCode, boolean value) Combines hashCode of previous elements sequence and value's hashCode.static intcombine(int hashCode, double value) Combines hashCode of previous elements sequence and value's hashCode.static intcombine(int hashCode, float value) Combines hashCode of previous elements sequence and value's hashCode.static intcombine(int hashCode, int value) Combines hashCode of previous elements sequence and value's hashCode.static intcombine(int hashCode, long value) Combines hashCode of previous elements sequence and value's hashCode.static intCombines hashCode of previous elements sequence and value's hashCode.final inthashCode()Returns accumulated hashCode
-
Field Details
-
EMPTY_HASH_CODE
public static final int EMPTY_HASH_CODEThe hashCode value before any data is appended, equals to 1.- See Also:
-
hashCode
private int hashCode
-
-
Constructor Details
-
HashCode
public HashCode()
-
-
Method Details
-
hashCode
-
combine
public static int combine(int hashCode, boolean value) Combines hashCode of previous elements sequence and value's hashCode.- Parameters:
hashCode- previous hashCode valuevalue- new element- Returns:
- combined hashCode
-
combine
public static int combine(int hashCode, long value) Combines hashCode of previous elements sequence and value's hashCode.- Parameters:
hashCode- previous hashCode valuevalue- new element- Returns:
- combined hashCode
-
combine
public static int combine(int hashCode, float value) Combines hashCode of previous elements sequence and value's hashCode.- Parameters:
hashCode- previous hashCode valuevalue- new element- Returns:
- combined hashCode
-
combine
public static int combine(int hashCode, double value) Combines hashCode of previous elements sequence and value's hashCode.- Parameters:
hashCode- previous hashCode valuevalue- new element- Returns:
- combined hashCode
-
combine
Combines hashCode of previous elements sequence and value's hashCode.- Parameters:
hashCode- previous hashCode valuevalue- new element- Returns:
- combined hashCode
-
combine
public static int combine(int hashCode, int value) Combines hashCode of previous elements sequence and value's hashCode.- Parameters:
hashCode- previous hashCode valuevalue- new element- Returns:
- combined hashCode
-
append
Appends value's hashCode to the current hashCode.- Parameters:
value- new element- Returns:
- this
-
append
Appends value's hashCode to the current hashCode.- Parameters:
value- new element- Returns:
- this
-
append
Appends value's hashCode to the current hashCode.- Parameters:
value- new element- Returns:
- this
-
append
Appends value's hashCode to the current hashCode.- Parameters:
value- new element- Returns:
- this
-
append
Appends value's hashCode to the current hashCode.- Parameters:
value- new element- Returns:
- this
-
append
-