Class CollectionsExt.AbstractImmutableCollection<C extends java.util.Collection<E>,​E>

  • Type Parameters:
    C - the class extending Collection with elements in E.
    E - the class of the elements of this collection.
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.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 java.util.Collection<E>,​E>
    extends java.util.AbstractCollection<E>
    A class of 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().

    • Constructor Detail

      • AbstractImmutableCollection

        private AbstractImmutableCollection()
        Creates an empty collection with no allowed modifications.
      • AbstractImmutableCollection

        AbstractImmutableCollection​(java.util.Set<CollectionsExt.Modification> mods)
        Creates an empty collection with the allowed modifications given by mods. CAUTION: This is not a collection containing mods.
        Parameters:
        mods - The modifications currently allowed.
    • Method Detail

      • allowModifications

        public final void allowModifications​(java.util.Set<CollectionsExt.Modification> mods)
        Allows in addition modifications in mods for this set, does not alter contents and returns this.
        Parameters:
        mods - modifications which shall be allowed in addition.
      • allowedModifications

        public final java.util.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 applying allowModifications(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 this CollectionsExt.ImmutableCollection. Note that the result may still throw UnsupportedOperationExceptions depending on the implementation.
      • size

        public final int size()
        Returns the size of this collection.
        Specified by:
        size in interface java.util.Collection<C extends java.util.Collection<E>>
        Specified by:
        size in class java.util.AbstractCollection<E>
      • add

        public final boolean add​(E obj)
        Specified by:
        add in interface java.util.Collection<C extends java.util.Collection<E>>
        Overrides:
        add in class java.util.AbstractCollection<E>
        Throws:
        java.lang.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.
      • remove

        public final boolean remove​(java.lang.Object obj)
        Specified by:
        remove in interface java.util.Collection<C extends java.util.Collection<E>>
        Overrides:
        remove in class java.util.AbstractCollection<E>
        Throws:
        java.lang.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.
      • clear

        public final void clear()
        Specified by:
        clear in interface java.util.Collection<C extends java.util.Collection<E>>
        Overrides:
        clear in class java.util.AbstractCollection<E>
        Throws:
        java.lang.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.
      • addAll

        public final boolean addAll​(java.util.Collection<? extends E> coll)
        Specified by:
        addAll in interface java.util.Collection<C extends java.util.Collection<E>>
        Overrides:
        addAll in class java.util.AbstractCollection<E>
        Throws:
        java.lang.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.
      • retainAll

        public final boolean retainAll​(java.util.Collection<?> coll)
        Specified by:
        retainAll in interface java.util.Collection<C extends java.util.Collection<E>>
        Overrides:
        retainAll in class java.util.AbstractCollection<E>
        Throws:
        java.lang.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.
      • removeAll

        public final boolean removeAll​(java.util.Collection<?> cmp)
        Specified by:
        removeAll in interface java.util.Collection<C extends java.util.Collection<E>>
        Overrides:
        removeAll in class java.util.AbstractCollection<E>
        Throws:
        java.lang.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.
      • removeIf

        public final boolean removeIf​(java.util.function.Predicate<? super E> filter)
        Throws:
        java.lang.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.
      • iterator

        public final java.util.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().
        Specified by:
        iterator in interface java.util.Collection<C extends java.util.Collection<E>>
        Specified by:
        iterator in interface java.lang.Iterable<C extends java.util.Collection<E>>
        Specified by:
        iterator in class java.util.AbstractCollection<E>
      • toString

        public final java.lang.String toString()
        Returns a string representation of this set, including the allowed modifications and the wrapped set.
        Overrides:
        toString in class java.util.AbstractCollection<E>