Class AbstractMapBasedMultimap.WrappedCollection
- java.lang.Object
-
- java.util.AbstractCollection<V>
-
- com.google.common.collect.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>
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. TherefreshIfEmpty
,removeIfEmpty
, andaddToMap
methods ensure that theWrappedCollection
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
, andaddToMap
methods call the corresponding methods of the full wrapped collection.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
AbstractMapBasedMultimap.WrappedCollection.WrappedIterator
Collection iterator forWrappedCollection
.
-
Field Summary
Fields Modifier and Type Field Description (package private) AbstractMapBasedMultimap.WrappedCollection
ancestor
(package private) java.util.Collection<V>
ancestorDelegate
(package private) java.util.Collection<V>
delegate
(package private) K
key
-
Constructor Summary
Constructors Constructor Description WrappedCollection(K key, java.util.Collection<V> delegate, AbstractMapBasedMultimap.WrappedCollection ancestor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(V value)
boolean
addAll(java.util.Collection<? extends V> collection)
(package private) void
addToMap()
Add the delegate to the map.void
clear()
boolean
contains(java.lang.Object o)
boolean
containsAll(java.util.Collection<?> c)
boolean
equals(java.lang.Object object)
(package private) AbstractMapBasedMultimap.WrappedCollection
getAncestor()
(package private) java.util.Collection<V>
getDelegate()
(package private) K
getKey()
int
hashCode()
java.util.Iterator<V>
iterator()
(package private) 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.boolean
remove(java.lang.Object o)
boolean
removeAll(java.util.Collection<?> c)
(package private) void
removeIfEmpty()
If collection is empty, remove it fromAbstractMapBasedMultimap.this.map
.boolean
retainAll(java.util.Collection<?> c)
int
size()
java.util.Spliterator<V>
spliterator()
java.lang.String
toString()
-
-
-
Field Detail
-
key
final K key
-
delegate
java.util.Collection<V> delegate
-
ancestor
@CheckForNull final AbstractMapBasedMultimap.WrappedCollection ancestor
-
ancestorDelegate
@CheckForNull final java.util.Collection<V> ancestorDelegate
-
-
Constructor Detail
-
WrappedCollection
WrappedCollection(K key, java.util.Collection<V> delegate, @CheckForNull AbstractMapBasedMultimap.WrappedCollection ancestor)
-
-
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 fromAbstractMapBasedMultimap.this.map
. For subcollections, check whether the ancestor collection is empty.
-
getKey
K getKey()
-
addToMap
void addToMap()
Add the delegate to the map. OtherWrappedCollection
methods should call this method after adding elements to a previously empty collection.Subcollection add the ancestor's delegate instead.
-
size
public int size()
-
equals
public boolean equals(@CheckForNull java.lang.Object object)
- Specified by:
equals
in interfacejava.util.Collection<V>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Collection<V>
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<V>
-
getDelegate
java.util.Collection<V> getDelegate()
-
iterator
public java.util.Iterator<V> iterator()
-
spliterator
public java.util.Spliterator<V> spliterator()
-
add
public boolean add(V value)
-
getAncestor
@CheckForNull AbstractMapBasedMultimap.WrappedCollection getAncestor()
-
addAll
public boolean addAll(java.util.Collection<? extends V> collection)
-
contains
public boolean contains(@CheckForNull java.lang.Object o)
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
-
clear
public void clear()
-
remove
public boolean remove(@CheckForNull java.lang.Object o)
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
-
-