Class CollectionsExt.AbstractImmutableCollection<C extends Collection<E>,E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- eu.simuline.util.CollectionsExt.AbstractImmutableCollection<C,E>
-
- Type Parameters:
C- the class extendingCollectionwith elements in E.E- the class of the elements of this collection.
- All Implemented Interfaces:
Iterable<E>,Collection<E>
- Direct Known Subclasses:
CollectionsExt.ImmutableCollection,CollectionsExt.ImmutableList,CollectionsExt.ImmutableSet,CollectionsExt.ImmutableSortedSet
- Enclosing class:
- CollectionsExt<E>
public abstract static class CollectionsExt.AbstractImmutableCollection<C extends Collection<E>,E> extends AbstractCollection<E>
A class ofCollections of elements of classEextendingCinitially throwing anUnsupportedOperationExceptionwhen trying to modify the collection either directly or via its iterator(s) or via a transparent view likeList.subList(int, int). Note that neither arrays given e.g. byCollection.toArray()norSpliterators given byCollection.spliterator()norStreams given byCollection.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 methodCollection.remove(Object)but it is possible to implementCollection.removeIf(Predicate)without invokingCollection.remove(Object)in implementations of implementing classes ofCollection. it is possible, although not likely, thatInstances of this class can be created via creator methods like
CollectionsExt.getImmutableCollection(Collection). The collection with the original restrictions can be regained byunrestricted(). A posteriory modifications can be allowed usingallowModification(CollectionsExt.Modification)or usingallowModifications(Set). This class ispublicandCollectionsExt.getImmutableCollection(Collection)returns instances of this class, to allow invoking methodsallowModification(CollectionsExt.Modification)andallowModifications(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 toallowedModifications().
-
-
Field Summary
Fields Modifier and Type Field Description private Set<CollectionsExt.Modification>modsThe set of allowed modifications.private static longserialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description privateAbstractImmutableCollection()Creates an empty collection with no allowed modifications.(package private)AbstractImmutableCollection(Set<CollectionsExt.Modification> mods)Creates an empty collection with the allowed modifications given bymods.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanadd(E obj)booleanaddAll(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 modificationmodfor this set, does not alter contents and returnsthis.voidallowModifications(Set<CollectionsExt.Modification> mods)Allows in addition modifications inmodsfor this set, does not alter contents and returnsthis.voidclear()Iterator<E>iterator()Returns an iterator with methodIterator.remove()which throws anUnsupportedOperationExceptionif eitherunrestricted()'s iterator does not allow this operation orRemoveObjis no allowed operation according toallowedModifications().booleanremove(Object obj)booleanremoveAll(Collection<?> cmp)booleanremoveIf(Predicate<? super E> filter)booleanretainAll(Collection<?> coll)intsize()Returns the size of this collection.StringtoString()Returns a string representation of this set, including the allowed modifications and the wrapped set.abstract Cunrestricted()Returns the underlying set without the restrictions imposed by thisCollectionsExt.ImmutableCollection.-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, toArray, toArray
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, spliterator, stream, toArray
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
mods
private final Set<CollectionsExt.Modification> mods
The set of allowed modifications.
-
-
Constructor Detail
-
AbstractImmutableCollection
private AbstractImmutableCollection()
Creates an empty collection with no allowed modifications.
-
AbstractImmutableCollection
AbstractImmutableCollection(Set<CollectionsExt.Modification> mods)
Creates an empty collection with the allowed modifications given bymods. CAUTION: This is not a collection containingmods.- Parameters:
mods- The modifications currently allowed.
-
-
Method Detail
-
allowModification
public final CollectionsExt.AbstractImmutableCollection<C,E> allowModification(CollectionsExt.Modification mod)
Allows in addition modificationmodfor this set, does not alter contents and returnsthis.- Parameters:
mod- A modification which shall be allowed in addition.
-
allowModifications
public final void allowModifications(Set<CollectionsExt.Modification> mods)
Allows in addition modifications inmodsfor this set, does not alter contents and returnsthis.- Parameters:
mods- modifications which shall be allowed in addition.
-
allowedModifications
public final 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. Also applyingallowModifications(Set)modifies the returned set.- Returns:
- the set of currently allowed modifications.
-
unrestricted
public abstract C unrestricted()
Returns the underlying set without the restrictions imposed by thisCollectionsExt.ImmutableCollection. Note that the result may still throwUnsupportedOperationExceptions depending on the implementation.
-
size
public final int size()
Returns the size of this collection.- Specified by:
sizein interfaceCollection<C extends Collection<E>>- Specified by:
sizein classAbstractCollection<E>
-
add
public final boolean add(E obj)
- Specified by:
addin interfaceCollection<C extends Collection<E>>- Overrides:
addin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orAddObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to add an object which is the case onlythisdoes not already containobj.
-
remove
public final boolean remove(Object obj)
- Specified by:
removein interfaceCollection<C extends Collection<E>>- Overrides:
removein classAbstractCollection<E>- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orRemoveObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to remove an object which is the case onlythiscontainsobj.
-
clear
public final void clear()
- Specified by:
clearin interfaceCollection<C extends Collection<E>>- Overrides:
clearin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orRemoveObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to clearthiswhich is the case only ifthisis not empty already.
-
addAll
public final boolean addAll(Collection<? extends E> coll)
- Specified by:
addAllin interfaceCollection<C extends Collection<E>>- Overrides:
addAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orAddObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to add objects which is the case only ifcollis not a subcollection ofthis.
-
retainAll
public final boolean retainAll(Collection<?> coll)
- Specified by:
retainAllin interfaceCollection<C extends Collection<E>>- Overrides:
retainAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orRemoveObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to remove objects which is the case only ifthisis not a subcollection ofcoll.
-
removeAll
public final boolean removeAll(Collection<?> cmp)
- Specified by:
removeAllin interfaceCollection<C extends Collection<E>>- Overrides:
removeAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orRemoveObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to remove objects which is the case only ifthisandcollare not disjoint.
-
removeIf
public final boolean removeIf(Predicate<? super E> filter)
- Throws:
UnsupportedOperationException- if eitherunrestricted()does not allow this operation orRemoveObjis no allowed operation according toallowedModifications(). In the latter case it does not matter, whether effectively it is tried to remove objects which is the case only iffilteraccepts an element ofthis.
-
iterator
public final Iterator<E> iterator()
Returns an iterator with methodIterator.remove()which throws anUnsupportedOperationExceptionif eitherunrestricted()'s iterator does not allow this operation orRemoveObjis no allowed operation according toallowedModifications().- Specified by:
iteratorin interfaceCollection<C extends Collection<E>>- Specified by:
iteratorin interfaceIterable<C extends Collection<E>>- Specified by:
iteratorin classAbstractCollection<E>
-
toString
public final String toString()
Returns a string representation of this set, including the allowed modifications and the wrapped set.- Overrides:
toStringin classAbstractCollection<E>
-
-