Class Triple<L,M,R>

java.lang.Object
org.daiitech.naftah.builtin.utils.tuple.Triple<L,M,R>
Type Parameters:
L - the left element type
M - the middle element type
R - the right element type
All Implemented Interfaces:
Serializable, Comparable<Triple<L,M,R>>, NTuple
Direct Known Subclasses:
ImmutableTriple, MutableTriple

public abstract sealed class Triple<L,M,R> extends Object implements NTuple, Comparable<Triple<L,M,R>>, Serializable permits ImmutableTriple<L,M,R>, MutableTriple<L,M,R>
A triple consisting of three elements.

This class is an abstract implementation defining the basic API. It refers to the elements as 'left', 'middle' and '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 triple, then the triple 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(Triple<L,M,R> other)
    Compares the triple based on the left element, followed by the middle element, finally the right element.
    boolean
    Checks if the tuple contains the specified element.
    boolean
    Compares this triple to another based on the three elements.
    get(int index)
    Returns the element at the specified zero-based position.
    abstract L
    Gets the left element from this triple.
    abstract M
    Gets the middle element from this triple.
    abstract R
    Gets the right element from this triple.
    int
    Returns a suitable hash code.
    static <L, M, R> Triple<L,M,R>
    of(L left, M middle, R right)
    Obtains an immutable triple of three objects inferring the generic types.
    static <L, M, R> Triple<L,M,R>
    ofNonNull(L left, M middle, R right)
    Obtains an immutable triple of three non-null objects inferring the generic types.
    Returns an array containing all elements of the tuple.
    Returns a String representation of this triple using the format ($left,$middle,$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
  • Field Details

  • Constructor Details

    • Triple

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

    • of

      public static <L, M, R> Triple<L,M,R> of(L left, M middle, R right)
      Obtains an immutable triple of three objects inferring the generic types.

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

      Type Parameters:
      L - the left element type
      M - the middle element type
      R - the right element type
      Parameters:
      left - the left element, may be null
      middle - the middle element, may be null
      right - the right element, may be null
      Returns:
      a triple formed from the three parameters, not null
    • ofNonNull

      public static <L, M, R> Triple<L,M,R> ofNonNull(L left, M middle, R right)
      Obtains an immutable triple of three non-null objects inferring the generic types.

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

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

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

      public boolean equals(Object obj)
      Compares this triple to another based on the three elements.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare to, null returns false
      Returns:
      true if the elements of the triple are equal
    • getLeft

      public abstract L getLeft()
      Gets the left element from this triple.
      Returns:
      the left element, may be null
    • getMiddle

      public abstract M getMiddle()
      Gets the middle element from this triple.
      Returns:
      the middle element, may be null
    • getRight

      public abstract R getRight()
      Gets the right element from this triple.
      Returns:
      the right element, may be null
    • hashCode

      public int hashCode()
      Returns a suitable hash code.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • toString

      public String toString()
      Returns a String representation of this triple using the format ($left,$middle,$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. Three variables may be used to embed the left and right elements. Use %1$s for the left element, %2$s for the middle and %3$s for the right element. The default format used by toString() is (%1$s,%2$s,%3$s).

      Parameters:
      format - the format string, optionally containing %1$s, %2$s and %3$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