Class UnmodifiableMultiSet<E>

  • Type Parameters:
    E - the type held in the multiset
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, MultiSet<E>, Unmodifiable

    public final class UnmodifiableMultiSet<E>
    extends AbstractMultiSetDecorator<E>
    implements Unmodifiable
    Decorates another MultiSet to ensure it can't be altered.

    Attempts to modify it will result in an UnsupportedOperationException.

    Since:
    4.1
    See Also:
    Serialized Form
    • Nested Class Summary

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E object)
      Adds one copy of the specified object to the MultiSet.
      int add​(E object, int count)
      Adds a number of occurrences of the specified object to the MultiSet.
      boolean addAll​(java.util.Collection<? extends E> coll)  
      void clear()  
      java.util.Set<MultiSet.Entry<E>> entrySet()
      Returns a Set of all entries contained in the MultiSet.
      java.util.Iterator<E> iterator()
      Returns an Iterator over the entire set of members, including copies due to cardinality.
      boolean remove​(java.lang.Object object)
      Removes one occurrence of the given object from the MultiSet.
      int remove​(java.lang.Object object, int count)
      Removes a number of occurrences of the specified object from the MultiSet.
      boolean removeAll​(java.util.Collection<?> coll)
      Remove all occurrences of all elements from this MultiSet represented in the given collection.
      boolean removeIf​(java.util.function.Predicate<? super E> filter)  
      boolean retainAll​(java.util.Collection<?> coll)
      Remove any elements of this MultiSet that are not contained in the given collection.
      int setCount​(E object, int count)
      Sets the number of occurrences of the specified object in the MultiSet to the given count.
      java.util.Set<E> uniqueSet()
      Returns a Set of unique elements in the MultiSet.
      static <E> MultiSet<E> unmodifiableMultiSet​(MultiSet<? extends E> multiset)
      Factory method to create an unmodifiable multiset.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        contains, isEmpty, parallelStream, spliterator, stream, toArray, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Method Detail

      • unmodifiableMultiSet

        public static <E> MultiSet<E> unmodifiableMultiSet​(MultiSet<? extends E> multiset)
        Factory method to create an unmodifiable multiset.

        If the multiset passed in is already unmodifiable, it is returned.

        Type Parameters:
        E - the type of the elements in the multiset
        Parameters:
        multiset - the multiset to decorate, may not be null
        Returns:
        an unmodifiable MultiSet
        Throws:
        java.lang.NullPointerException - if multiset is null
      • iterator

        public java.util.Iterator<E> iterator()
        Description copied from interface: MultiSet
        Returns an Iterator over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface MultiSet<E>
        Overrides:
        iterator in class AbstractCollectionDecorator<E>
        Returns:
        iterator over all elements in the MultiSet
      • add

        public boolean add​(E object)
        Description copied from interface: MultiSet
        Adds one copy of the specified object to the MultiSet.

        If the object is already in the MultiSet.uniqueSet() then increment its count as reported by MultiSet.getCount(Object). Otherwise add it to the MultiSet.uniqueSet() and report its count as 1.

        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface MultiSet<E>
        Overrides:
        add in class AbstractCollectionDecorator<E>
        Parameters:
        object - the object to add
        Returns:
        true always, as the size of the MultiSet is increased in any case
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> coll)
        Specified by:
        addAll in interface java.util.Collection<E>
        Overrides:
        addAll in class AbstractCollectionDecorator<E>
      • remove

        public boolean remove​(java.lang.Object object)
        Description copied from interface: MultiSet
        Removes one occurrence of the given object from the MultiSet.

        If the number of occurrences after this operations is reduced to zero, the object will be removed from the MultiSet.uniqueSet().

        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface MultiSet<E>
        Overrides:
        remove in class AbstractCollectionDecorator<E>
        Parameters:
        object - the object to remove
        Returns:
        true if this call changed the collection
      • removeIf

        public boolean removeIf​(java.util.function.Predicate<? super E> filter)
        Specified by:
        removeIf in interface java.util.Collection<E>
        Overrides:
        removeIf in class AbstractCollectionDecorator<E>
        Since:
        4.4
      • removeAll

        public boolean removeAll​(java.util.Collection<?> coll)
        Description copied from interface: MultiSet
        Remove all occurrences of all elements from this MultiSet represented in the given collection.
        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface MultiSet<E>
        Overrides:
        removeAll in class AbstractCollectionDecorator<E>
        Parameters:
        coll - the collection of elements to remove
        Returns:
        true if this call changed the multiset
      • retainAll

        public boolean retainAll​(java.util.Collection<?> coll)
        Description copied from interface: MultiSet
        Remove any elements of this MultiSet that are not contained in the given collection.
        Specified by:
        retainAll in interface java.util.Collection<E>
        Specified by:
        retainAll in interface MultiSet<E>
        Overrides:
        retainAll in class AbstractCollectionDecorator<E>
        Parameters:
        coll - the collection of elements to retain
        Returns:
        true if this call changed the multiset
      • setCount

        public int setCount​(E object,
                            int count)
        Description copied from interface: MultiSet
        Sets the number of occurrences of the specified object in the MultiSet to the given count.

        If the provided count is zero, the object will be removed from the MultiSet.uniqueSet().

        Specified by:
        setCount in interface MultiSet<E>
        Overrides:
        setCount in class AbstractMultiSetDecorator<E>
        Parameters:
        object - the object to update
        count - the number of occurrences of the object
        Returns:
        the number of occurrences of the object before this operation, zero if the object was not contained in the multiset
      • add

        public int add​(E object,
                       int count)
        Description copied from interface: MultiSet
        Adds a number of occurrences of the specified object to the MultiSet.

        If the object is already in the MultiSet.uniqueSet() then increment its count as reported by MultiSet.getCount(Object). Otherwise add it to the MultiSet.uniqueSet() and report its count as occurrences.

        Specified by:
        add in interface MultiSet<E>
        Overrides:
        add in class AbstractMultiSetDecorator<E>
        Parameters:
        object - the object to add
        count - the number of occurrences to add, may be zero, in which case no change is made to the multiset
        Returns:
        the number of occurrences of the object in the multiset before this operation; possibly zero
      • remove

        public int remove​(java.lang.Object object,
                          int count)
        Description copied from interface: MultiSet
        Removes a number of occurrences of the specified object from the MultiSet.

        If the number of occurrences to remove is greater than the actual number of occurrences in the multiset, the object will be removed from the multiset.

        Specified by:
        remove in interface MultiSet<E>
        Overrides:
        remove in class AbstractMultiSetDecorator<E>
        Parameters:
        object - the object to remove
        count - the number of occurrences to remove, may be zero, in which case no change is made to the multiset
        Returns:
        the number of occurrences of the object in the multiset before the operation; possibly zero
      • uniqueSet

        public java.util.Set<E> uniqueSet()
        Description copied from interface: MultiSet
        Returns a Set of unique elements in the MultiSet.

        Uniqueness constraints are the same as those in Set.

        The returned set is backed by this multiset, so any change to either is immediately reflected in the other. Only removal operations are supported, in which case all occurrences of the element are removed from the backing multiset.

        Specified by:
        uniqueSet in interface MultiSet<E>
        Overrides:
        uniqueSet in class AbstractMultiSetDecorator<E>
        Returns:
        the Set of unique MultiSet elements
      • entrySet

        public java.util.Set<MultiSet.Entry<E>> entrySet()
        Description copied from interface: MultiSet
        Returns a Set of all entries contained in the MultiSet.

        The returned set is backed by this multiset, so any change to either is immediately reflected in the other.

        Specified by:
        entrySet in interface MultiSet<E>
        Overrides:
        entrySet in class AbstractMultiSetDecorator<E>
        Returns:
        the Set of MultiSet entries