All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.Maps Maven / Gradle / Ivy

Go to download

Google Collections Library is a suite of new collections and collection-related goodness for Java 5.0

There is a newer version: 1.0
Show newest version
/*
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import com.google.common.base.Joiner.MapJoiner;
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.annotation.Nullable;

/**
 * Static utility methods pertaining to {@link Map} instances. Also see this
 * class's counterparts {@link Lists} and {@link Sets}.
 *
 * @author Kevin Bourrillion
 * @author Mike Bostock
 * @author Isaac Shum
 */
@GwtCompatible
public final class Maps {
  private Maps() {}

  /**
   * Creates a mutable, empty {@code HashMap} instance.
   *
   * 

Note: if mutability is not required, use {@link * ImmutableMap#of()} instead. * *

Note: if {@code K} is an {@code enum} type, use {@link * #newEnumMap} instead. * * @return a new, empty {@code HashMap} */ public static HashMap newHashMap() { return new HashMap(); } /** * Creates a {@code HashMap} instance with enough capacity to hold the * specified number of elements without rehashing. * * @param expectedSize the expected size * @return a new, empty {@code HashMap} with enough * capacity to hold {@code expectedSize} elements without rehashing * @throws IllegalArgumentException if {@code expectedSize} is negative */ public static HashMap newHashMapWithExpectedSize( int expectedSize) { /* * The HashMap is constructed with an initialCapacity that's greater than * expectedSize. The larger value is necessary because HashMap resizes * its internal array if the map size exceeds loadFactor * initialCapacity. */ return new HashMap(capacity(expectedSize)); } /** * Returns an appropriate value for the "capacity" (in reality, "minimum * table size") parameter of a {@link HashMap} constructor, such that the * resulting table will be between 25% and 50% full when it contains * {@code expectedSize} entries. * * @throws IllegalArgumentException if {@code expectedSize} is negative */ static int capacity(int expectedSize) { checkArgument(expectedSize >= 0); return Math.max(expectedSize * 2, 16); } /** * Creates a mutable {@code HashMap} instance with the same mappings as * the specified map. * *

Note: if mutability is not required, use {@link * ImmutableMap#copyOf(Map)} instead. * *

Note: if {@code K} is an {@link Enum} type, use {@link * #newEnumMap} instead. * * @param map the mappings to be placed in the new map * @return a new {@code HashMap} initialized with the mappings from * {@code map} */ public static HashMap newHashMap( Map map) { return new HashMap(map); } /** * Creates a mutable, empty, insertion-ordered {@code LinkedHashMap} * instance. * *

Note: if mutability is not required, use {@link * ImmutableMap#of()} instead. * * @return a new, empty {@code LinkedHashMap} */ public static LinkedHashMap newLinkedHashMap() { return new LinkedHashMap(); } /** * Creates a mutable, insertion-ordered {@code LinkedHashMap} instance * with the same mappings as the specified map. * *

Note: if mutability is not required, use {@link * ImmutableMap#copyOf(Map)} instead. * * @param map the mappings to be placed in the new map * @return a new, {@code LinkedHashMap} initialized with the * mappings from {@code map} */ public static LinkedHashMap newLinkedHashMap(Map map) { return new LinkedHashMap(map); } /** * Creates a mutable, empty {@code TreeMap} instance using the natural * ordering of its elements. * *

Note: if mutability is not required, use {@link * ImmutableSortedMap#of()} instead. * * @return a new, empty {@code TreeMap} */ @SuppressWarnings("unchecked") // eclipse doesn't like the raw Comparable public static TreeMap newTreeMap() { return new TreeMap(); } /** * Creates a mutable {@code TreeMap} instance with the same mappings as * the specified map and using the same ordering as the specified map. * *

Note: if mutability is not required, use {@link * ImmutableSortedMap#copyOfSorted(SortedMap)} instead. * * @param map the sorted map whose mappings are to be placed in the new map * and whose comparator is to be used to sort the new map * @return a new {@code TreeMap} initialized with the mappings from {@code * map} and using the comparator of {@code map} */ public static TreeMap newTreeMap(SortedMap map) { return new TreeMap(map); } /** * Creates a mutable, empty {@code TreeMap} instance using the given * comparator. * *

Note: if mutability is not required, use {@code * ImmutableSortedMap.orderedBy(comparator).build()} instead. * * @param comparator the comparator to sort the keys with * @return a new, empty {@code TreeMap} */ public static TreeMap newTreeMap( @Nullable Comparator comparator) { // Ideally, the extra type parameter "C" shouldn't be necessary. It is a // work-around of a compiler type inference quirk that prevents the // following code from being compiled: // Comparator> comparator = null; // Map, String> map = newTreeMap(comparator); return new TreeMap(comparator); } /** * Creates an {@code EnumMap} instance. * * @param type the key type for this map * @return a new, empty {@code EnumMap} */ public static , V> EnumMap newEnumMap(Class type) { return new EnumMap(checkNotNull(type)); } /** * Creates an {@code EnumMap} with the same mappings as the specified map. * * @param map the map from which to initialize this {@code EnumMap} * @return a new {@code EnumMap} initialized with the mappings from {@code * map} * @throws IllegalArgumentException if {@code m} is not an {@code EnumMap} * instance and contains no mappings */ public static , V> EnumMap newEnumMap( Map map) { return new EnumMap(map); } /** * Creates an {@code IdentityHashMap} instance. * * @return a new, empty {@code IdentityHashMap} */ public static IdentityHashMap newIdentityHashMap() { return new IdentityHashMap(); } /** * Returns a synchronized (thread-safe) bimap backed by the specified bimap. * In order to guarantee serial access, it is critical that all access * to the backing bimap is accomplished through the returned bimap. * *

It is imperative that the user manually synchronize on the returned map * when accessing any of its collection views:

   {@code
   *
   *   BiMap map = Maps.synchronizedBiMap(
   *       HashBiMap.create());
   *    ...
   *   Set set = map.keySet();  // Needn't be in synchronized block
   *    ...
   *   synchronized (map) {  // Synchronizing on map, not set!
   *     Iterator it = set.iterator(); // Must be in synchronized block
   *     while (it.hasNext()) {
   *       foo(it.next());
   *     }
   *   }}
* * Failure to follow this advice may result in non-deterministic behavior. * *

The returned bimap will be serializable if the specified bimap is * serializable. * * @param bimap the bimap to be wrapped in a synchronized view * @return a sychronized view of the specified bimap */ public static BiMap synchronizedBiMap(BiMap bimap) { return Synchronized.biMap(bimap, null); } /** * Computes the difference between two maps. This difference is an immutable * snapshot of the state of the maps at the time this method is called. It * will never change, even if the maps change at a later time. * *

Since this method uses {@code HashMap} instances internally, the keys of * the supplied maps must be well-behaved with respect to * {@link Object#equals} and {@link Object#hashCode}. * *

Note:If you only need to know whether two maps have the same * mappings, call {@code left.equals(right)} instead of this method. * * @param left the map to treat as the "left" map for purposes of comparison * @param right the map to treat as the "right" map for purposes of comparison * @return the difference between the two maps */ public static MapDifference difference( Map left, Map right) { Map onlyOnLeft = newHashMap(); Map onlyOnRight = new HashMap(right); // will whittle it down Map onBoth = newHashMap(); Map> differences = newHashMap(); boolean eq = true; for (Entry entry : left.entrySet()) { K leftKey = entry.getKey(); V leftValue = entry.getValue(); if (right.containsKey(leftKey)) { V rightValue = onlyOnRight.remove(leftKey); if (Objects.equal(leftValue, rightValue)) { onBoth.put(leftKey, leftValue); } else { eq = false; differences.put(leftKey, new ValueDifferenceImpl( leftValue, rightValue)); } } else { eq = false; onlyOnLeft.put(leftKey, leftValue); } } boolean areEqual = eq && onlyOnRight.isEmpty(); return new MapDifferenceImpl( areEqual, onlyOnLeft, onlyOnRight, onBoth, differences); } private static class MapDifferenceImpl implements MapDifference { final boolean areEqual; final Map onlyOnLeft; final Map onlyOnRight; final Map onBoth; final Map> differences; MapDifferenceImpl(boolean areEqual, Map onlyOnLeft, Map onlyOnRight, Map onBoth, Map> differences) { this.areEqual = areEqual; this.onlyOnLeft = Collections.unmodifiableMap(onlyOnLeft); this.onlyOnRight = Collections.unmodifiableMap(onlyOnRight); this.onBoth = Collections.unmodifiableMap(onBoth); this.differences = Collections.unmodifiableMap(differences); } public boolean areEqual() { return areEqual; } public Map entriesOnlyOnLeft() { return onlyOnLeft; } public Map entriesOnlyOnRight() { return onlyOnRight; } public Map entriesInCommon() { return onBoth; } public Map> entriesDiffering() { return differences; } @Override public boolean equals(Object object) { if (object == this) { return true; } if (object instanceof MapDifference) { MapDifference other = (MapDifference) object; return entriesOnlyOnLeft().equals(other.entriesOnlyOnLeft()) && entriesOnlyOnRight().equals(other.entriesOnlyOnRight()) && entriesInCommon().equals(other.entriesInCommon()) && entriesDiffering().equals(other.entriesDiffering()); } return false; } @Override public int hashCode() { return Objects.hashCode(entriesOnlyOnLeft(), entriesOnlyOnRight(), entriesInCommon(), entriesDiffering()); } @Override public String toString() { if (areEqual) { return "equal"; } StringBuilder result = new StringBuilder("not equal"); if (!onlyOnLeft.isEmpty()) { result.append(": only on left=").append(onlyOnLeft); } if (!onlyOnRight.isEmpty()) { result.append(": only on right=").append(onlyOnRight); } if (!differences.isEmpty()) { result.append(": value differences=").append(differences); } return result.toString(); } } static class ValueDifferenceImpl implements MapDifference.ValueDifference { private final V left; private final V right; ValueDifferenceImpl(@Nullable V left, @Nullable V right) { this.left = left; this.right = right; } public V leftValue() { return left; } public V rightValue() { return right; } @Override public boolean equals(@Nullable Object object) { if (object instanceof MapDifference.ValueDifference) { MapDifference.ValueDifference that = (MapDifference.ValueDifference) object; return Objects.equal(this.left, that.leftValue()) && Objects.equal(this.right, that.rightValue()); } return false; } @Override public int hashCode() { return Objects.hashCode(left, right); } @Override public String toString() { return "(" + left + ", " + right + ")"; } } /** * Returns an immutable map for which the {@link Map#values} are the given * elements in the given order, and each key is the product of invoking a * supplied function on its corresponding value. * * @param values the values to use when constructing the {@code Map} * @param keyFunction the function used to produce the key for each value * @return a map mapping the result of evaluating the function {@code * keyFunction} on each value in the input collection to that value * @throws IllegalArgumentException if {@code keyFunction} produces the same * key for more than one value in the input collection * @throws NullPointerException if any elements of {@code values} is null, or * if {@code keyFunction} produces {@code null} for any value */ // TODO: consider returning a bimap, whose inverse view does lookups by // invoking the function. public static ImmutableMap uniqueIndex( Iterable values, Function keyFunction) { checkNotNull(keyFunction); ImmutableMap.Builder builder = ImmutableMap.builder(); for (V value : values) { builder.put(keyFunction.apply(value), value); } return builder.build(); } /** * Creates an {@code ImmutableMap} from a {@code Properties} * instance. Properties normally derive from {@code Map}, but * they typically contain strings, which is awkward. This method lets you get * a plain-old-{@code Map} out of a {@code Properties}. * * @param properties a {@code Properties} object to be converted * @return an immutable map containing all the entries in * {@code properties} * @throws ClassCastException if any key in {@code Properties} is not a * {@code String} * @throws NullPointerException if any key or value in {@code Properties} is * null. */ public static ImmutableMap fromProperties(Properties properties) { ImmutableMap.Builder builder = ImmutableMap.builder(); for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) { String key = (String) e.nextElement(); builder.put(key, properties.getProperty(key)); } return builder.build(); } /** * Returns an immutable map entry with the specified key and value. The {@link * Entry#setValue} operation throws an {@link UnsupportedOperationException}. * *

The returned entry is serializable. * * @param key the key to be associated with the returned entry * @param value the value to be associated with the returned entry */ public static Entry immutableEntry( @Nullable final K key, @Nullable final V value) { return new ImmutableEntry(key, value); } /** * Returns an unmodifiable view of the specified set of entries. The {@link * Entry#setValue} operation throws an {@link UnsupportedOperationException}, * as do any operations that would modify the returned set. * * @param entrySet the entries for which to return an unmodifiable view * @return an unmodifiable view of the entries */ static Set> unmodifiableEntrySet( final Set> entrySet) { return new UnmodifiableEntrySet(Collections.unmodifiableSet( entrySet)); } /** * Returns an unmodifiable view of the specified map entry. The {@link * Entry#setValue} operation throws an {@link UnsupportedOperationException}. * This also has the side-effect of redefining {@code equals} to comply with * the Entry contract, to avoid a possible nefarious implementation of * equals. * * @param entry the entry for which to return an unmodifiable view * @return an unmodifiable view of the entry */ private static Entry unmodifiableEntry(final Entry entry) { checkNotNull(entry); return new AbstractMapEntry() { @Override public K getKey() { return entry.getKey(); } @Override public V getValue() { return entry.getValue(); } }; } /** @see Multimaps#unmodifiableEntries */ static class UnmodifiableEntries extends ForwardingCollection> { private final Collection> entries; UnmodifiableEntries(Collection> entries) { this.entries = entries; } @Override protected Collection> delegate() { return entries; } @Override public Iterator> iterator() { final Iterator> delegate = super.iterator(); return new ForwardingIterator>() { @Override public Entry next() { return unmodifiableEntry(super.next()); } @Override protected Iterator> delegate() { return delegate; } }; } // See java.util.Collections.UnmodifiableEntrySet for details on attacks. @Override public Object[] toArray() { return ObjectArrays.toArrayImpl(this); } @Override public T[] toArray(T[] array) { return ObjectArrays.toArrayImpl(this, array); } @Override public boolean contains(Object o) { return containsEntryImpl(delegate(), o); } @Override public boolean containsAll(Collection c) { return Collections2.containsAll(this, c); } } /** @see Maps#unmodifiableEntrySet(Set) */ static class UnmodifiableEntrySet extends UnmodifiableEntries implements Set> { UnmodifiableEntrySet(Set> entries) { super(entries); } // See java.util.Collections.UnmodifiableEntrySet for details on attacks. @Override public boolean equals(@Nullable Object object) { return Collections2.setEquals(this, object); } @Override public int hashCode() { return Sets.hashCodeImpl(this); } } /** * Returns an unmodifiable view of the specified bimap. This method allows * modules to provide users with "read-only" access to internal bimaps. Query * operations on the returned bimap "read through" to the specified bimap, and * attemps to modify the returned map, whether direct or via its collection * views, result in an {@code UnsupportedOperationException}. * *

The returned bimap will be serializable if the specified bimap is * serializable. * * @param bimap the bimap for which an unmodifiable view is to be returned * @return an unmodifiable view of the specified bimap */ public static BiMap unmodifiableBiMap( BiMap bimap) { return new UnmodifiableBiMap(bimap, null); } /** @see Maps#unmodifiableBiMap(BiMap) */ private static class UnmodifiableBiMap extends ForwardingMap implements BiMap, Serializable { final Map unmodifiableMap; final BiMap delegate; transient BiMap inverse; transient Set values; UnmodifiableBiMap(BiMap delegate, @Nullable BiMap inverse) { unmodifiableMap = Collections.unmodifiableMap(delegate); this.delegate = delegate; this.inverse = inverse; } @Override protected Map delegate() { return unmodifiableMap; } public V forcePut(K key, V value) { throw new UnsupportedOperationException(); } public BiMap inverse() { BiMap result = inverse; return (result == null) ? inverse = new UnmodifiableBiMap(delegate.inverse(), this) : result; } @Override public Set values() { Set result = values; return (result == null) ? values = Collections.unmodifiableSet(delegate.values()) : result; } private static final long serialVersionUID = 0; } /** * Implements {@code Collection.contains} safely for forwarding collections of * map entries. If {@code o} is an instance of {@code Map.Entry}, it is * wrapped using {@link #unmodifiableEntry} to protect against a possible * nefarious equals method. * *

Note that {@code c} is the backing (delegate) collection, rather than * the forwarding collection. * * @param c the delegate (unwrapped) collection of map entries * @param o the object that might be contained in {@code c} * @return {@code true} if {@code c} contains {@code o} */ static boolean containsEntryImpl(Collection> c, Object o) { if (!(o instanceof Entry)) { return false; } return c.contains(unmodifiableEntry((Entry) o)); } /** * Implements {@code Collection.remove} safely for forwarding collections of * map entries. If {@code o} is an instance of {@code Map.Entry}, it is * wrapped using {@link #unmodifiableEntry} to protect against a possible * nefarious equals method. * *

Note that {@code c} is backing (delegate) collection, rather than the * forwarding collection. * * @param c the delegate (unwrapped) collection of map entries * @param o the object to remove from {@code c} * @return {@code true} if {@code c} was changed */ static boolean removeEntryImpl(Collection> c, Object o) { if (!(o instanceof Entry)) { return false; } return c.remove(unmodifiableEntry((Entry) o)); } /** * Returns a view of a map where each value is transformed by a function. All * other properties of the map, such as iteration order, are left intact. For * example, the code: *

   {@code
   *
   *   Map map = ImmutableMap.of("a", 4, "b", 9);
   *   Function sqrt = new Function() {
   *     public Double apply(Integer in) {
   *       return Math.sqrt((int) in);
   *     }
   *   };
   *   Map transformed = Maps.transformValues(sqrt, map);
   *   System.out.println(transformed);}
* * ... prints {@code {a=2.0, b=3.0}}. * *

Changes in the underlying map are reflected in this view. Conversely, * this view supports removal operations, and these are reflected in the * underlying map. * *

It's acceptable for the underlying map to contain null keys, and even * null values provided that the function is capable of accepting null input. * The transformed map might contain null values, if the function sometimes * gives a null result. * *

The returned map is not thread-safe or serializable, even if the * underlying map is. * *

The function is applied lazily, invoked when needed. This is necessary * for the returned map to be a view, but it means that the function will be * applied many times for bulk operations like {@link Map#containsValue} and * {@code Map.toString()}. For this to perform well, {@code function} should * be fast. To avoid lazy evaluation when the returned map doesn't need to be * a view, copy the returned map into a new map of your choosing. */ public static Map transformValues( Map fromMap, Function function) { return new TransformedValuesMap(fromMap, function); } private static class TransformedValuesMap extends AbstractMap { final Map fromMap; final Function function; TransformedValuesMap(Map fromMap, Function function) { this.fromMap = checkNotNull(fromMap); this.function = checkNotNull(function); } @Override public int size() { return fromMap.size(); } @Override public boolean containsKey(Object key) { return fromMap.containsKey(key); } @Override public V2 get(Object key) { V1 value = fromMap.get(key); return (value != null || fromMap.containsKey(key)) ? function.apply(value) : null; } @Override public V2 remove(Object key) { return fromMap.containsKey(key) ? function.apply(fromMap.remove(key)) : null; } @Override public void clear() { fromMap.clear(); } volatile EntrySet entrySet; @Override public Set> entrySet() { if (entrySet == null) { entrySet = new EntrySet(); } return entrySet; } class EntrySet extends AbstractSet> { @Override public int size() { return TransformedValuesMap.this.size(); } @Override public Iterator> iterator() { final Iterator> mapIterator = fromMap.entrySet().iterator(); return new Iterator>() { public boolean hasNext() { return mapIterator.hasNext(); } public Entry next() { final Entry entry = mapIterator.next(); return new AbstractMapEntry() { @Override public K getKey() { return entry.getKey(); } @Override public V2 getValue() { return function.apply(entry.getValue()); } }; } public void remove() { mapIterator.remove(); } }; } @Override public void clear() { fromMap.clear(); } @Override public boolean contains(Object o) { if (!(o instanceof Entry)) { return false; } Entry entry = (Entry) o; Object entryKey = entry.getKey(); Object entryValue = entry.getValue(); V2 mapValue = TransformedValuesMap.this.get(entryKey); if (mapValue != null) { return mapValue.equals(entryValue); } return entryValue == null && containsKey(entryKey); } @Override public boolean remove(Object o) { if (contains(o)) { Entry entry = (Entry) o; Object key = entry.getKey(); fromMap.remove(key); return true; } return false; } } } /** * Returns a map containing the mappings in {@code unfiltered} whose keys * satisfy a predicate. The returned map is a live view of {@code unfiltered}; * changes to one affect the other. * *

The resulting map's {@code keySet()}, {@code entrySet()}, and {@code * values()} views have iterators that don't support {@code remove()}, but all * other methods are supported by the map and its views. The map's {@code * put()} and {@code putAll()} methods throw an {@link * IllegalArgumentException} if a key that doesn't satisfy the predicate is * provided. * *

When methods such as {@code removeAll()} and {@code clear()} are called * on the filtered map or its views, only mappings whose keys satisfy the * filter will be removed from the underlying map. * *

The returned map isn't threadsafe or serializable, even if {@code * unfiltered} is. * *

Many of the filtered map's methods, such as {@code size()}, * iterate across every key/value mapping in the underlying map and determine * which satisfy the filter. When a live view is not needed, it may be * faster to copy the filtered map and use the copy. */ public static Map filterKeys( Map unfiltered, final Predicate keyPredicate) { checkNotNull(keyPredicate); Predicate> entryPredicate = new Predicate>() { public boolean apply(Entry input) { return keyPredicate.apply(input.getKey()); } }; return (unfiltered instanceof AbstractFilteredMap) ? filterFiltered((AbstractFilteredMap) unfiltered, entryPredicate) : new FilteredKeyMap( checkNotNull(unfiltered), keyPredicate, entryPredicate); } /** * Returns a map containing the mappings in {@code unfiltered} whose values * satisfy a predicate. The returned map is a live view of {@code unfiltered}; * changes to one affect the other. * *

The resulting map's {@code keySet()}, {@code entrySet()}, and {@code * values()} views have iterators that don't support {@code remove()}, but all * other methods are supported by the map and its views. The {@link Map#put}, * {@link Map#putAll}, and {@link Entry#setValue} methods throw an {@link * IllegalArgumentException} if a value that doesn't satisfy the predicate is * provided. * *

When methods such as {@code removeAll()} and {@code clear()} are called * on the filtered map or its views, only mappings whose values satisfy the * filter will be removed from the underlying map. * *

The returned map isn't threadsafe or serializable, even if {@code * unfiltered} is. * *

Many of the filtered map's methods, such as {@code size()}, * iterate across every key/value mapping in the underlying map and determine * which satisfy the filter. When a live view is not needed, it may be * faster to copy the filtered map and use the copy. */ public static Map filterValues( Map unfiltered, final Predicate valuePredicate) { checkNotNull(valuePredicate); Predicate> entryPredicate = new Predicate>() { public boolean apply(Entry input) { return valuePredicate.apply(input.getValue()); } }; return filterEntries(unfiltered, entryPredicate); } /** * Returns a map containing the mappings in {@code unfiltered} that satisfy a * predicate. The returned map is a live view of {@code unfiltered}; changes * to one affect the other. * *

The resulting map's {@code keySet()}, {@code entrySet()}, and {@code * values()} views have iterators that don't support {@code remove()}, but all * other methods are supported by the map and its views. The map's {@code * put()} and {@code putAll()} methods throw an {@link * IllegalArgumentException} if a key/value pair that doesn't satisfy the * predicate is provided. Similarly, the map's entries have a {@link * Entry#setValue} method that throws an {@link IllegalArgumentException} when * the existing key and the provided value don't satisfy the predicate. * *

When methods such as {@code removeAll()} and {@code clear()} are called * on the filtered map or its views, only mappings that satisfy the filter * will be removed from the underlying map. * *

The returned map isn't threadsafe or serializable, even if {@code * unfiltered} is. * *

Many of the filtered map's methods, such as {@code size()}, * iterate across every key/value mapping in the underlying map and determine * which satisfy the filter. When a live view is not needed, it may be * faster to copy the filtered map and use the copy. */ public static Map filterEntries( Map unfiltered, Predicate> entryPredicate) { checkNotNull(entryPredicate); return (unfiltered instanceof AbstractFilteredMap) ? filterFiltered((AbstractFilteredMap) unfiltered, entryPredicate) : new FilteredEntryMap(checkNotNull(unfiltered), entryPredicate); } /** * Support {@code clear()}, {@code removeAll()}, and {@code retainAll()} when * filtering a filtered map. */ private static Map filterFiltered( AbstractFilteredMap map, Predicate> entryPredicate) { Predicate> predicate = Predicates.and(map.predicate, entryPredicate); return new FilteredEntryMap(map.unfiltered, predicate); } private static abstract class AbstractFilteredMap extends AbstractMap { final Map unfiltered; final Predicate> predicate; AbstractFilteredMap(Map unfiltered, Predicate> predicate) { this.unfiltered = unfiltered; this.predicate = predicate; } boolean apply(Object key, V value) { // This method is called only when the key is in the map, implying that // key is a K. @SuppressWarnings("unchecked") K k = (K) key; return predicate.apply(Maps.immutableEntry(k, value)); } @Override public V put(K key, V value) { checkArgument(apply(key, value)); return unfiltered.put(key, value); } @Override public void putAll(Map map) { for (Entry entry : map.entrySet()) { checkArgument(apply(entry.getKey(), entry.getValue())); } unfiltered.putAll(map); } @Override public boolean containsKey(Object key) { return unfiltered.containsKey(key) && apply(key, unfiltered.get(key)); } @Override public V get(Object key) { V value = unfiltered.get(key); return ((value != null) && apply(key, value)) ? value : null; } @Override public boolean isEmpty() { return entrySet().isEmpty(); } @Override public V remove(Object key) { return containsKey(key) ? unfiltered.remove(key) : null; } Collection values; @Override public Collection values() { Collection result = values; return (result == null) ? values = new Values() : values; } class Values extends AbstractCollection { @Override public Iterator iterator() { final Iterator> entryIterator = entrySet().iterator(); return new UnmodifiableIterator() { public boolean hasNext() { return entryIterator.hasNext(); } public V next() { return entryIterator.next().getValue(); } }; } @Override public int size() { return entrySet().size(); } @Override public void clear() { entrySet().clear(); } @Override public boolean isEmpty() { return entrySet().isEmpty(); } @Override public boolean remove(Object o) { Iterator> iterator = unfiltered.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); if (Objects.equal(o, entry.getValue()) && predicate.apply(entry)) { iterator.remove(); return true; } } return false; } @Override public boolean removeAll(Collection collection) { checkNotNull(collection); boolean changed = false; Iterator> iterator = unfiltered.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); if (collection.contains(entry.getValue()) && predicate.apply(entry)) { iterator.remove(); changed = true; } } return changed; } @Override public boolean retainAll(Collection collection) { checkNotNull(collection); boolean changed = false; Iterator> iterator = unfiltered.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); if (!collection.contains(entry.getValue()) && predicate.apply(entry)) { iterator.remove(); changed = true; } } return changed; } @Override public Object[] toArray() { // creating an ArrayList so filtering happens once return Lists.newArrayList(iterator()).toArray(); } @Override public T[] toArray(T[] array) { return Lists.newArrayList(iterator()).toArray(array); } } } private static class FilteredKeyMap extends AbstractFilteredMap { Predicate keyPredicate; FilteredKeyMap(Map unfiltered, Predicate keyPredicate, Predicate> entryPredicate) { super(unfiltered, entryPredicate); this.keyPredicate = keyPredicate; } Set> entrySet; @Override public Set> entrySet() { Set> result = entrySet; return (result == null) ? entrySet = Sets.filter(unfiltered.entrySet(), predicate) : result; } Set keySet; @Override public Set keySet() { Set result = keySet; return (result == null) ? keySet = Sets.filter(unfiltered.keySet(), keyPredicate) : result; } // The cast is called only when the key is in the unfiltered map, implying // that key is a K. @SuppressWarnings("unchecked") @Override public boolean containsKey(Object key) { return unfiltered.containsKey(key) && keyPredicate.apply((K) key); } } private static class FilteredEntryMap extends AbstractFilteredMap { /** * Entries in this set satisfy the predicate, but they don't validate the * input to {@code Entry.setValue()}. */ final Set> filteredEntrySet; FilteredEntryMap(Map unfiltered, Predicate> entryPredicate) { super(unfiltered, entryPredicate); filteredEntrySet = Sets.filter(unfiltered.entrySet(), predicate); } Set> entrySet; @Override public Set> entrySet() { Set> result = entrySet; return (result == null) ? entrySet = new EntrySet() : result; } private class EntrySet extends ForwardingSet> { @Override protected Set> delegate() { return filteredEntrySet; } @Override public Iterator> iterator() { final Iterator> iterator = filteredEntrySet.iterator(); return new UnmodifiableIterator>() { public boolean hasNext() { return iterator.hasNext(); } public Entry next() { final Entry entry = iterator.next(); return new ForwardingMapEntry() { @Override protected Entry delegate() { return entry; } @Override public V setValue(V value) { checkArgument(apply(entry.getKey(), value)); return super.setValue(value); } }; } }; } } Set keySet; @Override public Set keySet() { Set result = keySet; return (result == null) ? keySet = new KeySet() : result; } private class KeySet extends AbstractSet { @Override public Iterator iterator() { final Iterator> iterator = filteredEntrySet.iterator(); return new UnmodifiableIterator() { public boolean hasNext() { return iterator.hasNext(); } public K next() { return iterator.next().getKey(); } }; } @Override public int size() { return filteredEntrySet.size(); } @Override public void clear() { filteredEntrySet.clear(); } @Override public boolean contains(Object o) { return containsKey(o); } @Override public boolean remove(Object o) { if (containsKey(o)) { unfiltered.remove(o); return true; } return false; } @Override public boolean removeAll(Collection collection) { checkNotNull(collection); // for GWT boolean changed = false; for (Object obj : collection) { changed |= remove(obj); } return changed; } @Override public boolean retainAll(Collection collection) { checkNotNull(collection); // for GWT boolean changed = false; Iterator> iterator = unfiltered.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); if (!collection.contains(entry.getKey()) && predicate.apply(entry)) { iterator.remove(); changed = true; } } return changed; } @Override public Object[] toArray() { // creating an ArrayList so filtering happens once return Lists.newArrayList(iterator()).toArray(); } @Override public T[] toArray(T[] array) { return Lists.newArrayList(iterator()).toArray(array); } } } /** * {@code AbstractMap} extension that implements {@link #isEmpty()} as {@code * entrySet().isEmpty()} instead of {@code size() == 0} to speed up * implementations where {@code size()} is O(n), and it delegates the {@code * isEmpty()} methods of its key set and value collection to this * implementation. */ @GwtCompatible abstract static class ImprovedAbstractMap extends AbstractMap { /** * Creates the entry set to be returned by {@link #entrySet()}. This method * is invoked at most once on a given map, at the time when {@code * entrySet} is first called. */ protected abstract Set> createEntrySet(); private transient Set> entrySet; @Override public synchronized Set> entrySet() { if (entrySet == null) { entrySet = createEntrySet(); } return entrySet; } private transient Set keySet; @Override public synchronized Set keySet() { if (keySet == null) { final Set delegate = super.keySet(); keySet = new ForwardingSet() { @Override protected Set delegate() { return delegate; } @Override public boolean isEmpty() { return ImprovedAbstractMap.this.isEmpty(); } }; } return keySet; } private transient Collection values; @Override public synchronized Collection values() { if (values == null) { final Collection delegate = super.values(); values = new ForwardingCollection() { @Override protected Collection delegate() { return delegate; } @Override public boolean isEmpty() { return ImprovedAbstractMap.this.isEmpty(); } }; } return values; } /** * Returns {@code true} if this map contains no key-value mappings. * *

The implementation returns {@code entrySet().isEmpty()}. * * @return {@code true} if this map contains no key-value mappings */ @Override public boolean isEmpty() { return entrySet().isEmpty(); } } static final MapJoiner standardJoiner = Collections2.standardJoiner.withKeyValueSeparator("="); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy