Class Pair<L,R>

java.lang.Object
org.daiitech.naftah.builtin.utils.tuple.Pair<L,R>
Type Parameters:
L - the left element type
R - the right element type
All Implemented Interfaces:
Serializable, Comparable<Pair<L,R>>, Map.Entry<L,R>, NTuple
Direct Known Subclasses:
ImmutablePair, MutablePair

public abstract sealed class Pair<L,R> extends Object implements NTuple, Map.Entry<L,R>, Comparable<Pair<L,R>>, Serializable permits ImmutablePair<L,R>, MutablePair<L,R>
A pair consisting of two elements.

This class is an abstract implementation defining the basic API. It refers to the elements as 'left' and 'right'. It also implements the Map.Entry interface where the key is 'left' and the value is 'right'.

Subclass implementations may be mutable or immutable. However, there is no restriction on the type of the stored objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

Author:
Chakib Daii
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of elements contained in this tuple.
    int
    compareTo(Pair<L,R> other)
    Compares the pair based on the left element followed by the right element.
    boolean
    Checks if the tuple contains the specified element.
    boolean
    Compares this pair to another based on the two elements.
    get(int index)
    Returns the element at the specified zero-based position.
    final L
    Gets the key from this pair.
    abstract L
    Gets the left element from this pair.
    abstract R
    Gets the right element from this pair.
    Gets the value from this pair.
    int
    Returns a suitable hash code.
    static <L, R> Pair<L,R>
    of(Map.Entry<L,R> pair)
    Creates an immutable pair from a map entry.
    static <L, R> Pair<L,R>
    of(L left, R right)
    Creates an immutable pair of two objects inferring the generic types.
    static <L, R> Pair<L,R>
    ofNonNull(L left, R right)
    Creates an immutable pair of two non-null objects inferring the generic types.
    Returns an array containing all elements of the tuple.
    Returns a String representation of this pair using the format ($left,$right).
    toString(String format)
    Formats the receiver using the given format.

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface java.util.Map.Entry

    setValue
  • Field Details

  • Constructor Details

    • Pair

      public Pair()
      Constructs a new instance.
  • Method Details

    • of

      public static <L, R> Pair<L,R> of(L left, R right)
      Creates an immutable pair of two objects inferring the generic types.

      This factory allows the pair to be created using inference to obtain the generic types.

      Type Parameters:
      L - the left element type
      R - the right element type
      Parameters:
      left - the left element, may be null
      right - the right element, may be null
      Returns:
      a pair formed from the two parameters, not null
    • of

      public static <L, R> Pair<L,R> of(Map.Entry<L,R> pair)
      Creates an immutable pair from a map entry.

      This factory allows the pair to be created using inference to obtain the generic types.

      Type Parameters:
      L - the left element type
      R - the right element type
      Parameters:
      pair - the map entry.
      Returns:
      a pair formed from the map entry
    • ofNonNull

      public static <L, R> Pair<L,R> ofNonNull(L left, R right)
      Creates an immutable pair of two non-null objects inferring the generic types.

      This factory allows the pair to be created using inference to obtain the generic types.

      Type Parameters:
      L - the left element type
      R - the right element type
      Parameters:
      left - the left element, may not be null
      right - the right element, may not be null
      Returns:
      a pair formed from the two parameters, not null
      Throws:
      NullPointerException - if any input is null
    • compareTo

      public int compareTo(Pair<L,R> other)
      Compares the pair based on the left element followed by the right element. The types must be Comparable.
      Specified by:
      compareTo in interface Comparable<L>
      Parameters:
      other - the other pair, not null
      Returns:
      negative if this is less, zero if equal, positive if greater
    • equals

      public boolean equals(Object obj)
      Compares this pair to another based on the two elements.
      Specified by:
      equals in interface Map.Entry<L,R>
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare to, null returns false
      Returns:
      true if the elements of the pair are equal
    • getKey

      public final L getKey()
      Gets the key from this pair.

      This method implements the Map.Entry interface returning the left element as the key.

      Specified by:
      getKey in interface Map.Entry<L,R>
      Returns:
      the left element as the key, may be null
    • getLeft

      public abstract L getLeft()
      Gets the left element from this pair.

      When treated as a key-value pair, this is the key.

      Returns:
      the left element, may be null
    • getRight

      public abstract R getRight()
      Gets the right element from this pair.

      When treated as a key-value pair, this is the value.

      Returns:
      the right element, may be null
    • getValue

      public R getValue()
      Gets the value from this pair.

      This method implements the Map.Entry interface returning the right element as the value.

      Specified by:
      getValue in interface Map.Entry<L,R>
      Returns:
      the right element as the value, may be null
    • hashCode

      public int hashCode()
      Returns a suitable hash code. The hash code follows the definition in Map.Entry.
      Specified by:
      hashCode in interface Map.Entry<L,R>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • toString

      public String toString()
      Returns a String representation of this pair using the format ($left,$right).
      Overrides:
      toString in class Object
      Returns:
      a string describing this object, not null
    • toString

      public String toString(String format)
      Formats the receiver using the given format.

      This uses Formattable to perform the formatting. Two variables may be used to embed the left and right elements. Use %1$s for the left element (key) and %2$s for the right element (value). The default format used by toString() is (%1$s,%2$s).

      Parameters:
      format - the format string, optionally containing %1$s and %2$s, not null
      Returns:
      the formatted string, not null
    • arity

      public int arity()
      Returns the number of elements contained in this tuple.
      Specified by:
      arity in interface NTuple
      Returns:
      the arity (size) of this tuple, always >= 0
    • get

      public Object get(int index)
      Returns the element at the specified zero-based position.
      Specified by:
      get in interface NTuple
      Parameters:
      index - the element position, from 0 (inclusive) to arity() (exclusive)
      Returns:
      the element at the given position
    • toArray

      public Object[] toArray()
      Returns an array containing all elements of the tuple.
      Specified by:
      toArray in interface NTuple
      Returns:
      an array of tuple elements
    • contains

      public boolean contains(Object o)
      Checks if the tuple contains the specified element.
      Specified by:
      contains in interface NTuple
      Parameters:
      o - the element to check for
      Returns:
      true if the tuple contains the element, false otherwise