Package jsonvalues

Class Index

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

public final class Index extends Object implements Position
Represents the index of a value in a JSON array.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int
    The index number.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns this index.
    Throws a UserError exception because an Index cannot be cast into a Key.
    int
    Compares this index with another given position.
    boolean
    equals(Object that)
    Returns true if that is an index and both have the same value.
    int
    Returns the hashcode of this index.
    boolean
    Returns true.
    boolean
    Returns false.
    static Index
    of(int index)
    Creates a new instance of the Index class representing the given index.
    Returns the value of the index as a string.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface jsonvalues.Position

    isIndex, isKey, match
  • Field Details

    • n

      public final int n
      The index number.
  • Method Details

    • of

      public static Index of(int index)
      Creates a new instance of the Index class representing the given index.
      Parameters:
      index - the index to represent
      Returns:
      an Index object
      Throws:
      IndexOutOfBoundsException - if the index is less than -1
    • compareTo

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

      public Key asKey()
      Throws a UserError exception because an Index cannot be cast into a Key.
      Specified by:
      asKey in interface Position
      Returns:
      never returns, throws UserError
      Throws:
      UserError - always throws a UserError
    • asIndex

      public Index asIndex()
      Returns this index.
      Specified by:
      asIndex in interface Position
      Returns:
      this Index object
    • isIndex

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

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

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

      public boolean equals(Object that)
      Returns true if that is an index and both have the same value.
      Overrides:
      equals in class Object
      Parameters:
      that - other object
      Returns:
      true if both objects are indexes representing the same position
    • toString

      public String toString()
      Returns the value of the index as a string.
      Overrides:
      toString in class Object
      Returns:
      the value of the index as a string