Lines Matching full:collection
29 import java.util.Collection;
46 * a multimap as a map that associates each key with a collection of values. All
51 * #createCollection()}, which creates an empty collection of values for a key.
56 * to create the collection of values for that key. The subclass should not call
65 * <p>Keys and values may be null, as long as the underlying collection classes
69 * allow duplicates. If the collection, such as a {@link Set}, does not support
72 * List} that allow duplicates, the collection will keep the existing key-value
91 * The map variable contains the collection of values associated with each
93 * contain any values for that key, a new collection generated by
94 * createCollection is added to the map. That same collection instance
96 * all values for the key are removed, the key and collection are removed
99 * The get method returns a WrappedCollection, which decorates the collection
100 * in the map (if the key is present) or an empty collection (if the key is
101 * not present). When the collection delegate in the WrappedCollection is
107 private transient Map<K, Collection<V>> map;
117 protected AbstractMultimap(Map<K, Collection<V>> map) {
123 final void setMap(Map<K, Collection<V>> map) {
126 for (Collection<V> values : map.values()) {
133 * Creates the collection of values for a single key.
138 * <p>The returned collection class determines whether duplicate key-value
141 * @return an empty collection of values
143 abstract Collection<V> createCollection();
146 * Creates the collection of values for an explicitly provided key. By
151 * @param key key to associate with values in the collection
152 * @return an empty collection of values
154 Collection<V> createCollection(@Nullable K key) {
158 Map<K, Collection<V>> backingMap() {
177 for (Collection<V> collection : map.values()) {
178 if (collection.contains(value)) {
187 Collection<V> collection = map.get(key);
188 return collection != null && collection.contains(value);
194 Collection<V> collection = getOrCreateCollection(key);
196 if (collection.add(value)) {
204 private Collection<V> getOrCreateCollection(@Nullable K key) {
205 Collection<V> collection = map.get(key);
206 if (collection == null) {
207 collection = createCollection(key);
208 map.put(key, collection);
210 return collection;
214 Collection<V> collection = map.get(key);
215 if (collection == null) {
219 boolean changed = collection.remove(value);
222 if (collection.isEmpty()) {
235 Collection<V> collection = getOrCreateCollection(key);
236 int oldSize = collection.size();
239 if (values instanceof Collection) {
241 Collection<? extends V> c = (Collection<? extends V>) values;
242 changed = collection.addAll(c);
245 changed |= collection.add(value);
249 totalSize += (collection.size() - oldSize);
264 * <p>The returned collection is immutable.
266 public Collection<V> replaceValues(
273 Collection<V> collection = getOrCreateCollection(key);
274 Collection<V> oldValues = createCollection();
275 oldValues.addAll(collection);
277 totalSize -= collection.size();
278 collection.clear();
281 if (collection.add(iterator.next())) {
292 * <p>The returned collection is immutable.
294 public Collection<V> removeAll(@Nullable Object key) {
295 Collection<V> collection = map.remove(key);
296 Collection<V> output = createCollection();
298 if (collection != null) {
299 output.addAll(collection);
300 totalSize -= collection.size();
301 collection.clear();
307 private Collection<V> unmodifiableCollectionSubclass(
308 Collection<V> collection) {
309 if (collection instanceof SortedSet) {
310 return Collections.unmodifiableSortedSet((SortedSet<V>) collection);
311 } else if (collection instanceof Set) {
312 return Collections.unmodifiableSet((Set<V>) collection);
313 } else if (collection instanceof List) {
314 return Collections.unmodifiableList((List<V>) collection);
316 return Collections.unmodifiableCollection(collection);
321 // Clear each collection, to make previously returned collections empty.
322 for (Collection<V> collection : map.values()) {
323 collection.clear();
334 * <p>The returned collection is not serializable.
336 public Collection<V> get(@Nullable K key) {
337 Collection<V> collection = map.get(key);
338 if (collection == null) {
339 collection = createCollection(key);
341 return wrapCollection(key, collection);
345 * Generates a decorated collection that remains consistent with the values in
347 * returned collection, and vice versa.
349 private Collection<V> wrapCollection(
350 @Nullable K key, Collection<V> collection) {
351 if (collection instanceof SortedSet) {
352 return new WrappedSortedSet(key, (SortedSet<V>) collection, null);
353 } else if (collection instanceof Set) {
354 return new WrappedSet(key, (Set<V>) collection);
355 } else if (collection instanceof List) {
356 return wrapList(key, (List<V>) collection, null);
358 return new WrappedCollection(key, collection, null);
370 * Collection decorator that stays in sync with the multimap values for a key.
372 * have a delegate pointing to the underlying collection class.
381 * given key. Its ancestor field points to the full wrapped collection with
384 * of the full wrapped collection.
388 Collection<V> delegate;
390 final Collection<V> ancestorDelegate;
392 WrappedCollection(@Nullable K key, Collection<V> delegate,
402 * If the delegate collection is empty, but the multimap has values for the
403 * key, replace the delegate with the new collection for the key.
415 Collection<V> newDelegate = map.get(key);
423 * If collection is empty, remove it from {@code map}. For subcollections,
424 * check whether the ancestor collection is empty.
441 * collection.
476 Collection<V> getDelegate() {
485 /** Collection iterator for {@code WrappedCollection}. */
488 final Collection<V> originalDelegate = delegate;
550 @Override public boolean addAll(Collection<? extends V> collection) {
551 if (collection.isEmpty()) {
555 boolean changed = delegate.addAll(collection);
571 @Override public boolean containsAll(Collection<?> c) {
596 @Override public boolean removeAll(Collection<?> c) {
610 @Override public boolean retainAll(Collection<?> c) {
623 private Iterator<V> iteratorOrListIterator(Collection<V> collection) {
624 return (collection instanceof List)
625 ? ((List<V>) collection).listIterator()
626 : collection.iterator();
696 public boolean addAll(int index, Collection<? extends V> c) {
833 ? new SortedKeySet((SortedMap<K, Collection<V>>) map) : new KeySet(map);
842 final Map<K, Collection<V>> subMap;
844 KeySet(final Map<K, Collection<V>> subMap) {
854 final Iterator<Map.Entry<K, Collection<V>>> entryIterator
856 Map.Entry<K, Collection<V>> entry;
867 Collection<V> collection = entry.getValue();
869 totalSize -= collection.size();
870 collection.clear();
883 Collection<V> collection = subMap.remove(key);
884 if (collection != null) {
885 count = collection.size();
886 collection.clear();
892 @Override public boolean containsAll(Collection<?> c) {
907 SortedKeySet(SortedMap<K, Collection<V>> subMap) {
911 SortedMap<K, Collection<V>> sortedMap() {
912 return (SortedMap<K, Collection<V>>) subMap;
956 Collection<V> collection;
958 collection = map.get(key);
965 if (collection == null) {
968 int count = collection.size();
974 Iterator<V> iterator = collection.iterator();
1009 Collection<V> collection = map.get(entry.getElement());
1010 return (collection != null) &&
1011 (collection.size() == entry.getCount());
1030 Collection<V> collection = map.get(key);
1031 return (collection == null) ? 0 : collection.size();
1053 Collection<V> collection;
1055 collection = map.remove(key);
1063 if (collection != null) {
1064 count = collection.size();
1065 collection.clear();
1073 final Iterator<Map.Entry<K, Collection<V>>> asMapIterator
1088 final Map.Entry<K, Collection<V>> entry;
1090 public MultisetEntry(Map.Entry<K, Collection<V>> entry) {
1116 private transient Collection<V> valuesCollection;
1121 * <p>The iterator generated by the returned collection traverses the values
1124 public Collection<V> values() {
1125 Collection<V> result = valuesCollection;
1163 private transient Collection<Map.Entry<K, V>> entries;
1172 * <p>The iterator generated by the returned collection traverses the values
1177 * collection or its iterator.
1179 public Collection<Map.Entry<K, V>> entries() {
1180 Collection<Map.Entry<K, V>> result = entries;
1184 private Collection<Map.Entry<K, V>> createEntries() {
1235 final Iterator<Map.Entry<K, Collection<V>>> keyIterator;
1237 Collection<V> collection;
1250 Map.Entry<K, Collection<V>> entry = keyIterator.next();
1252 collection = entry.getValue();
1253 valueIterator = collection.iterator();
1269 if (collection.isEmpty()) {
1286 private transient Map<K, Collection<V>> asMap;
1288 public Map<K, Collection<V>> asMap() {
1289 Map<K, Collection<V>> result = asMap;
1293 private Map<K, Collection<V>> createAsMap() {
1295 ? new SortedAsMap((SortedMap<K, Collection<V>>) map) : new AsMap(map);
1298 private class AsMap extends AbstractMap<K, Collection<V>> {
1303 final transient Map<K, Collection<V>> submap;
1305 AsMap(Map<K, Collection<V>> submap) {
1309 transient Set<Map.Entry<K, Collection<V>>> entrySet;
1311 @Override public Set<Map.Entry<K, Collection<V>>> entrySet() {
1312 Set<Map.Entry<K, Collection<V>>> result = entrySet;
1322 @Override public Collection<V> get(Object key) {
1323 Collection<V> collection = Maps.safeGet(submap, key);
1324 if (collection == null) {
1329 return wrapCollection(k, collection);
1336 @Override public Collection<V> remove(Object key) {
1337 Collection<V> collection = submap.remove(key);
1338 if (collection == null) {
1342 Collection<V> output = createCollection();
1343 output.addAll(collection);
1344 totalSize -= collection.size();
1345 collection.clear();
1361 class AsMapEntries extends AbstractSet<Map.Entry<K, Collection<V>>> {
1362 @Override public Iterator<Map.Entry<K, Collection<V>>> iterator() {
1387 class AsMapIterator implements Iterator<Map.Entry<K, Collection<V>>> {
1388 final Iterator<Map.Entry<K, Collection<V>>> delegateIterator
1390 Collection<V> collection;
1396 public Map.Entry<K, Collection<V>> next() {
1397 Map.Entry<K, Collection<V>> entry = delegateIterator.next();
1399 collection = entry.getValue();
1400 return Maps.immutableEntry(key, wrapCollection(key, collection));
1405 totalSize -= collection.size();
1406 collection.clear();
1412 implements SortedMap<K, Collection<V>> {
1413 SortedAsMap(SortedMap<K, Collection<V>> submap) {
1417 SortedMap<K, Collection<V>> sortedMap() {
1418 return (SortedMap<K, Collection<V>>) submap;
1433 public SortedMap<K, Collection<V>> headMap(K toKey) {
1437 public SortedMap<K, Collection<V>> subMap(K fromKey, K toKey) {
1441 public SortedMap<K, Collection<V>> tailMap(K fromKey) {