Package org.apache.juneau.commons.lang
Class HashCode
java.lang.Object
org.apache.juneau.commons.lang.HashCode
Utility class for generating integer hash codes.
General usage:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(int i) Hashes the hashcode into this object.Hashes the hashcode of the specified object into this object.static final HashCodecreate()Create a new HashCode object.intget()Return the calculated hashcode value.static final intCalculates a hash code over the specified objects.protected ObjectConverts the object to a normalized form before grabbing it's hashcode.
-
Constructor Details
-
HashCode
public HashCode()
-
-
Method Details
-
create
Create a new HashCode object.- Returns:
- A new HashCode object.
-
of
Calculates a hash code over the specified objects.Uses the same algorithm as
Objects.hash(Object...)(31 * result + element hash).Special handling is provided for:
- Annotations: Uses
AnnotationUtils.hash(Annotation)to ensure consistent hashing according to theAnnotation.hashCode()contract. - Arrays: Uses content-based hashing via
Arrays.hashCode(Object[])instead of identity-based hashing. - Null values: Treated as 0 in the hash calculation.
- Parameters:
objects- The objects to calculate a hashcode over.- Returns:
- A numerical hashcode value.
- See Also:
- Annotations: Uses
-
add
Hashes the hashcode into this object.The formula is simply
hashCode = 31*hashCode + i; - Parameters:
i- The hashcode to hash into this object's hashcode.- Returns:
- This object.
-
add
Hashes the hashcode of the specified object into this object.The formula is
hashCode = 31*hashCode + elementHash; Special handling is provided for:
- Null values: Adds 0 to the hash code.
- Annotations: Uses
AnnotationUtils.hash(Annotation)to ensure consistent hashing according to theAnnotation.hashCode()contract. - Arrays: Uses content-based hashing via
Arrays.hashCode(Object[])instead of identity-based hashing. Supports all primitive array types and object arrays. - Other objects: Uses the object's
Object.hashCode()method.
- Parameters:
o- The object whose hashcode will be hashed with this object.- Returns:
- This object.
- See Also:
-
get
Return the calculated hashcode value.- Returns:
- The calculated hashcode.
-
unswap
Converts the object to a normalized form before grabbing it's hashcode.Subclasses can override this method to provide specialized handling (e.g. converting numbers to strings so that
123 and"123" end up creating the same hashcode.)Default implementation does nothing.
- Parameters:
o- The object to normalize before getting it's hashcode.- Returns:
- The normalized object.
-