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-JDKAccount. Though the original file has been modified, few changes have been applied to the Javadoc Commenting. Due to fact that that is a Javainterfacefile, 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 GitHubpublic (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 V2also 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: 32,197 Bytes Line Count: 776 '\n' Characters Found
-
-
Method Summary
Primary List Methods Modifier and Type Method Eget(int index)ReadOnlyList<E>subList(int fromIndex, int toIndex)Retrieve a List Element's Index Modifier and Type Method intindexOf(Object o)intlastIndexOf(Object o)Check this List's Contents Modifier and Type Method booleancontains(Object o)booleancontainsAll(Collection<?> c)booleanisEmpty()intsize()Convert this List to an Array Modifier and Type Method Object[]toArray()<T> T[]toArray(T[] a)Convert 'this' to a Standard 'java.util' Collections-Framework Type Modifier and Type Method List<E>cloneToList()List<E>wrapToImmutableList()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 booleanequals(Object o)inthashCode()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_VALUEelements, returnsInteger.MAX_VALUE.- Specified by:
sizein interfaceReadOnlyCollection<E>- Returns:
- the number of elements in this list
-
isEmpty
boolean isEmpty()
ReturnsTRUEif this list contains no elements.- Specified by:
isEmptyin interfaceReadOnlyCollection<E>- Returns:
TRUEif this list contains no elements
-
contains
boolean contains(java.lang.Object o)
ReturnsTRUEif this list contains the specified element. More formally, returnsTRUEif and only if this list contains at least one elementesuch thatObjects.equals(o, e).- Specified by:
containsin interfaceReadOnlyCollection<E>- Parameters:
o- element whose presence in this list is to be tested- Returns:
TRUEif 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:
iteratorin interfacejava.lang.Iterable<E>- Specified by:
iteratorin 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:
toArrayin 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.
Supposexis 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:
toArrayin 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)
ReturnsTRUEif this list contains all of the elements of the specified collection.- Specified by:
containsAllin interfaceReadOnlyCollection<E>- Parameters:
c- collection to be checked for containment in this list- Returns:
TRUEif 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. ReturnsTRUEif 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 elementse1ande2are 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 theReadOnlyListinterface.- Specified by:
equalsin interfaceReadOnlyCollection<E>- Overrides:
equalsin classjava.lang.Object- Parameters:
o- the object to be compared for equality with this list- Returns:
TRUEif 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,list1andlist2, as required by the general contract ofObject.hashCode.- Specified by:
hashCodein interfaceReadOnlyCollection<E>- Overrides:
hashCodein 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 indexisuch 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 indexisuch 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)
-
cloneToList
java.util.List<E> cloneToList()
Clones the internaljava.util.Listrepresentation, and returns it. This is the Top-Level Interface-Variant of this method. It returns the Interface-Implementationjava.util.List.
Alternate Variant:
The two classes which implement the interfaceReadOnlyList(which areReadOnlyVectorandReadOnlyArrayList), for convenience, also implement an identical "Wrap To" Method which returns the exact type of the internalListrepresentation (which arejava.util.ArrayListandjava.util.Vector) - rather than the Top-Leval Interface-Typejava.util.List.- Returns:
- For the sub-class
ReadOnlyArrayList, this will return a cloned instance of the internaljava.util.ArrayList.
- For the sub-class
ReadOnlyVector, this will return a cloned instance of the internaljava.util.Vector.
- For the sub-class
-
wrapToImmutableList
java.util.List<E> wrapToImmutableList()
Invokesjava.util.Collections.unmodifiableListon the internalList.
Standard JDKList:
There may be situations where the TypeReadOnlyListis simply not usable with an API that can't be adapted to accept Type's from the Java-HTMLReadOnlyPackage. If such a case arises, this method can be used to generate an immutable instance of a JDK-Standardjava.utilType, which does not inherit fromTorello.Java.ReadOnly
Classjava.util.Collectionsoffers a method that wraps a type into an Unmodifiable-Wrapper that implements the standardjava.util.List, but throws an exception if any attempts are made to invoke Mutator-Methods.
Unmodifiable-Collections throw Exceptions instead:
The returnedjava.util.Listinstance will hopefully provide an acceptable substitute whenReadOnlyType's cannot match an API's requirements. Java's Immutable-Wrappers work on a different principle - these classes retain any / all Mutator-Methods amongst their Type-Members, but instead throw anUnsupportedOperationExceptionif any of those methods are invoked!
Also, if a Java Collections FrameworkUnmodifiable-Class has any Member-Methods that return a "View" into the underlying Data-Structure, the Sub-Lists and Sub-Maps returned by those methods are also, subsequently, wrapped in anUnmodifiable-Wrapper. These returned instances will also throw anUnsupportedOperationExceptionif any of those methods are invoked!
This does provide a shield or layer of protection against modification to guarantee the "Immutability-Contract" offered by these classes, all-the-while returning to native Java API Collection Types, rather thanTorello.Java.ReadOnlyTypes, which may not always be compatible with a given API or Interface requirement!
Remember, many of the Data-Classes insidejava.utilhave methods whose documentation state very clearly that "any changes made the this [View] will be pushed back into the original type."- Returns:
- A
Listwhich adheres to the JDK interfacejava.util.List, but throws anUnsupportedOperationExceptionif a user attempts to invoke a Mutator-Method on the returned instance.
-
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. (IffromIndexandtoIndexare 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 forindexOfandlastIndexOf, and all of the algorithms in theCollectionsclass 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:ReadOnlyCollectionCreates aSpliteratorover 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.SIZEDand 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 ofIMMUTABLEorCONCURRENT, 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 aSupplierof 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 createdSpliteratorreportsSpliterator.SIZED.- Specified by:
spliteratorin interfacejava.lang.Iterable<E>- Specified by:
spliteratorin interfaceReadOnlyCollection<E>- Returns:
- a
Spliteratorover the elements in this collection - Code:
- Exact Method Body:
return (this instanceof RandomAccess) ? new 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 new JavaHTMLReadOnlyList<>(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
Listcontaining the specified element - Throws:
java.lang.NullPointerException- if the element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<> (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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<> (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
Listcontaining the specified elements - Throws:
java.lang.NullPointerException- if an element isnullor if the array isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyList<>(java.util.List.of(elements));
-
copyOf
static <E> ReadOnlyList<E> copyOf(ReadOnlyCollection<? extends E> coll)
Returns aReadOnlyListcontaining the elements of the givenReadOnlyCollection, in its iteration order. The givenCollectionmust not be null, and it must not contain any null elements. If the givenCollectionis subsequently modified, the returnedReadOnlyListwill not reflect such modifications- Type Parameters:
E- theReadOnlyList's element type- Parameters:
coll- aReadOnlyCollectionfrom which elements are drawn, must be non-null- Returns:
- a
ReadOnlyListcontaining 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());
-
-