Class Tuple
java.lang.Object
org.daiitech.naftah.builtin.utils.tuple.Tuple
- All Implemented Interfaces:
Serializable,Iterable<Object>,Collection<Object>,List<Object>,NTuple
An immutable tuple implementation that implements
List<Object>.
This class wraps an unmodifiable list of objects and provides
factory methods to create tuples from arrays or lists.
All mutating operations throw UnsupportedOperationException.
The string representation includes the Arabic word "تركيبة" meaning "Tuple".
- Author:
- Chakib Daii
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longThe underlying unmodifiable list of tuple elements. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidNot supported.booleanNot supported.booleanaddAll(int index, Collection<?> c) Not supported.booleanaddAll(Collection<?> coll) Not supported.intarity()Returns the number of elements contained in this tuple.voidclear()Not supported.booleanChecks if the tuple contains the specified element.booleancontainsAll(Collection<?> c) Always returns false.booleanChecks equality with another object.voidPerforms the given action for each element of the tuple.get(int index) Returns the element at the specified position in this tuple.inthashCode()Returns the hash code value for this tuple, delegated to the underlying list's hash code.intReturns the index of the first occurrence of the specified element, or -1 if not found.booleanisEmpty()Checks if the tuple contains no elements.iterator()Returns an iterator over the elements in this tuple.intReturns the index of the last occurrence of the specified element, or -1 if not found.Returns a list iterator over the elements in this tuple.listIterator(int index) Returns a list iterator of the elements in this tuple, starting at the specified position.static TupleCreates a tuple from a variable number of elements.static TupleCreates a tuple from a list of elements.Returns a possibly parallelStreamwith the tuple elements.remove(int index) Not supported.booleanNot supported.booleanremoveAll(Collection<?> coll) Not supported.voidreplaceAll(UnaryOperator<Object> operator) Not supported.booleanretainAll(Collection<?> coll) Not supported.Not supported.intsize()Returns the number of elements in this tuple.Creates aSpliteratorover the elements in this tuple.stream()Returns a sequentialStreamwith the tuple elements.subList(int fromIndex, int toIndex) Returns a view of the portion of this tuple between the specified indices.Object[]toArray()Returns an array containing all elements of the tuple.<T> T[]toArray(IntFunction<T[]> f) Returns an array containing all elements of the tuple, using the provided array generator function.<T> T[]toArray(T[] a) Returns an array containing all elements of the tuple, using the provided array if it is large enough.toString()Returns a string representation of the tuple.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
removeIf
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
values
The underlying unmodifiable list of tuple elements.
-
-
Constructor Details
-
Tuple
Private constructor to create a tuple from the given elements.The elements are stored in an immutable list using
List.of(Object...). Once created, the tuple cannot be modified: no elements can be added, removed, or replaced. This ensures that the tuple is fully immutable.- Parameters:
values- the elements to be included in the tuple- Throws:
NullPointerException- ifvaluesis null or contains null elements
-
-
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
-
arity
public int arity()Returns the number of elements contained in this tuple. -
size
public int size()Returns the number of elements in this tuple. -
isEmpty
public boolean isEmpty()Checks if the tuple contains no elements. -
contains
Checks if the tuple contains the specified element. -
toArray
Returns an array containing all elements of the tuple. -
toArray
public <T> T[] toArray(T[] a) Returns an array containing all elements of the tuple, using the provided array if it is large enough. -
toArray
Returns an array containing all elements of the tuple, using the provided array generator function.- Specified by:
toArrayin interfaceCollection<Object>- Type Parameters:
T- the type of the array elements- Parameters:
f- the array generator function- Returns:
- an array containing the elements of the tuple
-
toString
Returns a string representation of the tuple. The list's string as a tuple -
iterator
Returns an iterator over the elements in this tuple. -
forEach
Performs the given action for each element of the tuple. -
spliterator
Creates aSpliteratorover the elements in this tuple.- Specified by:
spliteratorin interfaceCollection<Object>- Specified by:
spliteratorin interfaceIterable<Object>- Specified by:
spliteratorin interfaceList<Object>- Returns:
- a spliterator over the tuple elements
-
stream
Returns a sequentialStreamwith the tuple elements.- Specified by:
streamin interfaceCollection<Object>- Returns:
- a sequential stream over the tuple elements
-
parallelStream
Returns a possibly parallelStreamwith the tuple elements.- Specified by:
parallelStreamin interfaceCollection<Object>- Returns:
- a parallel stream over the tuple elements
-
equals
Checks equality with another object. Returns true if the other object is the same instance or if the underlying list equals the other object. -
hashCode
public int hashCode()Returns the hash code value for this tuple, delegated to the underlying list's hash code. -
get
Returns the element at the specified position in this tuple. -
indexOf
Returns the index of the first occurrence of the specified element, or -1 if not found. -
lastIndexOf
Returns the index of the last occurrence of the specified element, or -1 if not found.- Specified by:
lastIndexOfin interfaceList<Object>- Parameters:
o- the element to search for- Returns:
- the index of the last occurrence, or -1 if not found
-
listIterator
Returns a list iterator over the elements in this tuple.- Specified by:
listIteratorin interfaceList<Object>- Returns:
- a list iterator over the tuple elements
-
listIterator
Returns a list iterator of the elements in this tuple, starting at the specified position.- Specified by:
listIteratorin interfaceList<Object>- Parameters:
index- the index to start the iterator at- Returns:
- a list iterator over the tuple elements starting at
index
-
subList
Returns a view of the portion of this tuple between the specified indices. The returned view is wrapped in a newTuple. -
add
Not supported. Always throwsUnsupportedOperationException.- Specified by:
addin interfaceCollection<Object>- Specified by:
addin interfaceList<Object>- Parameters:
e- element to be added- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
remove
Not supported. Always throwsUnsupportedOperationException.- Specified by:
removein interfaceCollection<Object>- Specified by:
removein interfaceList<Object>- Parameters:
o- element to be removed- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
containsAll
Always returns false. (Note: This deviates from usual List behavior.)- Specified by:
containsAllin interfaceCollection<Object>- Specified by:
containsAllin interfaceList<Object>- Parameters:
c- the collection to check for containment- Returns:
- false always
-
addAll
Not supported. Always throwsUnsupportedOperationException.- Specified by:
addAllin interfaceCollection<Object>- Specified by:
addAllin interfaceList<Object>- Parameters:
coll- collection containing elements to be added- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
removeAll
Not supported. Always throwsUnsupportedOperationException.- Specified by:
removeAllin interfaceCollection<Object>- Specified by:
removeAllin interfaceList<Object>- Parameters:
coll- collection containing elements to be removed- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
retainAll
Not supported. Always throwsUnsupportedOperationException.- Specified by:
retainAllin interfaceCollection<Object>- Specified by:
retainAllin interfaceList<Object>- Parameters:
coll- collection containing elements to be retained- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
clear
public void clear()Not supported. Always throwsUnsupportedOperationException.- Specified by:
clearin interfaceCollection<Object>- Specified by:
clearin interfaceList<Object>- Throws:
UnsupportedOperationException- always thrown
-
set
Not supported. Always throwsUnsupportedOperationException.- Specified by:
setin interfaceList<Object>- Parameters:
index- index of element to replaceelement- element to be stored- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
add
Not supported. Always throwsUnsupportedOperationException.- Specified by:
addin interfaceList<Object>- Parameters:
index- index at which the specified element is to be insertedelement- element to be inserted- Throws:
UnsupportedOperationException- always thrown
-
remove
Not supported. Always throwsUnsupportedOperationException.- Specified by:
removein interfaceList<Object>- Parameters:
index- index of the element to be removed- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
addAll
Not supported. Always throwsUnsupportedOperationException.- Specified by:
addAllin interfaceList<Object>- Parameters:
index- index at which to insert elementsc- collection containing elements to be added- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always thrown
-
replaceAll
Not supported. Always throwsUnsupportedOperationException.- Specified by:
replaceAllin interfaceList<Object>- Parameters:
operator- the operator to apply to each element- Throws:
UnsupportedOperationException- always thrown
-