Class AbstractMapBasedMultimap.WrappedCollection

  • All Implemented Interfaces:
    java.lang.Iterable<V>, java.util.Collection<V>
    Direct Known Subclasses:
    AbstractMapBasedMultimap.WrappedList, AbstractMapBasedMultimap.WrappedSet, AbstractMapBasedMultimap.WrappedSortedSet
    Enclosing class:
    AbstractMapBasedMultimap<K,​V>

    private class AbstractMapBasedMultimap.WrappedCollection
    extends java.util.AbstractCollection<V>
    Collection decorator that stays in sync with the multimap values for a key. There are two kinds of wrapped collections: full and subcollections. Both have a delegate pointing to the underlying collection class.

    Full collections, identified by a null ancestor field, contain all multimap values for a given key. Its delegate is a value in AbstractMapBasedMultimap.map whenever the delegate is non-empty. The refreshIfEmpty, removeIfEmpty, and addToMap methods ensure that the WrappedCollection and map remain consistent.

    A subcollection, such as a sublist, contains some of the values for a given key. Its ancestor field points to the full wrapped collection with all values for the key. The subcollection refreshIfEmpty, removeIfEmpty, and addToMap methods call the corresponding methods of the full wrapped collection.

    • Method Detail

      • refreshIfEmpty

        void refreshIfEmpty()
        If the delegate collection is empty, but the multimap has values for the key, replace the delegate with the new collection for the key.

        For a subcollection, refresh its ancestor and validate that the ancestor delegate hasn't changed.

      • removeIfEmpty

        void removeIfEmpty()
        If collection is empty, remove it from AbstractMapBasedMultimap.this.map. For subcollections, check whether the ancestor collection is empty.
      • getKey

        K getKey()
      • addToMap

        void addToMap()
        Add the delegate to the map. Other WrappedCollection methods should call this method after adding elements to a previously empty collection.

        Subcollection add the ancestor's delegate instead.

      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<V>
        Specified by:
        size in class java.util.AbstractCollection<V>
      • equals

        public boolean equals​(@Nullable
                              java.lang.Object object)
        Specified by:
        equals in interface java.util.Collection<V>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<V>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractCollection<V>
      • getDelegate

        java.util.Collection<V> getDelegate()
      • iterator

        public java.util.Iterator<V> iterator()
        Specified by:
        iterator in interface java.util.Collection<V>
        Specified by:
        iterator in interface java.lang.Iterable<V>
        Specified by:
        iterator in class java.util.AbstractCollection<V>
      • add

        public boolean add​(V value)
        Specified by:
        add in interface java.util.Collection<V>
        Overrides:
        add in class java.util.AbstractCollection<V>
      • addAll

        public boolean addAll​(java.util.Collection<? extends V> collection)
        Specified by:
        addAll in interface java.util.Collection<V>
        Overrides:
        addAll in class java.util.AbstractCollection<V>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<V>
        Overrides:
        contains in class java.util.AbstractCollection<V>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<V>
        Overrides:
        containsAll in class java.util.AbstractCollection<V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<V>
        Overrides:
        clear in class java.util.AbstractCollection<V>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<V>
        Overrides:
        remove in class java.util.AbstractCollection<V>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<V>
        Overrides:
        removeAll in class java.util.AbstractCollection<V>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<V>
        Overrides:
        retainAll in class java.util.AbstractCollection<V>