Interface NTuple
- All Known Implementing Classes:
ImmutablePair,ImmutableTriple,MutablePair,MutableTriple,Pair,Triple,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 2Triple— a tuple of arity 3Tuple— a general-purpose n-tuple backed by aList
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
Producttype hierarchy - .NET's
System.Runtime.CompilerServices.ITuple - Haskell and Python tuple types
-
Method Summary
Modifier and TypeMethodDescriptionintarity()Returns the number of elements contained in this tuple.booleanChecks if the tuple contains the specified element.get(int index) Returns the element at the specified zero-based position.static NaftahBugErrorCreates a newNaftahBugErrorindicating that null values are not allowed.static NaftahBugErrornewNaftahBugNullError(int line, int column) Creates a newNaftahBugErrorindicating that null values are not allowed, with the specified line and column.static NTupleCreates a tuple from a variable number of elements.static NTupleCreates a tuple from a list of elements.Object[]toArray()Returns an array containing all elements of the tuple.
-
Method Details
-
of
Creates a tuple from a variable number of elements.- Parameters:
elements- the elements to be included in the tuple- Returns:
- a new
Tuplecontaining the given elements - Throws:
NaftahBugError- if the input array is null
-
of
Creates a tuple from a list of elements.- Parameters:
elements- the list of elements to be included in the tuple- Returns:
- a new
Tuplecontaining the given list elements - Throws:
NaftahBugError- if the input list is null
-
newNaftahBugNullError
Creates a newNaftahBugErrorindicating that null values are not allowed. This convenience method uses default line and column values (-1).- Returns:
- a new
NaftahBugErrorinstance with an Arabic error message
-
newNaftahBugNullError
Creates a newNaftahBugErrorindicating that null values are not allowed, with the specified line and column.- Parameters:
line- the line number where the error occurred, or -1 if unknowncolumn- the column number where the error occurred, or -1 if unknown- Returns:
- a new
NaftahBugErrorinstance 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
Returns the element at the specified zero-based position.- Parameters:
index- the element position, from0(inclusive) toarity()(exclusive)- Returns:
- the element at the given position
- Throws:
IndexOutOfBoundsException- ifindexis outside the tuple's bounds
-
toArray
Object[] toArray()Returns an array containing all elements of the tuple.- Returns:
- an array of tuple elements
-
contains
Checks if the tuple contains the specified element.- Parameters:
o- the element to check for- Returns:
trueif the tuple contains the element,falseotherwise
-