Interface NTuple

All Known Implementing Classes:
ImmutablePair, ImmutableTriple, MutablePair, MutableTriple, Pair, Triple, Tuple

public sealed interface NTuple permits Pair<L,R>, Triple<L,M,R>, Tuple
A common contract for all tuple-like data structures with a fixed number of ordered components. Implementations represent heterogeneous aggregates of values, where each element is accessed by its positional index.

This sealed interface is the shared supertype of:

  • Pair — a tuple of arity 2
  • Triple — a tuple of arity 3
  • Tuple — a general-purpose n-tuple backed by a List

The arity of a tuple is the number of elements it contains. Elements are accessed by zero-based index using get(int). Implementations must throw an IndexOutOfBoundsException when an index is outside the bounds [0, arity()).

This interface is conceptually similar to:

  • Scala's Product type hierarchy
  • .NET's System.Runtime.CompilerServices.ITuple
  • Haskell and Python tuple types
Author:
Chakib Daii
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of elements contained in this tuple.
    boolean
    Checks if the tuple contains the specified element.
    get(int index)
    Returns the element at the specified zero-based position.
    Creates a new NaftahBugError indicating that null values are not allowed.
    newNaftahBugNullError(int line, int column)
    Creates a new NaftahBugError indicating that null values are not allowed, with the specified line and column.
    static NTuple
    of(Object... elements)
    Creates a tuple from a variable number of elements.
    static NTuple
    of(List<?> elements)
    Creates a tuple from a list of elements.
    Returns an array containing all elements of the tuple.
  • Method Details

    • of

      static NTuple of(Object... elements)
      Creates a tuple from a variable number of elements.
      Parameters:
      elements - the elements to be included in the tuple
      Returns:
      a new Tuple containing the given elements
      Throws:
      NaftahBugError - if the input array is null
    • of

      static NTuple of(List<?> elements)
      Creates a tuple from a list of elements.
      Parameters:
      elements - the list of elements to be included in the tuple
      Returns:
      a new Tuple containing the given list elements
      Throws:
      NaftahBugError - if the input list is null
    • newNaftahBugNullError

      static NaftahBugError newNaftahBugNullError()
      Creates a new NaftahBugError indicating that null values are not allowed. This convenience method uses default line and column values (-1).
      Returns:
      a new NaftahBugError instance with an Arabic error message
    • newNaftahBugNullError

      static NaftahBugError newNaftahBugNullError(int line, int column)
      Creates a new NaftahBugError indicating that null values are not allowed, with the specified line and column.
      Parameters:
      line - the line number where the error occurred, or -1 if unknown
      column - the column number where the error occurred, or -1 if unknown
      Returns:
      a new NaftahBugError instance with an Arabic error message and location
    • arity

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

      Object get(int index)
      Returns the element at the specified zero-based position.
      Parameters:
      index - the element position, from 0 (inclusive) to arity() (exclusive)
      Returns:
      the element at the given position
      Throws:
      IndexOutOfBoundsException - if index is outside the tuple's bounds
    • toArray

      Object[] toArray()
      Returns an array containing all elements of the tuple.
      Returns:
      an array of tuple elements
    • contains

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