java.lang.Object
com.blackhillsoftware.json.util.HashKey
All Implemented Interfaces:
Comparable<HashKey>

public class HashKey extends Object implements Comparable<HashKey>
Build HashMap keys from multiple objects.

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:

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 Details

    • of

      public static HashKey of(Object value)
      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

      public static HashKey of(String name, Object value)
      Create a new HashKey with a named object as the first member
      Parameters:
      name - the name to be used when generating JSON
      value - the value to include in the key
      Returns:
      a new HashKey
    • and

      public HashKey and(Object value)
      Include an additional object in the key
      Parameters:
      value - the object to include in the key
      Returns:
      this HashKey, to allow method chaining
    • and

      public HashKey and(String name, Object value)
      Include an additional named object in the key
      Parameters:
      name - the element name to be used when generating JSON
      value - 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
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      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.
      Overrides:
      equals in class Object
    • getEntry

      public Object getEntry(String name)
      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

      public Map<String,Object> getNamedValues()
      Get named values from the key (used when converting to JSON)
      Returns:
      a Map of names and objects
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(HashKey other)
      Specified by:
      compareTo in interface Comparable<HashKey>