Package jsonvalues

Class Key

java.lang.Object
jsonvalues.Key
All Implemented Interfaces:
Comparable<Position>, Position

public final class Key extends Object implements Position
Represents the key of a value in a JSON object.
  • Field Details

    • name

      public final String name
      The name of the key.
  • Method Details

    • of

      public static Key of(String key)
      Returns a new instance representing a key with the given name in a JSON object. Any string, except null, can represent a key in a JSON object, even the empty string.
      Parameters:
      key - the given name of the key
      Returns:
      a new Key object
    • compareTo

      public int compareTo(Position o)
      Compares this key with another given position. If the given position is a key, both are compared lexicographically. If it's an index, both are compared numerically.
      Specified by:
      compareTo in interface Comparable<Position>
      Parameters:
      o - the given position
      Returns:
      0 if they are equal, +1 if this is greater, -1 otherwise
    • asKey

      public Key asKey()
      Returns this key.
      Specified by:
      asKey in interface Position
      Returns:
      this object
    • asIndex

      public Index asIndex()
      Throws a UserError exception.
      Specified by:
      asIndex in interface Position
      Returns:
      this position as an Index
      Throws:
      UserError - a Key can't be cast into an Index
    • isIndex

      public boolean isIndex()
      Returns false.
      Specified by:
      isIndex in interface Position
      Returns:
      false
    • isKey

      public boolean isKey()
      Returns true.
      Specified by:
      isKey in interface Position
      Returns:
      true
    • hashCode

      public int hashCode()
      Returns the hashcode of this key.
      Overrides:
      hashCode in class Object
      Returns:
      the hashcode of the name.
    • equals

      public boolean equals(Object that)
      Returns true if that is a key and both have the same name.
      Overrides:
      equals in class Object
      Parameters:
      that - the other object
      Returns:
      true if both objects are Keys with the same name
    • toString

      public String toString()
      Returns the name of the key.
      Overrides:
      toString in class Object
      Returns:
      the name of the key