C - the class extending Collection with elements in E.E - the class of the elements of this collection.public abstract static class CollectionsExt.AbstractImmutableCollection<C extends Collection<E>,E> extends AbstractCollection<E>
Collections of elements of class E
extending C
initially throwing an UnsupportedOperationException
when trying to modify the collection
either directly or via its iterator(s)
or via a transparent view
like List.subList(int, int).
Note that neither arrays given e.g. by Collection.toArray()
nor Spliterators given by Collection.spliterator()
nor Streams given by Collection.parallelStream()
give no rise to modifications of the underlying collection.
**** currently, replacing (done for lists, e.g.),
is not considered a modification but the objects are transformed...
very esotheric...
**** what must be done is overwrite the default implementation.
Since it is not clear whether the wrapped collection did also overwrite,
it is not clear which kind of operation is done.
Thus it seems appropriate to define another kind of modification:
SetAll
New releases of the jdk may add default methods to interfaces
and may thus add ways to modify a list.
The problem is not so much a default implementation of the interface
since this is based on elementary methods of that interface
but if a class overwrites this
using internal methods or direct access to fields.
In particular, a each default method must be reimplemented:
E.g. Collection.removeIf(Predicate)
uses method Collection.remove(Object)
but it is possible to implement Collection.removeIf(Predicate)
without invoking Collection.remove(Object)
in implementations of implementing classes of Collection.
it is possible, although not likely,
that
Instances of this class can be created via creator methods like
CollectionsExt.getImmutableCollection(Collection).
The collection with the original restrictions can be regained
by unrestricted().
A posteriory modifications can be allowed
using allowModification(CollectionsExt.Modification) or
using allowModifications(Set).
This class is public
and CollectionsExt.getImmutableCollection(Collection)
returns instances of this class, to allow invoking methods
allowModification(CollectionsExt.Modification) and
allowModifications(Set).
Methods defining non-modifying access,
are just delegated to the collection
returned by unrestricted(),
whereas methods for modification are blocked
if the modification is not allowed according to
allowedModifications().
| Modifier and Type | Field and Description |
|---|---|
private Set<CollectionsExt.Modification> |
mods
The set of allowed modifications.
|
private static long |
serialVersionUID |
| Modifier | Constructor and Description |
|---|---|
private |
AbstractImmutableCollection()
Creates an empty collection with no allowed modifications.
|
(package private) |
AbstractImmutableCollection(Set<CollectionsExt.Modification> mods)
Creates an empty collection with the allowed modifications
given by
mods. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E obj) |
boolean |
addAll(Collection<? extends E> coll) |
Set<CollectionsExt.Modification> |
allowedModifications()
Returns the set of allowed modification of this set
without backup: Changing the returned set
changes the allowed modifications for this set.
|
CollectionsExt.AbstractImmutableCollection<C,E> |
allowModification(CollectionsExt.Modification mod)
Allows in addition modification
mod
for this set, does not alter contents and returns this. |
void |
allowModifications(Set<CollectionsExt.Modification> mods)
Allows in addition modifications in
mods
for this set, does not alter contents and returns this. |
void |
clear() |
Iterator<E> |
iterator()
Returns an iterator with method
Iterator.remove()
which throws an UnsupportedOperationException
if either unrestricted()'s iterator
does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications(). |
boolean |
remove(Object obj) |
boolean |
removeAll(Collection<?> cmp) |
boolean |
removeIf(Predicate<? super E> filter) |
boolean |
retainAll(Collection<?> coll) |
int |
size()
Returns the size of this collection.
|
String |
toString()
Returns a string representation of this set,
including the allowed modifications
and the wrapped set.
|
abstract C |
unrestricted()
Returns the underlying set without the restrictions
imposed by this
CollectionsExt.ImmutableCollection. |
contains, containsAll, isEmpty, toArray, toArrayclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitequals, hashCode, parallelStream, spliterator, streamprivate static final long serialVersionUID
private final Set<CollectionsExt.Modification> mods
private AbstractImmutableCollection()
#CollectionsExt.AbstractImmutableCollection(Set)AbstractImmutableCollection(Set<CollectionsExt.Modification> mods)
mods.
CAUTION: This is not a collection containing mods.mods - The modifications currently allowed.public final CollectionsExt.AbstractImmutableCollection<C,E> allowModification(CollectionsExt.Modification mod)
mod
for this set, does not alter contents and returns this.mod - A modification which shall be allowed in addition.public final void allowModifications(Set<CollectionsExt.Modification> mods)
mods
for this set, does not alter contents and returns this.mods - modifications which shall be allowed in addition.public final Set<CollectionsExt.Modification> allowedModifications()
allowModifications(Set)
modifies the returned set.public abstract C unrestricted()
CollectionsExt.ImmutableCollection.
Note that the result
may still throw UnsupportedOperationExceptions
depending on the implementation.public final int size()
size in interface Collection<E>size in class AbstractCollection<E>public final boolean add(E obj)
add in interface Collection<E>add in class AbstractCollection<E>UnsupportedOperationException - if either unrestricted() does not allow this operation
or AddObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to add an object
which is the case only this
does not already contain obj.public final boolean remove(Object obj)
remove in interface Collection<E>remove in class AbstractCollection<E>UnsupportedOperationException - if either unrestricted() does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to remove an object
which is the case only this
contains obj.public final void clear()
clear in interface Collection<E>clear in class AbstractCollection<E>UnsupportedOperationException - if either unrestricted() does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to clear this
which is the case only if this is not empty already.public final boolean addAll(Collection<? extends E> coll)
addAll in interface Collection<E>addAll in class AbstractCollection<E>UnsupportedOperationException - if either unrestricted() does not allow this operation
or AddObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to add objects
which is the case only if coll
is not a subcollection of this.public final boolean retainAll(Collection<?> coll)
retainAll in interface Collection<E>retainAll in class AbstractCollection<E>UnsupportedOperationException - if either unrestricted() does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to remove objects
which is the case only if this
is not a subcollection of coll.public final boolean removeAll(Collection<?> cmp)
removeAll in interface Collection<E>removeAll in class AbstractCollection<E>UnsupportedOperationException - if either unrestricted() does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to remove objects
which is the case only if this
and coll are not disjoint.public final boolean removeIf(Predicate<? super E> filter)
UnsupportedOperationException - if either unrestricted() does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications().
In the latter case it does not matter,
whether effectively it is tried to remove objects
which is the case only if filter
accepts an element of this.public final Iterator<E> iterator()
Iterator.remove()
which throws an UnsupportedOperationException
if either unrestricted()'s iterator
does not allow this operation
or RemoveObj
is no allowed operation
according to allowedModifications().iterator in interface Iterable<E>iterator in interface Collection<E>iterator in class AbstractCollection<E>public final String toString()
toString in class AbstractCollection<E>Copyright © 2012–2018 Simuline Organization (l2r). All rights reserved.