- All Implemented Interfaces:
Comparable<HashKey>
This class provides a simple way of creating HashMap keys combining several objects e.g. Strings, DateTimes etc.
This avoids the need to implement and maintain another class with hashCode and equals methods for the composite key. Each object used in the composite key needs to independently implement hashCode and equals. Common Java classes can generally be relied on to meet this requirement.
The key includes optional names. If the key is converted to JSON, named fields in the key will be included in the output with corresponding element names. Fields without names will not be included in generated JSON.
Names must match for keys to be considered equal.
Individual entries in the key can be null.
This class implements Comparable
for simple sorting of output.
The rules used for compareTo(HashKey)
are:
- Both names and values are compared, in the order they were added to the key.
- If values are the same type and implement
Comparable
, they are compared usingComparable.compareTo(Object)
. - Otherwise, the results of
Object.toString()
are compared.
This comparison is not necessarily high performance but should be adequate for sorting thousands to tens of thousands of values e.g. formatting output. If you require a higher performance comparison to sort large quantities of data you may need to create your own class to use as a key.
-
Method Summary
Modifier and TypeMethodDescriptionInclude an additional object in the keyInclude an additional named object in the keyint
boolean
Compare this HashKey with the specified object.Get an object from the entry by name (only for entries added with a name)Get named values from the key (used when converting to JSON)int
hashCode()
Generate a hash code for this keystatic HashKey
Create a new HashKey with an object as the first memberstatic HashKey
Create a new HashKey with a named object as the first membertoString()
-
Method Details
-
of
Create a new HashKey with an object as the first member- Parameters:
value
- the value to include in the key- Returns:
- a new HashKey
-
of
Create a new HashKey with a named object as the first member- Parameters:
name
- the name to be used when generating JSONvalue
- the value to include in the key- Returns:
- a new HashKey
-
and
Include an additional object in the key- Parameters:
value
- the object to include in the key- Returns:
- this HashKey, to allow method chaining
-
and
Include an additional named object in the key- Parameters:
name
- the element name to be used when generating JSONvalue
- the object to include in the key- Returns:
- this HashKey, to allow method chaining
-
hashCode
public int hashCode()Generate a hash code for this key -
equals
Compare this HashKey with the specified object. Returns true if the object is a HashKey and the sequence of names and values in this key compare as equal to the sequence of names and values in the other key. If an entry in this HashKey sequence is null, the entry in the other HashKey sequence must also be null. -
getEntry
Get an object from the entry by name (only for entries added with a name)- Parameters:
name
- the name of the entry- Returns:
- the object or null
-
getNamedValues
Get named values from the key (used when converting to JSON)- Returns:
- a Map of names and objects
-
toString
-
compareTo
- Specified by:
compareTo
in interfaceComparable<HashKey>
-