Package Torello.Java.ReadOnly
Interface ReadOnlyList<E>
-
- Type Parameters:
E
- the type of elements in this list
- All Superinterfaces:
java.lang.Iterable<E>
,ReadOnlyCollection<E>
- All Known Implementing Classes:
ReadOnlyArrayList
,ReadOnlyVector
public interface ReadOnlyList<E> extends ReadOnlyCollection<E>
This interface was originally copied fromGitHub's Open-JDK
Account. Though the original file has been modified, few changes have been applied to the Javadoc Commenting. Due to fact that that is a Javainterface
file, there were few method bodies with Source-Code to begin with - meaning this is largely a copy of Method-Signatures and Java-Doc Comments.
Method and parameter names & types have not been modified whatsoever; but several methods had to be eliminated for not being Read-Only. This Source-File was copied from the original Open JDK-21 file of the same (or, rather, highly similar) Interface-Name. The original file may be viewed on theJDK-21 GitHub
public (and, coincidentally, Read-Only) Source-Release archive for Java Packagejava.util.*
The Original'.java'
Source-File's Header-Copyright Information is included here:File Copyright
. Within that Copyright Notice, it is suggested that a copy of theGNU Public License V2
also be included alongside.Immutable variant of Java Collections Framework interfacejava.util.List<E>
. This interface contains all of the methods that the standard Java interface ReadOnlyList contains - except those which would directly or indirectly modify / mutate the internal data-structure.
Hi-Lited Source-Code:- View Here: Torello/Java/ReadOnly/ReadOnlyList.java
- Open New Browser-Tab: Torello/Java/ReadOnly/ReadOnlyList.java
File Size: 26,751 Bytes Line Count: 608 '\n' Characters Found
-
-
Method Summary
Primary List Methods Modifier and Type Method E
get(int index)
ReadOnlyList<E>
subList(int fromIndex, int toIndex)
Retrieve a List Element's Index Modifier and Type Method int
indexOf(Object o)
int
lastIndexOf(Object o)
Check this List's Contents Modifier and Type Method boolean
contains(Object o)
boolean
containsAll(Collection<?> c)
boolean
isEmpty()
int
size()
Convert this List to an Array Modifier and Type Method Object[]
toArray()
<T> T[]
toArray(T[] a)
Static Methods: Builder's Modifier and Type Method static <E> ReadOnlyList<E>
copyOf(ReadOnlyCollection<? extends E> coll)
static <E> ReadOnlyList<E>
of()
static <E> ReadOnlyList<E>
of(E e1)
static <E> ReadOnlyList<E>
of(E... elements)
static <E> ReadOnlyList<E>
of(E e1, E e2)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4, E e5)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4, E e5, E e6)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
static <E> ReadOnlyList<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
Default Methods (Interface Methods with a Concrete, Default Implementation) Modifier and Type Method default Spliterator<E>
spliterator()
Methods: class java.lang.Object Modifier and Type Method boolean
equals(Object o)
int
hashCode()
Methods: interface java.lang.Iterable Modifier and Type Method RemoveUnsupportedIterator<E>
iterator()
ReadOnlyListIterator<E>
listIterator()
ReadOnlyListIterator<E>
listIterator(int index)
-
Methods inherited from interface Torello.Java.ReadOnly.ReadOnlyCollection
containsAND, containsAND, containsNAND, containsNAND, containsOR, containsOR, containsXOR, containsXOR, copyIntoCollection, parallelStream, stream, toArray
-
-
-
-
Method Detail
-
size
int size()
Returns the number of elements in this list. If this list contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.- Specified by:
size
in interfaceReadOnlyCollection<E>
- Returns:
- the number of elements in this list
-
isEmpty
boolean isEmpty()
ReturnsTRUE
if this list contains no elements.- Specified by:
isEmpty
in interfaceReadOnlyCollection<E>
- Returns:
TRUE
if this list contains no elements
-
contains
boolean contains(java.lang.Object o)
ReturnsTRUE
if this list contains the specified element. More formally, returnsTRUE
if and only if this list contains at least one elemente
such thatObjects.equals(o, e)
.- Specified by:
contains
in interfaceReadOnlyCollection<E>
- Parameters:
o
- element whose presence in this list is to be tested- Returns:
TRUE
if this list contains the specified element- Throws:
java.lang.ClassCastException
- if the type of the specified element is incompatible with this list (optional)java.lang.NullPointerException
- if the specified element is null and this list does not permit null elements (optional)
-
iterator
RemoveUnsupportedIterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.- Specified by:
iterator
in interfacejava.lang.Iterable<E>
- Specified by:
iterator
in interfaceReadOnlyCollection<E>
- Returns:
- an iterator over the elements in this list in proper sequence
-
toArray
java.lang.Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.- Specified by:
toArray
in interfaceReadOnlyCollection<E>
- Returns:
- an array containing all of the elements in this list in proper sequence
-
toArray
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set tonull
. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like thetoArray()
method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Supposex
is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array ofString
:
String[] y = x.toArray(new String[0]);
Note thattoArray(new Object[0])
is identical in function totoArray()
.- Specified by:
toArray
in interfaceReadOnlyCollection<E>
- Type Parameters:
T
- the component type of the array to contain the collection- Parameters:
a
- the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing the elements of this list
- Throws:
java.lang.ArrayStoreException
- if the runtime type of the specified array is not a supertype of the runtime type of every element in this listjava.lang.NullPointerException
- if the specified array is null
-
containsAll
boolean containsAll(java.util.Collection<?> c)
ReturnsTRUE
if this list contains all of the elements of the specified collection.- Specified by:
containsAll
in interfaceReadOnlyCollection<E>
- Parameters:
c
- collection to be checked for containment in this list- Returns:
TRUE
if this list contains all of the elements of the specified collection- Throws:
java.lang.ClassCastException
- if the types of one or more elements in the specified collection are incompatible with this list (optional)java.lang.NullPointerException
- if the specified collection contains one or more null elements and this list does not permit null elements (optional), or if the specified collection is null- See Also:
contains(Object)
-
equals
boolean equals(java.lang.Object o)
Compares the specified object with this list for equality. ReturnsTRUE
if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elementse1
ande2
are equal ifObjects.equals(e1, e2)
.) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of theReadOnlyList
interface.- Specified by:
equals
in interfaceReadOnlyCollection<E>
- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- the object to be compared for equality with this list- Returns:
TRUE
if the specified object is equal to this list- See Also:
Object.equals(Object)
,ReadOnlySet.equals(Object)
,equals(Object)
-
hashCode
int hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1; for (E e : list) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
This ensures thatlist1.equals(list2)
implies thatlist1.hashCode()==list2.hashCode()
for any two lists,list1
andlist2
, as required by the general contract ofObject.hashCode
.- Specified by:
hashCode
in interfaceReadOnlyCollection<E>
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code value for this list
- See Also:
Object.equals(Object)
,equals(Object)
-
get
-
indexOf
int indexOf(java.lang.Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest indexi
such thatObjects.equals(o, get(i))
, or -1 if there is no such index.- Parameters:
o
- element to search for- Returns:
- the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
- Throws:
java.lang.ClassCastException
- if the type of the specified element is incompatible with this list (optional)java.lang.NullPointerException
- if the specified element is null and this list does not permit null elements (optional)
-
lastIndexOf
int lastIndexOf(java.lang.Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest indexi
such thatObjects.equals(o, get(i))
, or -1 if there is no such index.- Parameters:
o
- element to search for- Returns:
- the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
- Throws:
java.lang.ClassCastException
- if the type of the specified element is incompatible with this list (optional)java.lang.NullPointerException
- if the specified element is null and this list does not permit null elements (optional)
-
listIterator
ReadOnlyListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).- Returns:
- a list iterator over the elements in this list (in proper sequence)
-
listIterator
ReadOnlyListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call toReadOnlyListIterator.next()
. An initial call toReadOnlyListIterator.previous()
would return the element with the specified index minus one.- Parameters:
index
- index of the first element to be returned from the list iterator (by a call tonext
)- Returns:
- a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()
)
-
subList
ReadOnlyList<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specifiedfromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed forindexOf
andlastIndexOf
, and all of the algorithms in theCollections
class can be applied to a subList.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)- Parameters:
fromIndex
- low endpoint (inclusive) of the subListtoIndex
- high endpoint (exclusive) of the subList- Returns:
- a view of the specified range within this list
- Throws:
java.lang.IndexOutOfBoundsException
- for an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex
)
-
spliterator
default java.util.Spliterator<E> spliterator()
Description copied from interface:ReadOnlyCollection
Creates aSpliterator
over the elements in this collection.
Implementations should document characteristic values reported by the spliterator. Such characteristic values are not required to be reported if the spliterator reportsSpliterator.SIZED
and this collection contains no elements.
The default implementation should be overridden by subclasses that can return a more efficient spliterator. In order to preserve expected laziness behavior for theReadOnlyCollection.stream()
andReadOnlyCollection.parallelStream()
methods, spliterators should either have the characteristic ofIMMUTABLE
orCONCURRENT
, or be late-binding. If none of these is practical, the overriding class should describe the spliterator's documented policy of binding and structural interference, and should override theReadOnlyCollection.stream()
andReadOnlyCollection.parallelStream()
methods to create streams using aSupplier
of the spliterator, as in:
Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)
These requirements ensure that streams produced by theReadOnlyCollection.stream()
andReadOnlyCollection.parallelStream()
methods will reflect the contents of the collection as of initiation of the terminal stream operation.
The default implementation creates a late-binding spliterator from the collection'sIterator
.
The createdSpliterator
reportsSpliterator.SIZED
.- Specified by:
spliterator
in interfacejava.lang.Iterable<E>
- Specified by:
spliterator
in interfaceReadOnlyCollection<E>
- Returns:
- a
Spliterator
over the elements in this collection - Code:
- Exact Method Body:
return (this instanceof RandomAccess) ? new AbstractReadOnlyList.RandomAccessSpliterator<>(this) : Spliterators.spliterator(this.iterator(), this.size(), Spliterator.ORDERED);
-
of
static <E> ReadOnlyList<E> of()
Returns an unmodifiable list containing zero elements.- Type Parameters:
E
- theList
's element type- Returns:
- an empty
List
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of());
-
of
static <E> ReadOnlyList<E> of(E e1)
Returns an unmodifiable list containing one element.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the single element- Returns:
- a
List
containing the specified element - Throws:
java.lang.NullPointerException
- if the element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2)
Returns an unmodifiable list containing two elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3)
Returns an unmodifiable list containing three elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2, e3));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4)
Returns an unmodifiable list containing four elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2, e3, e4));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4, E e5)
Returns an unmodifiable list containing five elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth elemente5
- the fifth element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2, e3, e4, e5));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
Returns an unmodifiable list containing six elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth elemente5
- the fifth elemente6
- the sixth element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2, e3, e4, e5, e6));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
Returns an unmodifiable list containing seven elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth elemente5
- the fifth elemente6
- the sixth elemente7
- the seventh element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2, e3, e4, e5, e6, e7));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
Returns an unmodifiable list containing eight elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth elemente5
- the fifth elemente6
- the sixth elemente7
- the seventh elemente8
- the eighth element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(e1, e2, e3, e4, e5, e6, e7, e8));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
Returns an unmodifiable list containing nine elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth elemente5
- the fifth elemente6
- the sixth elemente7
- the seventh elemente8
- the eighth elemente9
- the ninth element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList (java.util.List.of(e1, e2, e3, e4, e5, e6, e7, e8, e9));
-
of
static <E> ReadOnlyList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
Returns an unmodifiable list containing ten elements.- Type Parameters:
E
- theList
's element type- Parameters:
e1
- the first elemente2
- the second elemente3
- the third elemente4
- the fourth elemente5
- the fifth elemente6
- the sixth elemente7
- the seventh elemente8
- the eighth elemente9
- the ninth elemente10
- the tenth element- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList (java.util.List.of(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10));
-
of
@SafeVarargs static <E> ReadOnlyList<E> of(E... elements)
Returns an unmodifiable list containing an arbitrary number of elements.- Type Parameters:
E
- theList
's element type- Parameters:
elements
- the elements to be contained in the list- Returns:
- a
List
containing the specified elements - Throws:
java.lang.NullPointerException
- if an element isnull
or if the array isnull
- Code:
- Exact Method Body:
return InterfaceBuilder.toReadOnlyList(java.util.List.of(elements));
-
copyOf
static <E> ReadOnlyList<E> copyOf(ReadOnlyCollection<? extends E> coll)
Returns aReadOnlyList
containing the elements of the givenReadOnlyCollection
, in its iteration order. The givenCollection
must not be null, and it must not contain any null elements. If the givenCollection
is subsequently modified, the returnedReadOnlyList
will not reflect such modifications- Type Parameters:
E
- theReadOnlyList
's element type- Parameters:
coll
- aReadOnlyCollection
from which elements are drawn, must be non-null- Returns:
- a
ReadOnlyList
containing the elements of the givenCollection
- Throws:
java.lang.NullPointerException
- if coll is null, or if it contains any nulls- Code:
- Exact Method Body:
return (ReadOnlyList.class.isAssignableFrom(coll.getClass())) ? (ReadOnlyList<E>) coll : new ReadOnlyArrayList<E>((ReadOnlyCollection<E>) coll, coll.size());
-
-