Package Torello.Java.ReadOnly
Interface ReadOnlySet<E>
-
- Type Parameters:
E- the type of elements maintained by this set
- All Superinterfaces:
java.lang.Iterable<E>,ReadOnlyCollection<E>
- All Known Subinterfaces:
ReadOnlyNavigableSet<E>,ReadOnlySequencedSet<E>,ReadOnlySortedSet<E>
- All Known Implementing Classes:
ReadOnlyHashSet,ReadOnlyTreeSet
public interface ReadOnlySet<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.Set<E>. This interface contains all of the methods that the standard Java interface ReadOnlySet contains - except those which would directly or indirectly modify / mutate the internal data-structure.
Hi-Lited Source-Code:- View Here: Torello/Java/ReadOnly/ReadOnlySet.java
- Open New Browser-Tab: Torello/Java/ReadOnly/ReadOnlySet.java
File Size: 22,935 Bytes Line Count: 519 '\n' Characters Found
-
-
Method Summary
Checking this Set's Contents Modifier and Type Method booleancontains(Object o)booleancontainsAll(Collection<?> c)booleanisEmpty()intsize()Convert this Set 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 Set<E>cloneToSet()Set<E>wrapToImmutableSet()Static Methods: Builder's Modifier and Type Method static <E> ReadOnlySet<E>copyOf(Collection<? extends E> coll)static <E> ReadOnlySet<E>of()static <E> ReadOnlySet<E>of(E e1)static <E> ReadOnlySet<E>of(E... elements)static <E> ReadOnlySet<E>of(E e1, E e2)static <E> ReadOnlySet<E>of(E e1, E e2, E e3)static <E> ReadOnlySet<E>of(E e1, E e2, E e3, E e4)static <E> ReadOnlySet<E>of(E e1, E e2, E e3, E e4, E e5)static <E> ReadOnlySet<E>of(E e1, E e2, E e3, E e4, E e5, E e6)static <E> ReadOnlySet<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)static <E> ReadOnlySet<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)static <E> ReadOnlySet<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)static <E> ReadOnlySet<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()-
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 set (its cardinality). If this set contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Specified by:
sizein interfaceReadOnlyCollection<E>- Returns:
- the number of elements in this set (its cardinality)
-
isEmpty
boolean isEmpty()
ReturnsTRUEif this set contains no elements.- Specified by:
isEmptyin interfaceReadOnlyCollection<E>- Returns:
TRUEif this set contains no elements
-
contains
boolean contains(java.lang.Object o)
ReturnsTRUEif this set contains the specified element. More formally, returnsTRUEif and only if this set contains an elementesuch thatObjects.equals(o, e).- Specified by:
containsin interfaceReadOnlyCollection<E>- Parameters:
o- element whose presence in this set is to be tested- Returns:
TRUEif this set contains the specified element- Throws:
java.lang.ClassCastException- if the type of the specified element is incompatible with this set (optional)java.lang.NullPointerException- if the specified element is null and this set does not permit null elements (optional)
-
iterator
RemoveUnsupportedIterator<E> iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).- Specified by:
iteratorin interfacejava.lang.Iterable<E>- Specified by:
iteratorin interfaceReadOnlyCollection<E>- Returns:
- an iterator over the elements in this set
-
toArray
java.lang.Object[] toArray()
Returns an array containing all of the elements in this set. If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
The returned array will be "safe" in that no references to it are maintained by this set. (In other words, this method must allocate a new array even if this set 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 the elements in this set
-
toArray
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set 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 set.
If this set fits in the specified array with room to spare (i.e., the array has more elements than this set), the element in the array immediately following the end of the set is set tonull. (This is useful in determining the length of this set only if the caller knows that this set does not contain any null elements.)
If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
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 set known to contain only strings. The following code can be used to dump the set 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 set 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 all the elements in this set
- 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 setjava.lang.NullPointerException- if the specified array is null
-
containsAll
boolean containsAll(java.util.Collection<?> c)
ReturnsTRUEif this set contains all of the elements of the specified collection. If the specified collection is also a set, this method returnsTRUEif it is a subset of this set.- Specified by:
containsAllin interfaceReadOnlyCollection<E>- Parameters:
c- collection to be checked for containment in this set- Returns:
TRUEif this set 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 set (optional)java.lang.NullPointerException- if the specified collection contains one or more null elements and this set does not permit null elements (optional), or if the specified collection is null- See Also:
contains(Object)
-
cloneToSet
java.util.Set<E> cloneToSet()
Clones the internaljava.util.Setrepresentation, and returns it. This is the Top-Level Interface-Variant of this method. It returns the Interface-Implementationjava.util.Set.
Alternate Variant:
The two classes which implement the interfaceReadOnlySet(which areReadOnlyHashSetandReadOnlyTreeSet), for convenience, also implement an identical "Wrap To" Method which returns the exact type of the internalSetrepresentation (which arejava.util.HashSetandjava.util.TreeSet) - rather than the Top-Level Interface-Typejava.util.Set.- Returns:
- For the sub-class
ReadOnlyHashSet, this will return a cloned instance of the internaljava.util.HashSet.
- For the sub-class
ReadOnlyTreeSet, this will return a cloned instance of the internaljava.util.TreeSet.
- For the sub-class
-
wrapToImmutableSet
java.util.Set<E> wrapToImmutableSet()
Invokesjava.util.Collections.unmodifiableSeton the internalSet
Standard JDKSet:
There may be situations where the TypeReadOnlySetis 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.Set, but throws an exception if any attempts are made to invoke Mutator-Methods.
Unmodifiable-Collections throw Exceptions instead:
The returnedjava.util.Setinstance 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
Setwhich adheres to the JDK interfacejava.util.Set, but throws anUnsupportedOperationExceptionif a user attempts to invoke a Mutator-Method on the returned instance.
-
equals
boolean equals(java.lang.Object o)
Compares the specified object with this set for equality. ReturnsTRUEif the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.- Specified by:
equalsin interfaceReadOnlyCollection<E>- Overrides:
equalsin classjava.lang.Object- Parameters:
o- object to be compared for equality with this set- Returns:
TRUEif the specified object is equal to this set- See Also:
Object.equals(Object),equals(Object),ReadOnlyList.equals(Object)
-
hashCode
int hashCode()
Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hash code of anullelement is defined to be zero. This ensures thats1.equals(s2)implies thats1.hashCode()==s2.hashCode()for any two setss1ands2, 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 set
- See Also:
Object.equals(Object),Set.equals(Object)
-
spliterator
default java.util.Spliterator<E> spliterator()
Creates aSpliteratorover the elements in this set.
TheSpliteratorreportsSpliterator.DISTINCT. Implementations should document the reporting of additional characteristic values.- Specified by:
spliteratorin interfacejava.lang.Iterable<E>- Specified by:
spliteratorin interfaceReadOnlyCollection<E>- Returns:
- a
Spliteratorover the elements in this set - Code:
- Exact Method Body:
return Spliterators.spliterator(this.iterator(), this.size(), Spliterator.DISTINCT);
-
of
static <E> ReadOnlySet<E> of()
Returns an unmodifiable set containing zero elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Returns:
- an empty
Set - Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of());
-
of
static <E> ReadOnlySet<E> of(E e1)
Returns an unmodifiable set containing one element. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Parameters:
e1- the single element- Returns:
- a
Setcontaining the specified element - Throws:
java.lang.NullPointerException- if the element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2)
Returns an unmodifiable set containing two elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Parameters:
e1- the first elemente2- the second element- Returns:
- a
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if the elements are duplicatesjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3)
Returns an unmodifiable set containing three elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Parameters:
e1- the first elemente2- the second elemente3- the third element- Returns:
- a
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3, E e4)
Returns an unmodifiable set containing four elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth element- Returns:
- a
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3, E e4, E e5)
Returns an unmodifiable set containing five elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth element- Returns:
- a
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4, e5));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
Returns an unmodifiable set containing six elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet'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
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4, e5, e6));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
Returns an unmodifiable set containing seven elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet'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
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4, e5, e6, e7));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
Returns an unmodifiable set containing eight elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet'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
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4, e5, e6, e7, e8));
-
of
static <E> ReadOnlySet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
Returns an unmodifiable set containing nine elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet'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
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4, e5, e6, e7, e8, e9));
-
of
static <E> ReadOnlySet<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 set containing ten elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet'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
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10));
-
of
@SafeVarargs static <E> ReadOnlySet<E> of(E... elements)
Returns an unmodifiable set containing an arbitrary number of elements. See Unmodifiable Sets for details.- Type Parameters:
E- theSet's element type- Parameters:
elements- the elements to be contained in the set- Returns:
- a
Setcontaining the specified elements - Throws:
java.lang.IllegalArgumentException- if there are any duplicate elementsjava.lang.NullPointerException- if an element isnullor if the array isnull- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.of(elements));
-
copyOf
static <E> ReadOnlySet<E> copyOf(java.util.Collection<? extends E> coll)
Returns an unmodifiable Set containing the elements of the given Collection. The given Collection must not be null, and it must not contain any null elements. If the given Collection contains duplicate elements, an arbitrary element of the duplicates is preserved. If the given Collection is subsequently modified, the returned Set will not reflect such modifications.- Type Parameters:
E- theSet's element type- Parameters:
coll- aCollectionfrom which elements are drawn, must be non-null- Returns:
- a
Setcontaining the elements of the givenCollection - Throws:
java.lang.NullPointerException- if coll is null, or if it contains any nulls- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>(Set.copyOf(coll));
-
-