001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static com.google.common.base.Predicates.compose;
022import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
023import static com.google.common.collect.CollectPreconditions.checkNonnegative;
024
025import com.google.common.annotations.Beta;
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.annotations.GwtIncompatible;
028import com.google.common.base.Converter;
029import com.google.common.base.Equivalence;
030import com.google.common.base.Function;
031import com.google.common.base.Objects;
032import com.google.common.base.Preconditions;
033import com.google.common.base.Predicate;
034import com.google.common.base.Predicates;
035import com.google.common.collect.MapDifference.ValueDifference;
036import com.google.common.primitives.Ints;
037import com.google.errorprone.annotations.CanIgnoreReturnValue;
038import com.google.j2objc.annotations.RetainedWith;
039import com.google.j2objc.annotations.Weak;
040import com.google.j2objc.annotations.WeakOuter;
041import java.io.Serializable;
042import java.util.AbstractCollection;
043import java.util.AbstractMap;
044import java.util.Collection;
045import java.util.Collections;
046import java.util.Comparator;
047import java.util.EnumMap;
048import java.util.Enumeration;
049import java.util.HashMap;
050import java.util.IdentityHashMap;
051import java.util.Iterator;
052import java.util.LinkedHashMap;
053import java.util.Map;
054import java.util.Map.Entry;
055import java.util.NavigableMap;
056import java.util.NavigableSet;
057import java.util.Properties;
058import java.util.Set;
059import java.util.SortedMap;
060import java.util.SortedSet;
061import java.util.TreeMap;
062import java.util.concurrent.ConcurrentHashMap;
063import java.util.concurrent.ConcurrentMap;
064import javax.annotation.Nullable;
065
066/**
067 * Static utility methods pertaining to {@link Map} instances (including instances of
068 * {@link SortedMap}, {@link BiMap}, etc.). Also see this class's counterparts
069 * {@link Lists}, {@link Sets} and {@link Queues}.
070 *
071 * <p>See the Guava User Guide article on <a href=
072 * "https://github.com/google/guava/wiki/CollectionUtilitiesExplained#maps">
073 * {@code Maps}</a>.
074 *
075 * @author Kevin Bourrillion
076 * @author Mike Bostock
077 * @author Isaac Shum
078 * @author Louis Wasserman
079 * @since 2.0
080 */
081@GwtCompatible(emulated = true)
082public final class Maps {
083  private Maps() {}
084
085  private enum EntryFunction implements Function<Entry<?, ?>, Object> {
086    KEY {
087      @Override
088      @Nullable
089      public Object apply(Entry<?, ?> entry) {
090        return entry.getKey();
091      }
092    },
093    VALUE {
094      @Override
095      @Nullable
096      public Object apply(Entry<?, ?> entry) {
097        return entry.getValue();
098      }
099    };
100  }
101
102  @SuppressWarnings("unchecked")
103  static <K> Function<Entry<K, ?>, K> keyFunction() {
104    return (Function) EntryFunction.KEY;
105  }
106
107  @SuppressWarnings("unchecked")
108  static <V> Function<Entry<?, V>, V> valueFunction() {
109    return (Function) EntryFunction.VALUE;
110  }
111
112  static <K, V> Iterator<K> keyIterator(Iterator<Entry<K, V>> entryIterator) {
113    return Iterators.transform(entryIterator, Maps.<K>keyFunction());
114  }
115
116  static <K, V> Iterator<V> valueIterator(Iterator<Entry<K, V>> entryIterator) {
117    return Iterators.transform(entryIterator, Maps.<V>valueFunction());
118  }
119
120  /**
121   * Returns an immutable map instance containing the given entries.
122   * Internally, the returned map will be backed by an {@link EnumMap}.
123   *
124   * <p>The iteration order of the returned map follows the enum's iteration
125   * order, not the order in which the elements appear in the given map.
126   *
127   * @param map the map to make an immutable copy of
128   * @return an immutable map containing those entries
129   * @since 14.0
130   */
131  @GwtCompatible(serializable = true)
132  @Beta
133  public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
134      Map<K, ? extends V> map) {
135    if (map instanceof ImmutableEnumMap) {
136      @SuppressWarnings("unchecked") // safe covariant cast
137      ImmutableEnumMap<K, V> result = (ImmutableEnumMap<K, V>) map;
138      return result;
139    }
140    Iterator<? extends Map.Entry<K, ? extends V>> entryItr = map.entrySet().iterator();
141    if (!entryItr.hasNext()) {
142      return ImmutableMap.of();
143    }
144    Map.Entry<K, ? extends V> entry1 = entryItr.next();
145    K key1 = entry1.getKey();
146    V value1 = entry1.getValue();
147    checkEntryNotNull(key1, value1);
148    Class<K> clazz = key1.getDeclaringClass();
149    EnumMap<K, V> enumMap = new EnumMap<K, V>(clazz);
150    enumMap.put(key1, value1);
151    while (entryItr.hasNext()) {
152      Entry<K, ? extends V> entry = entryItr.next();
153      K key = entry.getKey();
154      V value = entry.getValue();
155      checkEntryNotNull(key, value);
156      enumMap.put(key, value);
157    }
158    return ImmutableEnumMap.asImmutable(enumMap);
159  }
160
161  /**
162   * Creates a <i>mutable</i>, empty {@code HashMap} instance.
163   *
164   * <p><b>Note:</b> if mutability is not required, use {@link
165   * ImmutableMap#of()} instead.
166   *
167   * <p><b>Note:</b> if {@code K} is an {@code enum} type, use {@link
168   * #newEnumMap} instead.
169   *
170   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
171   * should be treated as deprecated. Instead, use the {@code HashMap}
172   * constructor directly, taking advantage of the new
173   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
174   *
175   * @return a new, empty {@code HashMap}
176   */
177  public static <K, V> HashMap<K, V> newHashMap() {
178    return new HashMap<K, V>();
179  }
180
181  /**
182   * Creates a {@code HashMap} instance, with a high enough "initial capacity"
183   * that it <i>should</i> hold {@code expectedSize} elements without growth.
184   * This behavior cannot be broadly guaranteed, but it is observed to be true
185   * for OpenJDK 1.7. It also can't be guaranteed that the method isn't
186   * inadvertently <i>oversizing</i> the returned map.
187   *
188   * @param expectedSize the number of entries you expect to add to the
189   *        returned map
190   * @return a new, empty {@code HashMap} with enough capacity to hold {@code
191   *         expectedSize} entries without resizing
192   * @throws IllegalArgumentException if {@code expectedSize} is negative
193   */
194  public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) {
195    return new HashMap<K, V>(capacity(expectedSize));
196  }
197
198  /**
199   * Returns a capacity that is sufficient to keep the map from being resized as long as it grows no
200   * larger than expectedSize and the load factor is ≥ its default (0.75).
201   */
202  static int capacity(int expectedSize) {
203    if (expectedSize < 3) {
204      checkNonnegative(expectedSize, "expectedSize");
205      return expectedSize + 1;
206    }
207    if (expectedSize < Ints.MAX_POWER_OF_TWO) {
208      // This is the calculation used in JDK8 to resize when a putAll
209      // happens; it seems to be the most conservative calculation we
210      // can make.  0.75 is the default load factor.
211      return (int) ((float) expectedSize / 0.75F + 1.0F);
212    }
213    return Integer.MAX_VALUE; // any large value
214  }
215
216  /**
217   * Creates a <i>mutable</i> {@code HashMap} instance with the same mappings as
218   * the specified map.
219   *
220   * <p><b>Note:</b> if mutability is not required, use {@link
221   * ImmutableMap#copyOf(Map)} instead.
222   *
223   * <p><b>Note:</b> if {@code K} is an {@link Enum} type, use {@link
224   * #newEnumMap} instead.
225   *
226   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
227   * should be treated as deprecated. Instead, use the {@code HashMap}
228   * constructor directly, taking advantage of the new
229   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
230   *
231   * @param map the mappings to be placed in the new map
232   * @return a new {@code HashMap} initialized with the mappings from {@code
233   *         map}
234   */
235  public static <K, V> HashMap<K, V> newHashMap(Map<? extends K, ? extends V> map) {
236    return new HashMap<K, V>(map);
237  }
238
239  /**
240   * Creates a <i>mutable</i>, empty, insertion-ordered {@code LinkedHashMap}
241   * instance.
242   *
243   * <p><b>Note:</b> if mutability is not required, use {@link
244   * ImmutableMap#of()} instead.
245   *
246   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
247   * should be treated as deprecated. Instead, use the {@code LinkedHashMap}
248   * constructor directly, taking advantage of the new
249   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
250   *
251   * @return a new, empty {@code LinkedHashMap}
252   */
253  public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() {
254    return new LinkedHashMap<K, V>();
255  }
256
257  /**
258   * Creates a {@code LinkedHashMap} instance, with a high enough
259   * "initial capacity" that it <i>should</i> hold {@code expectedSize}
260   * elements without growth. This behavior cannot be broadly guaranteed, but
261   * it is observed to be true for OpenJDK 1.7. It also can't be guaranteed
262   * that the method isn't inadvertently <i>oversizing</i> the returned map.
263   *
264   * @param expectedSize the number of entries you expect to add to the
265   *        returned map
266   * @return a new, empty {@code LinkedHashMap} with enough capacity to hold
267   *         {@code expectedSize} entries without resizing
268   * @throws IllegalArgumentException if {@code expectedSize} is negative
269   * @since 19.0
270   */
271  public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) {
272    return new LinkedHashMap<K, V>(capacity(expectedSize));
273  }
274
275  /**
276   * Creates a <i>mutable</i>, insertion-ordered {@code LinkedHashMap} instance
277   * with the same mappings as the specified map.
278   *
279   * <p><b>Note:</b> if mutability is not required, use {@link
280   * ImmutableMap#copyOf(Map)} instead.
281   *
282   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
283   * should be treated as deprecated. Instead, use the {@code LinkedHashMap}
284   * constructor directly, taking advantage of the new
285   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
286   *
287   * @param map the mappings to be placed in the new map
288   * @return a new, {@code LinkedHashMap} initialized with the mappings from
289   *         {@code map}
290   */
291  public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(Map<? extends K, ? extends V> map) {
292    return new LinkedHashMap<K, V>(map);
293  }
294
295  /**
296   * Creates a new empty {@link ConcurrentHashMap} instance.
297   *
298   * @since 3.0
299   */
300  public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
301    return new ConcurrentHashMap<>();
302  }
303
304  /**
305   * Creates a <i>mutable</i>, empty {@code TreeMap} instance using the natural
306   * ordering of its elements.
307   *
308   * <p><b>Note:</b> if mutability is not required, use {@link
309   * ImmutableSortedMap#of()} instead.
310   *
311   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
312   * should be treated as deprecated. Instead, use the {@code TreeMap}
313   * constructor directly, taking advantage of the new
314   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
315   *
316   * @return a new, empty {@code TreeMap}
317   */
318  public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() {
319    return new TreeMap<K, V>();
320  }
321
322  /**
323   * Creates a <i>mutable</i> {@code TreeMap} instance with the same mappings as
324   * the specified map and using the same ordering as the specified map.
325   *
326   * <p><b>Note:</b> if mutability is not required, use {@link
327   * ImmutableSortedMap#copyOfSorted(SortedMap)} instead.
328   *
329   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
330   * should be treated as deprecated. Instead, use the {@code TreeMap}
331   * constructor directly, taking advantage of the new
332   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
333   *
334   * @param map the sorted map whose mappings are to be placed in the new map
335   *        and whose comparator is to be used to sort the new map
336   * @return a new {@code TreeMap} initialized with the mappings from {@code
337   *         map} and using the comparator of {@code map}
338   */
339  public static <K, V> TreeMap<K, V> newTreeMap(SortedMap<K, ? extends V> map) {
340    return new TreeMap<K, V>(map);
341  }
342
343  /**
344   * Creates a <i>mutable</i>, empty {@code TreeMap} instance using the given
345   * comparator.
346   *
347   * <p><b>Note:</b> if mutability is not required, use {@code
348   * ImmutableSortedMap.orderedBy(comparator).build()} instead.
349   *
350   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
351   * should be treated as deprecated. Instead, use the {@code TreeMap}
352   * constructor directly, taking advantage of the new
353   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
354   *
355   * @param comparator the comparator to sort the keys with
356   * @return a new, empty {@code TreeMap}
357   */
358  public static <C, K extends C, V> TreeMap<K, V> newTreeMap(@Nullable Comparator<C> comparator) {
359    // Ideally, the extra type parameter "C" shouldn't be necessary. It is a
360    // work-around of a compiler type inference quirk that prevents the
361    // following code from being compiled:
362    // Comparator<Class<?>> comparator = null;
363    // Map<Class<? extends Throwable>, String> map = newTreeMap(comparator);
364    return new TreeMap<K, V>(comparator);
365  }
366
367  /**
368   * Creates an {@code EnumMap} instance.
369   *
370   * @param type the key type for this map
371   * @return a new, empty {@code EnumMap}
372   */
373  public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(Class<K> type) {
374    return new EnumMap<K, V>(checkNotNull(type));
375  }
376
377  /**
378   * Creates an {@code EnumMap} with the same mappings as the specified map.
379   *
380   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
381   * should be treated as deprecated. Instead, use the {@code EnumMap}
382   * constructor directly, taking advantage of the new
383   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
384   *
385   * @param map the map from which to initialize this {@code EnumMap}
386   * @return a new {@code EnumMap} initialized with the mappings from {@code
387   *         map}
388   * @throws IllegalArgumentException if {@code m} is not an {@code EnumMap}
389   *         instance and contains no mappings
390   */
391  public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(Map<K, ? extends V> map) {
392    return new EnumMap<K, V>(map);
393  }
394
395  /**
396   * Creates an {@code IdentityHashMap} instance.
397   *
398   * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and
399   * should be treated as deprecated. Instead, use the {@code IdentityHashMap}
400   * constructor directly, taking advantage of the new
401   * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
402   *
403   * @return a new, empty {@code IdentityHashMap}
404   */
405  public static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
406    return new IdentityHashMap<K, V>();
407  }
408
409  /**
410   * Computes the difference between two maps. This difference is an immutable
411   * snapshot of the state of the maps at the time this method is called. It
412   * will never change, even if the maps change at a later time.
413   *
414   * <p>Since this method uses {@code HashMap} instances internally, the keys of
415   * the supplied maps must be well-behaved with respect to
416   * {@link Object#equals} and {@link Object#hashCode}.
417   *
418   * <p><b>Note:</b>If you only need to know whether two maps have the same
419   * mappings, call {@code left.equals(right)} instead of this method.
420   *
421   * @param left the map to treat as the "left" map for purposes of comparison
422   * @param right the map to treat as the "right" map for purposes of comparison
423   * @return the difference between the two maps
424   */
425  @SuppressWarnings("unchecked")
426  public static <K, V> MapDifference<K, V> difference(
427      Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
428    if (left instanceof SortedMap) {
429      SortedMap<K, ? extends V> sortedLeft = (SortedMap<K, ? extends V>) left;
430      SortedMapDifference<K, V> result = difference(sortedLeft, right);
431      return result;
432    }
433    return difference(left, right, Equivalence.equals());
434  }
435
436  /**
437   * Computes the difference between two maps. This difference is an immutable
438   * snapshot of the state of the maps at the time this method is called. It
439   * will never change, even if the maps change at a later time.
440   *
441   * <p>Since this method uses {@code HashMap} instances internally, the keys of
442   * the supplied maps must be well-behaved with respect to
443   * {@link Object#equals} and {@link Object#hashCode}.
444   *
445   * @param left the map to treat as the "left" map for purposes of comparison
446   * @param right the map to treat as the "right" map for purposes of comparison
447   * @param valueEquivalence the equivalence relationship to use to compare
448   *    values
449   * @return the difference between the two maps
450   * @since 10.0
451   */
452  public static <K, V> MapDifference<K, V> difference(
453      Map<? extends K, ? extends V> left,
454      Map<? extends K, ? extends V> right,
455      Equivalence<? super V> valueEquivalence) {
456    Preconditions.checkNotNull(valueEquivalence);
457
458    Map<K, V> onlyOnLeft = newLinkedHashMap();
459    Map<K, V> onlyOnRight = new LinkedHashMap<K, V>(right); // will whittle it down
460    Map<K, V> onBoth = newLinkedHashMap();
461    Map<K, MapDifference.ValueDifference<V>> differences = newLinkedHashMap();
462    doDifference(left, right, valueEquivalence, onlyOnLeft, onlyOnRight, onBoth, differences);
463    return new MapDifferenceImpl<K, V>(onlyOnLeft, onlyOnRight, onBoth, differences);
464  }
465
466  private static <K, V> void doDifference(
467      Map<? extends K, ? extends V> left,
468      Map<? extends K, ? extends V> right,
469      Equivalence<? super V> valueEquivalence,
470      Map<K, V> onlyOnLeft,
471      Map<K, V> onlyOnRight,
472      Map<K, V> onBoth,
473      Map<K, MapDifference.ValueDifference<V>> differences) {
474    for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
475      K leftKey = entry.getKey();
476      V leftValue = entry.getValue();
477      if (right.containsKey(leftKey)) {
478        V rightValue = onlyOnRight.remove(leftKey);
479        if (valueEquivalence.equivalent(leftValue, rightValue)) {
480          onBoth.put(leftKey, leftValue);
481        } else {
482          differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
483        }
484      } else {
485        onlyOnLeft.put(leftKey, leftValue);
486      }
487    }
488  }
489
490  private static <K, V> Map<K, V> unmodifiableMap(Map<K, ? extends V> map) {
491    if (map instanceof SortedMap) {
492      return Collections.unmodifiableSortedMap((SortedMap<K, ? extends V>) map);
493    } else {
494      return Collections.unmodifiableMap(map);
495    }
496  }
497
498  static class MapDifferenceImpl<K, V> implements MapDifference<K, V> {
499    final Map<K, V> onlyOnLeft;
500    final Map<K, V> onlyOnRight;
501    final Map<K, V> onBoth;
502    final Map<K, ValueDifference<V>> differences;
503
504    MapDifferenceImpl(
505        Map<K, V> onlyOnLeft,
506        Map<K, V> onlyOnRight,
507        Map<K, V> onBoth,
508        Map<K, ValueDifference<V>> differences) {
509      this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
510      this.onlyOnRight = unmodifiableMap(onlyOnRight);
511      this.onBoth = unmodifiableMap(onBoth);
512      this.differences = unmodifiableMap(differences);
513    }
514
515    @Override
516    public boolean areEqual() {
517      return onlyOnLeft.isEmpty() && onlyOnRight.isEmpty() && differences.isEmpty();
518    }
519
520    @Override
521    public Map<K, V> entriesOnlyOnLeft() {
522      return onlyOnLeft;
523    }
524
525    @Override
526    public Map<K, V> entriesOnlyOnRight() {
527      return onlyOnRight;
528    }
529
530    @Override
531    public Map<K, V> entriesInCommon() {
532      return onBoth;
533    }
534
535    @Override
536    public Map<K, ValueDifference<V>> entriesDiffering() {
537      return differences;
538    }
539
540    @Override
541    public boolean equals(Object object) {
542      if (object == this) {
543        return true;
544      }
545      if (object instanceof MapDifference) {
546        MapDifference<?, ?> other = (MapDifference<?, ?>) object;
547        return entriesOnlyOnLeft().equals(other.entriesOnlyOnLeft())
548            && entriesOnlyOnRight().equals(other.entriesOnlyOnRight())
549            && entriesInCommon().equals(other.entriesInCommon())
550            && entriesDiffering().equals(other.entriesDiffering());
551      }
552      return false;
553    }
554
555    @Override
556    public int hashCode() {
557      return Objects.hashCode(
558          entriesOnlyOnLeft(), entriesOnlyOnRight(), entriesInCommon(), entriesDiffering());
559    }
560
561    @Override
562    public String toString() {
563      if (areEqual()) {
564        return "equal";
565      }
566
567      StringBuilder result = new StringBuilder("not equal");
568      if (!onlyOnLeft.isEmpty()) {
569        result.append(": only on left=").append(onlyOnLeft);
570      }
571      if (!onlyOnRight.isEmpty()) {
572        result.append(": only on right=").append(onlyOnRight);
573      }
574      if (!differences.isEmpty()) {
575        result.append(": value differences=").append(differences);
576      }
577      return result.toString();
578    }
579  }
580
581  static class ValueDifferenceImpl<V> implements MapDifference.ValueDifference<V> {
582    private final V left;
583    private final V right;
584
585    static <V> ValueDifference<V> create(@Nullable V left, @Nullable V right) {
586      return new ValueDifferenceImpl<V>(left, right);
587    }
588
589    private ValueDifferenceImpl(@Nullable V left, @Nullable V right) {
590      this.left = left;
591      this.right = right;
592    }
593
594    @Override
595    public V leftValue() {
596      return left;
597    }
598
599    @Override
600    public V rightValue() {
601      return right;
602    }
603
604    @Override
605    public boolean equals(@Nullable Object object) {
606      if (object instanceof MapDifference.ValueDifference) {
607        MapDifference.ValueDifference<?> that = (MapDifference.ValueDifference<?>) object;
608        return Objects.equal(this.left, that.leftValue())
609            && Objects.equal(this.right, that.rightValue());
610      }
611      return false;
612    }
613
614    @Override
615    public int hashCode() {
616      return Objects.hashCode(left, right);
617    }
618
619    @Override
620    public String toString() {
621      return "(" + left + ", " + right + ")";
622    }
623  }
624
625  /**
626   * Computes the difference between two sorted maps, using the comparator of
627   * the left map, or {@code Ordering.natural()} if the left map uses the
628   * natural ordering of its elements. This difference is an immutable snapshot
629   * of the state of the maps at the time this method is called. It will never
630   * change, even if the maps change at a later time.
631   *
632   * <p>Since this method uses {@code TreeMap} instances internally, the keys of
633   * the right map must all compare as distinct according to the comparator
634   * of the left map.
635   *
636   * <p><b>Note:</b>If you only need to know whether two sorted maps have the
637   * same mappings, call {@code left.equals(right)} instead of this method.
638   *
639   * @param left the map to treat as the "left" map for purposes of comparison
640   * @param right the map to treat as the "right" map for purposes of comparison
641   * @return the difference between the two maps
642   * @since 11.0
643   */
644  public static <K, V> SortedMapDifference<K, V> difference(
645      SortedMap<K, ? extends V> left, Map<? extends K, ? extends V> right) {
646    checkNotNull(left);
647    checkNotNull(right);
648    Comparator<? super K> comparator = orNaturalOrder(left.comparator());
649    SortedMap<K, V> onlyOnLeft = Maps.newTreeMap(comparator);
650    SortedMap<K, V> onlyOnRight = Maps.newTreeMap(comparator);
651    onlyOnRight.putAll(right); // will whittle it down
652    SortedMap<K, V> onBoth = Maps.newTreeMap(comparator);
653    SortedMap<K, MapDifference.ValueDifference<V>> differences = Maps.newTreeMap(comparator);
654    doDifference(left, right, Equivalence.equals(), onlyOnLeft, onlyOnRight, onBoth, differences);
655    return new SortedMapDifferenceImpl<K, V>(onlyOnLeft, onlyOnRight, onBoth, differences);
656  }
657
658  static class SortedMapDifferenceImpl<K, V> extends MapDifferenceImpl<K, V>
659      implements SortedMapDifference<K, V> {
660    SortedMapDifferenceImpl(
661        SortedMap<K, V> onlyOnLeft,
662        SortedMap<K, V> onlyOnRight,
663        SortedMap<K, V> onBoth,
664        SortedMap<K, ValueDifference<V>> differences) {
665      super(onlyOnLeft, onlyOnRight, onBoth, differences);
666    }
667
668    @Override
669    public SortedMap<K, ValueDifference<V>> entriesDiffering() {
670      return (SortedMap<K, ValueDifference<V>>) super.entriesDiffering();
671    }
672
673    @Override
674    public SortedMap<K, V> entriesInCommon() {
675      return (SortedMap<K, V>) super.entriesInCommon();
676    }
677
678    @Override
679    public SortedMap<K, V> entriesOnlyOnLeft() {
680      return (SortedMap<K, V>) super.entriesOnlyOnLeft();
681    }
682
683    @Override
684    public SortedMap<K, V> entriesOnlyOnRight() {
685      return (SortedMap<K, V>) super.entriesOnlyOnRight();
686    }
687  }
688
689  /**
690   * Returns the specified comparator if not null; otherwise returns {@code
691   * Ordering.natural()}. This method is an abomination of generics; the only
692   * purpose of this method is to contain the ugly type-casting in one place.
693   */
694  @SuppressWarnings("unchecked")
695  static <E> Comparator<? super E> orNaturalOrder(@Nullable Comparator<? super E> comparator) {
696    if (comparator != null) { // can't use ? : because of javac bug 5080917
697      return comparator;
698    }
699    return (Comparator<E>) Ordering.natural();
700  }
701
702  /**
703   * Returns a live {@link Map} view whose keys are the contents of {@code set}
704   * and whose values are computed on demand using {@code function}. To get an
705   * immutable <i>copy</i> instead, use {@link #toMap(Iterable, Function)}.
706   *
707   * <p>Specifically, for each {@code k} in the backing set, the returned map
708   * has an entry mapping {@code k} to {@code function.apply(k)}. The {@code
709   * keySet}, {@code values}, and {@code entrySet} views of the returned map
710   * iterate in the same order as the backing set.
711   *
712   * <p>Modifications to the backing set are read through to the returned map.
713   * The returned map supports removal operations if the backing set does.
714   * Removal operations write through to the backing set.  The returned map
715   * does not support put operations.
716   *
717   * <p><b>Warning:</b> If the function rejects {@code null}, caution is
718   * required to make sure the set does not contain {@code null}, because the
719   * view cannot stop {@code null} from being added to the set.
720   *
721   * <p><b>Warning:</b> This method assumes that for any instance {@code k} of
722   * key type {@code K}, {@code k.equals(k2)} implies that {@code k2} is also
723   * of type {@code K}. Using a key type for which this may not hold, such as
724   * {@code ArrayList}, may risk a {@code ClassCastException} when calling
725   * methods on the resulting map view.
726   *
727   * @since 14.0
728   */
729  public static <K, V> Map<K, V> asMap(Set<K> set, Function<? super K, V> function) {
730    return new AsMapView<K, V>(set, function);
731  }
732
733  /**
734   * Returns a view of the sorted set as a map, mapping keys from the set
735   * according to the specified function.
736   *
737   * <p>Specifically, for each {@code k} in the backing set, the returned map
738   * has an entry mapping {@code k} to {@code function.apply(k)}. The {@code
739   * keySet}, {@code values}, and {@code entrySet} views of the returned map
740   * iterate in the same order as the backing set.
741   *
742   * <p>Modifications to the backing set are read through to the returned map.
743   * The returned map supports removal operations if the backing set does.
744   * Removal operations write through to the backing set.  The returned map does
745   * not support put operations.
746   *
747   * <p><b>Warning:</b> If the function rejects {@code null}, caution is
748   * required to make sure the set does not contain {@code null}, because the
749   * view cannot stop {@code null} from being added to the set.
750   *
751   * <p><b>Warning:</b> This method assumes that for any instance {@code k} of
752   * key type {@code K}, {@code k.equals(k2)} implies that {@code k2} is also of
753   * type {@code K}. Using a key type for which this may not hold, such as
754   * {@code ArrayList}, may risk a {@code ClassCastException} when calling
755   * methods on the resulting map view.
756   *
757   * @since 14.0
758   */
759  public static <K, V> SortedMap<K, V> asMap(SortedSet<K> set, Function<? super K, V> function) {
760    return new SortedAsMapView<K, V>(set, function);
761  }
762
763  /**
764   * Returns a view of the navigable set as a map, mapping keys from the set
765   * according to the specified function.
766   *
767   * <p>Specifically, for each {@code k} in the backing set, the returned map
768   * has an entry mapping {@code k} to {@code function.apply(k)}. The {@code
769   * keySet}, {@code values}, and {@code entrySet} views of the returned map
770   * iterate in the same order as the backing set.
771   *
772   * <p>Modifications to the backing set are read through to the returned map.
773   * The returned map supports removal operations if the backing set does.
774   * Removal operations write through to the backing set.  The returned map
775   * does not support put operations.
776   *
777   * <p><b>Warning:</b> If the function rejects {@code null}, caution is
778   * required to make sure the set does not contain {@code null}, because the
779   * view cannot stop {@code null} from being added to the set.
780   *
781   * <p><b>Warning:</b> This method assumes that for any instance {@code k} of
782   * key type {@code K}, {@code k.equals(k2)} implies that {@code k2} is also
783   * of type {@code K}. Using a key type for which this may not hold, such as
784   * {@code ArrayList}, may risk a {@code ClassCastException} when calling
785   * methods on the resulting map view.
786   *
787   * @since 14.0
788   */
789  @GwtIncompatible // NavigableMap
790  public static <K, V> NavigableMap<K, V> asMap(
791      NavigableSet<K> set, Function<? super K, V> function) {
792    return new NavigableAsMapView<K, V>(set, function);
793  }
794
795  private static class AsMapView<K, V> extends ViewCachingAbstractMap<K, V> {
796
797    private final Set<K> set;
798    final Function<? super K, V> function;
799
800    Set<K> backingSet() {
801      return set;
802    }
803
804    AsMapView(Set<K> set, Function<? super K, V> function) {
805      this.set = checkNotNull(set);
806      this.function = checkNotNull(function);
807    }
808
809    @Override
810    public Set<K> createKeySet() {
811      return removeOnlySet(backingSet());
812    }
813
814    @Override
815    Collection<V> createValues() {
816      return Collections2.transform(set, function);
817    }
818
819    @Override
820    public int size() {
821      return backingSet().size();
822    }
823
824    @Override
825    public boolean containsKey(@Nullable Object key) {
826      return backingSet().contains(key);
827    }
828
829    @Override
830    public V get(@Nullable Object key) {
831      if (Collections2.safeContains(backingSet(), key)) {
832        @SuppressWarnings("unchecked") // unsafe, but Javadoc warns about it
833        K k = (K) key;
834        return function.apply(k);
835      } else {
836        return null;
837      }
838    }
839
840    @Override
841    public V remove(@Nullable Object key) {
842      if (backingSet().remove(key)) {
843        @SuppressWarnings("unchecked") // unsafe, but Javadoc warns about it
844        K k = (K) key;
845        return function.apply(k);
846      } else {
847        return null;
848      }
849    }
850
851    @Override
852    public void clear() {
853      backingSet().clear();
854    }
855
856    @Override
857    protected Set<Entry<K, V>> createEntrySet() {
858      @WeakOuter
859      class EntrySetImpl extends EntrySet<K, V> {
860        @Override
861        Map<K, V> map() {
862          return AsMapView.this;
863        }
864
865        @Override
866        public Iterator<Entry<K, V>> iterator() {
867          return asMapEntryIterator(backingSet(), function);
868        }
869      }
870      return new EntrySetImpl();
871    }
872  }
873
874  static <K, V> Iterator<Entry<K, V>> asMapEntryIterator(
875      Set<K> set, final Function<? super K, V> function) {
876    return new TransformedIterator<K, Entry<K, V>>(set.iterator()) {
877      @Override
878      Entry<K, V> transform(final K key) {
879        return immutableEntry(key, function.apply(key));
880      }
881    };
882  }
883
884  private static class SortedAsMapView<K, V> extends AsMapView<K, V> implements SortedMap<K, V> {
885
886    SortedAsMapView(SortedSet<K> set, Function<? super K, V> function) {
887      super(set, function);
888    }
889
890    @Override
891    SortedSet<K> backingSet() {
892      return (SortedSet<K>) super.backingSet();
893    }
894
895    @Override
896    public Comparator<? super K> comparator() {
897      return backingSet().comparator();
898    }
899
900    @Override
901    public Set<K> keySet() {
902      return removeOnlySortedSet(backingSet());
903    }
904
905    @Override
906    public SortedMap<K, V> subMap(K fromKey, K toKey) {
907      return asMap(backingSet().subSet(fromKey, toKey), function);
908    }
909
910    @Override
911    public SortedMap<K, V> headMap(K toKey) {
912      return asMap(backingSet().headSet(toKey), function);
913    }
914
915    @Override
916    public SortedMap<K, V> tailMap(K fromKey) {
917      return asMap(backingSet().tailSet(fromKey), function);
918    }
919
920    @Override
921    public K firstKey() {
922      return backingSet().first();
923    }
924
925    @Override
926    public K lastKey() {
927      return backingSet().last();
928    }
929  }
930
931  @GwtIncompatible // NavigableMap
932  private static final class NavigableAsMapView<K, V> extends AbstractNavigableMap<K, V> {
933    /*
934     * Using AbstractNavigableMap is simpler than extending SortedAsMapView and rewriting all the
935     * NavigableMap methods.
936     */
937
938    private final NavigableSet<K> set;
939    private final Function<? super K, V> function;
940
941    NavigableAsMapView(NavigableSet<K> ks, Function<? super K, V> vFunction) {
942      this.set = checkNotNull(ks);
943      this.function = checkNotNull(vFunction);
944    }
945
946    @Override
947    public NavigableMap<K, V> subMap(
948        K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
949      return asMap(set.subSet(fromKey, fromInclusive, toKey, toInclusive), function);
950    }
951
952    @Override
953    public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
954      return asMap(set.headSet(toKey, inclusive), function);
955    }
956
957    @Override
958    public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
959      return asMap(set.tailSet(fromKey, inclusive), function);
960    }
961
962    @Override
963    public Comparator<? super K> comparator() {
964      return set.comparator();
965    }
966
967    @Override
968    @Nullable
969    public V get(@Nullable Object key) {
970      if (Collections2.safeContains(set, key)) {
971        @SuppressWarnings("unchecked") // unsafe, but Javadoc warns about it
972        K k = (K) key;
973        return function.apply(k);
974      } else {
975        return null;
976      }
977    }
978
979    @Override
980    public void clear() {
981      set.clear();
982    }
983
984    @Override
985    Iterator<Entry<K, V>> entryIterator() {
986      return asMapEntryIterator(set, function);
987    }
988
989    @Override
990    Iterator<Entry<K, V>> descendingEntryIterator() {
991      return descendingMap().entrySet().iterator();
992    }
993
994    @Override
995    public NavigableSet<K> navigableKeySet() {
996      return removeOnlyNavigableSet(set);
997    }
998
999    @Override
1000    public int size() {
1001      return set.size();
1002    }
1003
1004    @Override
1005    public NavigableMap<K, V> descendingMap() {
1006      return asMap(set.descendingSet(), function);
1007    }
1008  }
1009
1010  private static <E> Set<E> removeOnlySet(final Set<E> set) {
1011    return new ForwardingSet<E>() {
1012      @Override
1013      protected Set<E> delegate() {
1014        return set;
1015      }
1016
1017      @Override
1018      public boolean add(E element) {
1019        throw new UnsupportedOperationException();
1020      }
1021
1022      @Override
1023      public boolean addAll(Collection<? extends E> es) {
1024        throw new UnsupportedOperationException();
1025      }
1026    };
1027  }
1028
1029  private static <E> SortedSet<E> removeOnlySortedSet(final SortedSet<E> set) {
1030    return new ForwardingSortedSet<E>() {
1031      @Override
1032      protected SortedSet<E> delegate() {
1033        return set;
1034      }
1035
1036      @Override
1037      public boolean add(E element) {
1038        throw new UnsupportedOperationException();
1039      }
1040
1041      @Override
1042      public boolean addAll(Collection<? extends E> es) {
1043        throw new UnsupportedOperationException();
1044      }
1045
1046      @Override
1047      public SortedSet<E> headSet(E toElement) {
1048        return removeOnlySortedSet(super.headSet(toElement));
1049      }
1050
1051      @Override
1052      public SortedSet<E> subSet(E fromElement, E toElement) {
1053        return removeOnlySortedSet(super.subSet(fromElement, toElement));
1054      }
1055
1056      @Override
1057      public SortedSet<E> tailSet(E fromElement) {
1058        return removeOnlySortedSet(super.tailSet(fromElement));
1059      }
1060    };
1061  }
1062
1063  @GwtIncompatible // NavigableSet
1064  private static <E> NavigableSet<E> removeOnlyNavigableSet(final NavigableSet<E> set) {
1065    return new ForwardingNavigableSet<E>() {
1066      @Override
1067      protected NavigableSet<E> delegate() {
1068        return set;
1069      }
1070
1071      @Override
1072      public boolean add(E element) {
1073        throw new UnsupportedOperationException();
1074      }
1075
1076      @Override
1077      public boolean addAll(Collection<? extends E> es) {
1078        throw new UnsupportedOperationException();
1079      }
1080
1081      @Override
1082      public SortedSet<E> headSet(E toElement) {
1083        return removeOnlySortedSet(super.headSet(toElement));
1084      }
1085
1086      @Override
1087      public SortedSet<E> subSet(E fromElement, E toElement) {
1088        return removeOnlySortedSet(super.subSet(fromElement, toElement));
1089      }
1090
1091      @Override
1092      public SortedSet<E> tailSet(E fromElement) {
1093        return removeOnlySortedSet(super.tailSet(fromElement));
1094      }
1095
1096      @Override
1097      public NavigableSet<E> headSet(E toElement, boolean inclusive) {
1098        return removeOnlyNavigableSet(super.headSet(toElement, inclusive));
1099      }
1100
1101      @Override
1102      public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
1103        return removeOnlyNavigableSet(super.tailSet(fromElement, inclusive));
1104      }
1105
1106      @Override
1107      public NavigableSet<E> subSet(
1108          E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
1109        return removeOnlyNavigableSet(
1110            super.subSet(fromElement, fromInclusive, toElement, toInclusive));
1111      }
1112
1113      @Override
1114      public NavigableSet<E> descendingSet() {
1115        return removeOnlyNavigableSet(super.descendingSet());
1116      }
1117    };
1118  }
1119
1120  /**
1121   * Returns an immutable map whose keys are the distinct elements of {@code
1122   * keys} and whose value for each key was computed by {@code valueFunction}.
1123   * The map's iteration order is the order of the first appearance of each key
1124   * in {@code keys}.
1125   *
1126   * <p>When there are multiple instances of a key in {@code keys}, it is
1127   * unspecified whether {@code valueFunction} will be applied to more than one
1128   * instance of that key and, if it is, which result will be mapped to that
1129   * key in the returned map.
1130   *
1131   * <p>If {@code keys} is a {@link Set}, a live view can be obtained instead of
1132   * a copy using {@link Maps#asMap(Set, Function)}.
1133   *
1134   * @throws NullPointerException if any element of {@code keys} is
1135   *     {@code null}, or if {@code valueFunction} produces {@code null}
1136   *     for any key
1137   * @since 14.0
1138   */
1139  public static <K, V> ImmutableMap<K, V> toMap(
1140      Iterable<K> keys, Function<? super K, V> valueFunction) {
1141    return toMap(keys.iterator(), valueFunction);
1142  }
1143
1144  /**
1145   * Returns an immutable map whose keys are the distinct elements of {@code
1146   * keys} and whose value for each key was computed by {@code valueFunction}.
1147   * The map's iteration order is the order of the first appearance of each key
1148   * in {@code keys}.
1149   *
1150   * <p>When there are multiple instances of a key in {@code keys}, it is
1151   * unspecified whether {@code valueFunction} will be applied to more than one
1152   * instance of that key and, if it is, which result will be mapped to that
1153   * key in the returned map.
1154   *
1155   * @throws NullPointerException if any element of {@code keys} is
1156   *     {@code null}, or if {@code valueFunction} produces {@code null}
1157   *     for any key
1158   * @since 14.0
1159   */
1160  public static <K, V> ImmutableMap<K, V> toMap(
1161      Iterator<K> keys, Function<? super K, V> valueFunction) {
1162    checkNotNull(valueFunction);
1163    // Using LHM instead of a builder so as not to fail on duplicate keys
1164    Map<K, V> builder = newLinkedHashMap();
1165    while (keys.hasNext()) {
1166      K key = keys.next();
1167      builder.put(key, valueFunction.apply(key));
1168    }
1169    return ImmutableMap.copyOf(builder);
1170  }
1171
1172  /**
1173   * Returns a map with the given {@code values}, indexed by keys derived from
1174   * those values. In other words, each input value produces an entry in the map
1175   * whose key is the result of applying {@code keyFunction} to that value.
1176   * These entries appear in the same order as the input values. Example usage:
1177   * <pre>   {@code
1178   *
1179   *   Color red = new Color("red", 255, 0, 0);
1180   *   ...
1181   *   ImmutableSet<Color> allColors = ImmutableSet.of(red, green, blue);
1182   *
1183   *   Map<String, Color> colorForName =
1184   *       uniqueIndex(allColors, toStringFunction());
1185   *   assertThat(colorForName).containsEntry("red", red);}</pre>
1186   *
1187   * <p>If your index may associate multiple values with each key, use {@link
1188   * Multimaps#index(Iterable, Function) Multimaps.index}.
1189   *
1190   * @param values the values to use when constructing the {@code Map}
1191   * @param keyFunction the function used to produce the key for each value
1192   * @return a map mapping the result of evaluating the function {@code
1193   *         keyFunction} on each value in the input collection to that value
1194   * @throws IllegalArgumentException if {@code keyFunction} produces the same
1195   *         key for more than one value in the input collection
1196   * @throws NullPointerException if any element of {@code values} is {@code
1197   *         null}, or if {@code keyFunction} produces {@code null} for any value
1198   */
1199  @CanIgnoreReturnValue
1200  public static <K, V> ImmutableMap<K, V> uniqueIndex(
1201      Iterable<V> values, Function<? super V, K> keyFunction) {
1202    // TODO(lowasser): consider presizing the builder if values is a Collection
1203    return uniqueIndex(values.iterator(), keyFunction);
1204  }
1205
1206  /**
1207   * Returns a map with the given {@code values}, indexed by keys derived from
1208   * those values. In other words, each input value produces an entry in the map
1209   * whose key is the result of applying {@code keyFunction} to that value.
1210   * These entries appear in the same order as the input values. Example usage:
1211   * <pre>   {@code
1212   *
1213   *   Color red = new Color("red", 255, 0, 0);
1214   *   ...
1215   *   Iterator<Color> allColors = ImmutableSet.of(red, green, blue).iterator();
1216   *
1217   *   Map<String, Color> colorForName =
1218   *       uniqueIndex(allColors, toStringFunction());
1219   *   assertThat(colorForName).containsEntry("red", red);}</pre>
1220   *
1221   * <p>If your index may associate multiple values with each key, use {@link
1222   * Multimaps#index(Iterator, Function) Multimaps.index}.
1223   *
1224   * @param values the values to use when constructing the {@code Map}
1225   * @param keyFunction the function used to produce the key for each value
1226   * @return a map mapping the result of evaluating the function {@code
1227   *         keyFunction} on each value in the input collection to that value
1228   * @throws IllegalArgumentException if {@code keyFunction} produces the same
1229   *         key for more than one value in the input collection
1230   * @throws NullPointerException if any element of {@code values} is {@code
1231   *         null}, or if {@code keyFunction} produces {@code null} for any value
1232   * @since 10.0
1233   */
1234  @CanIgnoreReturnValue
1235  public static <K, V> ImmutableMap<K, V> uniqueIndex(
1236      Iterator<V> values, Function<? super V, K> keyFunction) {
1237    checkNotNull(keyFunction);
1238    ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
1239    while (values.hasNext()) {
1240      V value = values.next();
1241      builder.put(keyFunction.apply(value), value);
1242    }
1243    try {
1244      return builder.build();
1245    } catch (IllegalArgumentException duplicateKeys) {
1246      throw new IllegalArgumentException(
1247          duplicateKeys.getMessage()
1248              + ". To index multiple values under a key, use Multimaps.index.");
1249    }
1250  }
1251
1252  /**
1253   * Creates an {@code ImmutableMap<String, String>} from a {@code Properties}
1254   * instance. Properties normally derive from {@code Map<Object, Object>}, but
1255   * they typically contain strings, which is awkward. This method lets you get
1256   * a plain-old-{@code Map} out of a {@code Properties}.
1257   *
1258   * @param properties a {@code Properties} object to be converted
1259   * @return an immutable map containing all the entries in {@code properties}
1260   * @throws ClassCastException if any key in {@code Properties} is not a {@code
1261   *         String}
1262   * @throws NullPointerException if any key or value in {@code Properties} is
1263   *         null
1264   */
1265  @GwtIncompatible // java.util.Properties
1266  public static ImmutableMap<String, String> fromProperties(Properties properties) {
1267    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
1268
1269    for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
1270      String key = (String) e.nextElement();
1271      builder.put(key, properties.getProperty(key));
1272    }
1273
1274    return builder.build();
1275  }
1276
1277  /**
1278   * Returns an immutable map entry with the specified key and value. The {@link
1279   * Entry#setValue} operation throws an {@link UnsupportedOperationException}.
1280   *
1281   * <p>The returned entry is serializable.
1282   *
1283   * @param key the key to be associated with the returned entry
1284   * @param value the value to be associated with the returned entry
1285   */
1286  @GwtCompatible(serializable = true)
1287  public static <K, V> Entry<K, V> immutableEntry(@Nullable K key, @Nullable V value) {
1288    return new ImmutableEntry<K, V>(key, value);
1289  }
1290
1291  /**
1292   * Returns an unmodifiable view of the specified set of entries. The {@link
1293   * Entry#setValue} operation throws an {@link UnsupportedOperationException},
1294   * as do any operations that would modify the returned set.
1295   *
1296   * @param entrySet the entries for which to return an unmodifiable view
1297   * @return an unmodifiable view of the entries
1298   */
1299  static <K, V> Set<Entry<K, V>> unmodifiableEntrySet(Set<Entry<K, V>> entrySet) {
1300    return new UnmodifiableEntrySet<K, V>(Collections.unmodifiableSet(entrySet));
1301  }
1302
1303  /**
1304   * Returns an unmodifiable view of the specified map entry. The {@link
1305   * Entry#setValue} operation throws an {@link UnsupportedOperationException}.
1306   * This also has the side-effect of redefining {@code equals} to comply with
1307   * the Entry contract, to avoid a possible nefarious implementation of equals.
1308   *
1309   * @param entry the entry for which to return an unmodifiable view
1310   * @return an unmodifiable view of the entry
1311   */
1312  static <K, V> Entry<K, V> unmodifiableEntry(final Entry<? extends K, ? extends V> entry) {
1313    checkNotNull(entry);
1314    return new AbstractMapEntry<K, V>() {
1315      @Override
1316      public K getKey() {
1317        return entry.getKey();
1318      }
1319
1320      @Override
1321      public V getValue() {
1322        return entry.getValue();
1323      }
1324    };
1325  }
1326
1327  static <K, V> UnmodifiableIterator<Entry<K, V>> unmodifiableEntryIterator(
1328      final Iterator<Entry<K, V>> entryIterator) {
1329    return new UnmodifiableIterator<Entry<K, V>>() {
1330      @Override
1331      public boolean hasNext() {
1332        return entryIterator.hasNext();
1333      }
1334
1335      @Override
1336      public Entry<K, V> next() {
1337        return unmodifiableEntry(entryIterator.next());
1338      }
1339    };
1340  }
1341
1342  /** @see Multimaps#unmodifiableEntries */
1343  static class UnmodifiableEntries<K, V> extends ForwardingCollection<Entry<K, V>> {
1344    private final Collection<Entry<K, V>> entries;
1345
1346    UnmodifiableEntries(Collection<Entry<K, V>> entries) {
1347      this.entries = entries;
1348    }
1349
1350    @Override
1351    protected Collection<Entry<K, V>> delegate() {
1352      return entries;
1353    }
1354
1355    @Override
1356    public Iterator<Entry<K, V>> iterator() {
1357      return unmodifiableEntryIterator(entries.iterator());
1358    }
1359
1360    // See java.util.Collections.UnmodifiableEntrySet for details on attacks.
1361
1362    @Override
1363    public Object[] toArray() {
1364      return standardToArray();
1365    }
1366
1367    @Override
1368    public <T> T[] toArray(T[] array) {
1369      return standardToArray(array);
1370    }
1371  }
1372
1373  /** @see Maps#unmodifiableEntrySet(Set) */
1374  static class UnmodifiableEntrySet<K, V> extends UnmodifiableEntries<K, V>
1375      implements Set<Entry<K, V>> {
1376    UnmodifiableEntrySet(Set<Entry<K, V>> entries) {
1377      super(entries);
1378    }
1379
1380    // See java.util.Collections.UnmodifiableEntrySet for details on attacks.
1381
1382    @Override
1383    public boolean equals(@Nullable Object object) {
1384      return Sets.equalsImpl(this, object);
1385    }
1386
1387    @Override
1388    public int hashCode() {
1389      return Sets.hashCodeImpl(this);
1390    }
1391  }
1392
1393  /**
1394   * Returns a {@link Converter} that converts values using {@link BiMap#get bimap.get()},
1395   * and whose inverse view converts values using
1396   * {@link BiMap#inverse bimap.inverse()}{@code .get()}.
1397   *
1398   * <p>To use a plain {@link Map} as a {@link Function}, see
1399   * {@link com.google.common.base.Functions#forMap(Map)} or
1400   * {@link com.google.common.base.Functions#forMap(Map, Object)}.
1401   *
1402   * @since 16.0
1403   */
1404  @Beta
1405  public static <A, B> Converter<A, B> asConverter(final BiMap<A, B> bimap) {
1406    return new BiMapConverter<A, B>(bimap);
1407  }
1408
1409  private static final class BiMapConverter<A, B> extends Converter<A, B> implements Serializable {
1410    private final BiMap<A, B> bimap;
1411
1412    BiMapConverter(BiMap<A, B> bimap) {
1413      this.bimap = checkNotNull(bimap);
1414    }
1415
1416    @Override
1417    protected B doForward(A a) {
1418      return convert(bimap, a);
1419    }
1420
1421    @Override
1422    protected A doBackward(B b) {
1423      return convert(bimap.inverse(), b);
1424    }
1425
1426    private static <X, Y> Y convert(BiMap<X, Y> bimap, X input) {
1427      Y output = bimap.get(input);
1428      checkArgument(output != null, "No non-null mapping present for input: %s", input);
1429      return output;
1430    }
1431
1432    @Override
1433    public boolean equals(@Nullable Object object) {
1434      if (object instanceof BiMapConverter) {
1435        BiMapConverter<?, ?> that = (BiMapConverter<?, ?>) object;
1436        return this.bimap.equals(that.bimap);
1437      }
1438      return false;
1439    }
1440
1441    @Override
1442    public int hashCode() {
1443      return bimap.hashCode();
1444    }
1445
1446    // There's really no good way to implement toString() without printing the entire BiMap, right?
1447    @Override
1448    public String toString() {
1449      return "Maps.asConverter(" + bimap + ")";
1450    }
1451
1452    private static final long serialVersionUID = 0L;
1453  }
1454
1455  /**
1456   * Returns a synchronized (thread-safe) bimap backed by the specified bimap.
1457   * In order to guarantee serial access, it is critical that <b>all</b> access
1458   * to the backing bimap is accomplished through the returned bimap.
1459   *
1460   * <p>It is imperative that the user manually synchronize on the returned map
1461   * when accessing any of its collection views: <pre>   {@code
1462   *
1463   *   BiMap<Long, String> map = Maps.synchronizedBiMap(
1464   *       HashBiMap.<Long, String>create());
1465   *   ...
1466   *   Set<Long> set = map.keySet();  // Needn't be in synchronized block
1467   *   ...
1468   *   synchronized (map) {  // Synchronizing on map, not set!
1469   *     Iterator<Long> it = set.iterator(); // Must be in synchronized block
1470   *     while (it.hasNext()) {
1471   *       foo(it.next());
1472   *     }
1473   *   }}</pre>
1474   *
1475   * <p>Failure to follow this advice may result in non-deterministic behavior.
1476   *
1477   * <p>The returned bimap will be serializable if the specified bimap is
1478   * serializable.
1479   *
1480   * @param bimap the bimap to be wrapped in a synchronized view
1481   * @return a synchronized view of the specified bimap
1482   */
1483  public static <K, V> BiMap<K, V> synchronizedBiMap(BiMap<K, V> bimap) {
1484    return Synchronized.biMap(bimap, null);
1485  }
1486
1487  /**
1488   * Returns an unmodifiable view of the specified bimap. This method allows
1489   * modules to provide users with "read-only" access to internal bimaps. Query
1490   * operations on the returned bimap "read through" to the specified bimap, and
1491   * attempts to modify the returned map, whether direct or via its collection
1492   * views, result in an {@code UnsupportedOperationException}.
1493   *
1494   * <p>The returned bimap will be serializable if the specified bimap is
1495   * serializable.
1496   *
1497   * @param bimap the bimap for which an unmodifiable view is to be returned
1498   * @return an unmodifiable view of the specified bimap
1499   */
1500  public static <K, V> BiMap<K, V> unmodifiableBiMap(BiMap<? extends K, ? extends V> bimap) {
1501    return new UnmodifiableBiMap<K, V>(bimap, null);
1502  }
1503
1504  /** @see Maps#unmodifiableBiMap(BiMap) */
1505  private static class UnmodifiableBiMap<K, V> extends ForwardingMap<K, V>
1506      implements BiMap<K, V>, Serializable {
1507    final Map<K, V> unmodifiableMap;
1508    final BiMap<? extends K, ? extends V> delegate;
1509    @RetainedWith
1510    BiMap<V, K> inverse;
1511    transient Set<V> values;
1512
1513    UnmodifiableBiMap(BiMap<? extends K, ? extends V> delegate, @Nullable BiMap<V, K> inverse) {
1514      unmodifiableMap = Collections.unmodifiableMap(delegate);
1515      this.delegate = delegate;
1516      this.inverse = inverse;
1517    }
1518
1519    @Override
1520    protected Map<K, V> delegate() {
1521      return unmodifiableMap;
1522    }
1523
1524    @Override
1525    public V forcePut(K key, V value) {
1526      throw new UnsupportedOperationException();
1527    }
1528
1529    @Override
1530    public BiMap<V, K> inverse() {
1531      BiMap<V, K> result = inverse;
1532      return (result == null)
1533          ? inverse = new UnmodifiableBiMap<V, K>(delegate.inverse(), this)
1534          : result;
1535    }
1536
1537    @Override
1538    public Set<V> values() {
1539      Set<V> result = values;
1540      return (result == null) ? values = Collections.unmodifiableSet(delegate.values()) : result;
1541    }
1542
1543    private static final long serialVersionUID = 0;
1544  }
1545
1546  /**
1547   * Returns a view of a map where each value is transformed by a function. All
1548   * other properties of the map, such as iteration order, are left intact. For
1549   * example, the code: <pre>   {@code
1550   *
1551   *   Map<String, Integer> map = ImmutableMap.of("a", 4, "b", 9);
1552   *   Function<Integer, Double> sqrt =
1553   *       new Function<Integer, Double>() {
1554   *         public Double apply(Integer in) {
1555   *           return Math.sqrt((int) in);
1556   *         }
1557   *       };
1558   *   Map<String, Double> transformed = Maps.transformValues(map, sqrt);
1559   *   System.out.println(transformed);}</pre>
1560   *
1561   * ... prints {@code {a=2.0, b=3.0}}.
1562   *
1563   * <p>Changes in the underlying map are reflected in this view. Conversely,
1564   * this view supports removal operations, and these are reflected in the
1565   * underlying map.
1566   *
1567   * <p>It's acceptable for the underlying map to contain null keys, and even
1568   * null values provided that the function is capable of accepting null input.
1569   * The transformed map might contain null values, if the function sometimes
1570   * gives a null result.
1571   *
1572   * <p>The returned map is not thread-safe or serializable, even if the
1573   * underlying map is.
1574   *
1575   * <p>The function is applied lazily, invoked when needed. This is necessary
1576   * for the returned map to be a view, but it means that the function will be
1577   * applied many times for bulk operations like {@link Map#containsValue} and
1578   * {@code Map.toString()}. For this to perform well, {@code function} should
1579   * be fast. To avoid lazy evaluation when the returned map doesn't need to be
1580   * a view, copy the returned map into a new map of your choosing.
1581   */
1582  public static <K, V1, V2> Map<K, V2> transformValues(
1583      Map<K, V1> fromMap, Function<? super V1, V2> function) {
1584    return transformEntries(fromMap, asEntryTransformer(function));
1585  }
1586
1587  /**
1588   * Returns a view of a sorted map where each value is transformed by a
1589   * function. All other properties of the map, such as iteration order, are
1590   * left intact. For example, the code: <pre>   {@code
1591   *
1592   *   SortedMap<String, Integer> map = ImmutableSortedMap.of("a", 4, "b", 9);
1593   *   Function<Integer, Double> sqrt =
1594   *       new Function<Integer, Double>() {
1595   *         public Double apply(Integer in) {
1596   *           return Math.sqrt((int) in);
1597   *         }
1598   *       };
1599   *   SortedMap<String, Double> transformed =
1600   *        Maps.transformValues(map, sqrt);
1601   *   System.out.println(transformed);}</pre>
1602   *
1603   * ... prints {@code {a=2.0, b=3.0}}.
1604   *
1605   * <p>Changes in the underlying map are reflected in this view. Conversely,
1606   * this view supports removal operations, and these are reflected in the
1607   * underlying map.
1608   *
1609   * <p>It's acceptable for the underlying map to contain null keys, and even
1610   * null values provided that the function is capable of accepting null input.
1611   * The transformed map might contain null values, if the function sometimes
1612   * gives a null result.
1613   *
1614   * <p>The returned map is not thread-safe or serializable, even if the
1615   * underlying map is.
1616   *
1617   * <p>The function is applied lazily, invoked when needed. This is necessary
1618   * for the returned map to be a view, but it means that the function will be
1619   * applied many times for bulk operations like {@link Map#containsValue} and
1620   * {@code Map.toString()}. For this to perform well, {@code function} should
1621   * be fast. To avoid lazy evaluation when the returned map doesn't need to be
1622   * a view, copy the returned map into a new map of your choosing.
1623   *
1624   * @since 11.0
1625   */
1626  public static <K, V1, V2> SortedMap<K, V2> transformValues(
1627      SortedMap<K, V1> fromMap, Function<? super V1, V2> function) {
1628    return transformEntries(fromMap, asEntryTransformer(function));
1629  }
1630
1631  /**
1632   * Returns a view of a navigable map where each value is transformed by a
1633   * function. All other properties of the map, such as iteration order, are
1634   * left intact.  For example, the code: <pre>   {@code
1635   *
1636   *   NavigableMap<String, Integer> map = Maps.newTreeMap();
1637   *   map.put("a", 4);
1638   *   map.put("b", 9);
1639   *   Function<Integer, Double> sqrt =
1640   *       new Function<Integer, Double>() {
1641   *         public Double apply(Integer in) {
1642   *           return Math.sqrt((int) in);
1643   *         }
1644   *       };
1645   *   NavigableMap<String, Double> transformed =
1646   *        Maps.transformNavigableValues(map, sqrt);
1647   *   System.out.println(transformed);}</pre>
1648   *
1649   * ... prints {@code {a=2.0, b=3.0}}.
1650   *
1651   * Changes in the underlying map are reflected in this view.
1652   * Conversely, this view supports removal operations, and these are reflected
1653   * in the underlying map.
1654   *
1655   * <p>It's acceptable for the underlying map to contain null keys, and even
1656   * null values provided that the function is capable of accepting null input.
1657   * The transformed map might contain null values, if the function sometimes
1658   * gives a null result.
1659   *
1660   * <p>The returned map is not thread-safe or serializable, even if the
1661   * underlying map is.
1662   *
1663   * <p>The function is applied lazily, invoked when needed. This is necessary
1664   * for the returned map to be a view, but it means that the function will be
1665   * applied many times for bulk operations like {@link Map#containsValue} and
1666   * {@code Map.toString()}. For this to perform well, {@code function} should
1667   * be fast. To avoid lazy evaluation when the returned map doesn't need to be
1668   * a view, copy the returned map into a new map of your choosing.
1669   *
1670   * @since 13.0
1671   */
1672  @GwtIncompatible // NavigableMap
1673  public static <K, V1, V2> NavigableMap<K, V2> transformValues(
1674      NavigableMap<K, V1> fromMap, Function<? super V1, V2> function) {
1675    return transformEntries(fromMap, asEntryTransformer(function));
1676  }
1677
1678  /**
1679   * Returns a view of a map whose values are derived from the original map's
1680   * entries. In contrast to {@link #transformValues}, this method's
1681   * entry-transformation logic may depend on the key as well as the value.
1682   *
1683   * <p>All other properties of the transformed map, such as iteration order,
1684   * are left intact. For example, the code: <pre>   {@code
1685   *
1686   *   Map<String, Boolean> options =
1687   *       ImmutableMap.of("verbose", true, "sort", false);
1688   *   EntryTransformer<String, Boolean, String> flagPrefixer =
1689   *       new EntryTransformer<String, Boolean, String>() {
1690   *         public String transformEntry(String key, Boolean value) {
1691   *           return value ? key : "no" + key;
1692   *         }
1693   *       };
1694   *   Map<String, String> transformed =
1695   *       Maps.transformEntries(options, flagPrefixer);
1696   *   System.out.println(transformed);}</pre>
1697   *
1698   * ... prints {@code {verbose=verbose, sort=nosort}}.
1699   *
1700   * <p>Changes in the underlying map are reflected in this view. Conversely,
1701   * this view supports removal operations, and these are reflected in the
1702   * underlying map.
1703   *
1704   * <p>It's acceptable for the underlying map to contain null keys and null
1705   * values provided that the transformer is capable of accepting null inputs.
1706   * The transformed map might contain null values if the transformer sometimes
1707   * gives a null result.
1708   *
1709   * <p>The returned map is not thread-safe or serializable, even if the
1710   * underlying map is.
1711   *
1712   * <p>The transformer is applied lazily, invoked when needed. This is
1713   * necessary for the returned map to be a view, but it means that the
1714   * transformer will be applied many times for bulk operations like {@link
1715   * Map#containsValue} and {@link Object#toString}. For this to perform well,
1716   * {@code transformer} should be fast. To avoid lazy evaluation when the
1717   * returned map doesn't need to be a view, copy the returned map into a new
1718   * map of your choosing.
1719   *
1720   * <p><b>Warning:</b> This method assumes that for any instance {@code k} of
1721   * {@code EntryTransformer} key type {@code K}, {@code k.equals(k2)} implies
1722   * that {@code k2} is also of type {@code K}. Using an {@code
1723   * EntryTransformer} key type for which this may not hold, such as {@code
1724   * ArrayList}, may risk a {@code ClassCastException} when calling methods on
1725   * the transformed map.
1726   *
1727   * @since 7.0
1728   */
1729  public static <K, V1, V2> Map<K, V2> transformEntries(
1730      Map<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) {
1731    return new TransformedEntriesMap<K, V1, V2>(fromMap, transformer);
1732  }
1733
1734  /**
1735   * Returns a view of a sorted map whose values are derived from the original
1736   * sorted map's entries. In contrast to {@link #transformValues}, this
1737   * method's entry-transformation logic may depend on the key as well as the
1738   * value.
1739   *
1740   * <p>All other properties of the transformed map, such as iteration order,
1741   * are left intact. For example, the code: <pre>   {@code
1742   *
1743   *   Map<String, Boolean> options =
1744   *       ImmutableSortedMap.of("verbose", true, "sort", false);
1745   *   EntryTransformer<String, Boolean, String> flagPrefixer =
1746   *       new EntryTransformer<String, Boolean, String>() {
1747   *         public String transformEntry(String key, Boolean value) {
1748   *           return value ? key : "yes" + key;
1749   *         }
1750   *       };
1751   *   SortedMap<String, String> transformed =
1752   *       Maps.transformEntries(options, flagPrefixer);
1753   *   System.out.println(transformed);}</pre>
1754   *
1755   * ... prints {@code {sort=yessort, verbose=verbose}}.
1756   *
1757   * <p>Changes in the underlying map are reflected in this view. Conversely,
1758   * this view supports removal operations, and these are reflected in the
1759   * underlying map.
1760   *
1761   * <p>It's acceptable for the underlying map to contain null keys and null
1762   * values provided that the transformer is capable of accepting null inputs.
1763   * The transformed map might contain null values if the transformer sometimes
1764   * gives a null result.
1765   *
1766   * <p>The returned map is not thread-safe or serializable, even if the
1767   * underlying map is.
1768   *
1769   * <p>The transformer is applied lazily, invoked when needed. This is
1770   * necessary for the returned map to be a view, but it means that the
1771   * transformer will be applied many times for bulk operations like {@link
1772   * Map#containsValue} and {@link Object#toString}. For this to perform well,
1773   * {@code transformer} should be fast. To avoid lazy evaluation when the
1774   * returned map doesn't need to be a view, copy the returned map into a new
1775   * map of your choosing.
1776   *
1777   * <p><b>Warning:</b> This method assumes that for any instance {@code k} of
1778   * {@code EntryTransformer} key type {@code K}, {@code k.equals(k2)} implies
1779   * that {@code k2} is also of type {@code K}. Using an {@code
1780   * EntryTransformer} key type for which this may not hold, such as {@code
1781   * ArrayList}, may risk a {@code ClassCastException} when calling methods on
1782   * the transformed map.
1783   *
1784   * @since 11.0
1785   */
1786  public static <K, V1, V2> SortedMap<K, V2> transformEntries(
1787      SortedMap<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) {
1788    return new TransformedEntriesSortedMap<K, V1, V2>(fromMap, transformer);
1789  }
1790
1791  /**
1792   * Returns a view of a navigable map whose values are derived from the
1793   * original navigable map's entries. In contrast to {@link
1794   * #transformValues}, this method's entry-transformation logic may
1795   * depend on the key as well as the value.
1796   *
1797   * <p>All other properties of the transformed map, such as iteration order,
1798   * are left intact. For example, the code: <pre>   {@code
1799   *
1800   *   NavigableMap<String, Boolean> options = Maps.newTreeMap();
1801   *   options.put("verbose", false);
1802   *   options.put("sort", true);
1803   *   EntryTransformer<String, Boolean, String> flagPrefixer =
1804   *       new EntryTransformer<String, Boolean, String>() {
1805   *         public String transformEntry(String key, Boolean value) {
1806   *           return value ? key : ("yes" + key);
1807   *         }
1808   *       };
1809   *   NavigableMap<String, String> transformed =
1810   *       LabsMaps.transformNavigableEntries(options, flagPrefixer);
1811   *   System.out.println(transformed);}</pre>
1812   *
1813   * ... prints {@code {sort=yessort, verbose=verbose}}.
1814   *
1815   * <p>Changes in the underlying map are reflected in this view.
1816   * Conversely, this view supports removal operations, and these are reflected
1817   * in the underlying map.
1818   *
1819   * <p>It's acceptable for the underlying map to contain null keys and null
1820   * values provided that the transformer is capable of accepting null inputs.
1821   * The transformed map might contain null values if the transformer sometimes
1822   * gives a null result.
1823   *
1824   * <p>The returned map is not thread-safe or serializable, even if the
1825   * underlying map is.
1826   *
1827   * <p>The transformer is applied lazily, invoked when needed. This is
1828   * necessary for the returned map to be a view, but it means that the
1829   * transformer will be applied many times for bulk operations like {@link
1830   * Map#containsValue} and {@link Object#toString}. For this to perform well,
1831   * {@code transformer} should be fast. To avoid lazy evaluation when the
1832   * returned map doesn't need to be a view, copy the returned map into a new
1833   * map of your choosing.
1834   *
1835   * <p><b>Warning:</b> This method assumes that for any instance {@code k} of
1836   * {@code EntryTransformer} key type {@code K}, {@code k.equals(k2)} implies
1837   * that {@code k2} is also of type {@code K}. Using an {@code
1838   * EntryTransformer} key type for which this may not hold, such as {@code
1839   * ArrayList}, may risk a {@code ClassCastException} when calling methods on
1840   * the transformed map.
1841   *
1842   * @since 13.0
1843   */
1844  @GwtIncompatible // NavigableMap
1845  public static <K, V1, V2> NavigableMap<K, V2> transformEntries(
1846      NavigableMap<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) {
1847    return new TransformedEntriesNavigableMap<K, V1, V2>(fromMap, transformer);
1848  }
1849
1850  /**
1851   * A transformation of the value of a key-value pair, using both key and value
1852   * as inputs. To apply the transformation to a map, use
1853   * {@link Maps#transformEntries(Map, EntryTransformer)}.
1854   *
1855   * @param <K> the key type of the input and output entries
1856   * @param <V1> the value type of the input entry
1857   * @param <V2> the value type of the output entry
1858   * @since 7.0
1859   */
1860  public interface EntryTransformer<K, V1, V2> {
1861    /**
1862     * Determines an output value based on a key-value pair. This method is
1863     * <i>generally expected</i>, but not absolutely required, to have the
1864     * following properties:
1865     *
1866     * <ul>
1867     * <li>Its execution does not cause any observable side effects.
1868     * <li>The computation is <i>consistent with equals</i>; that is,
1869     *     {@link Objects#equal Objects.equal}{@code (k1, k2) &&}
1870     *     {@link Objects#equal}{@code (v1, v2)} implies that {@code
1871     *     Objects.equal(transformer.transform(k1, v1),
1872     *     transformer.transform(k2, v2))}.
1873     * </ul>
1874     *
1875     * @throws NullPointerException if the key or value is null and this
1876     *     transformer does not accept null arguments
1877     */
1878    V2 transformEntry(@Nullable K key, @Nullable V1 value);
1879  }
1880
1881  /**
1882   * Views a function as an entry transformer that ignores the entry key.
1883   */
1884  static <K, V1, V2> EntryTransformer<K, V1, V2> asEntryTransformer(
1885      final Function<? super V1, V2> function) {
1886    checkNotNull(function);
1887    return new EntryTransformer<K, V1, V2>() {
1888      @Override
1889      public V2 transformEntry(K key, V1 value) {
1890        return function.apply(value);
1891      }
1892    };
1893  }
1894
1895  static <K, V1, V2> Function<V1, V2> asValueToValueFunction(
1896      final EntryTransformer<? super K, V1, V2> transformer, final K key) {
1897    checkNotNull(transformer);
1898    return new Function<V1, V2>() {
1899      @Override
1900      public V2 apply(@Nullable V1 v1) {
1901        return transformer.transformEntry(key, v1);
1902      }
1903    };
1904  }
1905
1906  /**
1907   * Views an entry transformer as a function from {@code Entry} to values.
1908   */
1909  static <K, V1, V2> Function<Entry<K, V1>, V2> asEntryToValueFunction(
1910      final EntryTransformer<? super K, ? super V1, V2> transformer) {
1911    checkNotNull(transformer);
1912    return new Function<Entry<K, V1>, V2>() {
1913      @Override
1914      public V2 apply(Entry<K, V1> entry) {
1915        return transformer.transformEntry(entry.getKey(), entry.getValue());
1916      }
1917    };
1918  }
1919
1920  /**
1921   * Returns a view of an entry transformed by the specified transformer.
1922   */
1923  static <V2, K, V1> Entry<K, V2> transformEntry(
1924      final EntryTransformer<? super K, ? super V1, V2> transformer, final Entry<K, V1> entry) {
1925    checkNotNull(transformer);
1926    checkNotNull(entry);
1927    return new AbstractMapEntry<K, V2>() {
1928      @Override
1929      public K getKey() {
1930        return entry.getKey();
1931      }
1932
1933      @Override
1934      public V2 getValue() {
1935        return transformer.transformEntry(entry.getKey(), entry.getValue());
1936      }
1937    };
1938  }
1939
1940  /**
1941   * Views an entry transformer as a function from entries to entries.
1942   */
1943  static <K, V1, V2> Function<Entry<K, V1>, Entry<K, V2>> asEntryToEntryFunction(
1944      final EntryTransformer<? super K, ? super V1, V2> transformer) {
1945    checkNotNull(transformer);
1946    return new Function<Entry<K, V1>, Entry<K, V2>>() {
1947      @Override
1948      public Entry<K, V2> apply(final Entry<K, V1> entry) {
1949        return transformEntry(transformer, entry);
1950      }
1951    };
1952  }
1953
1954  static class TransformedEntriesMap<K, V1, V2> extends IteratorBasedAbstractMap<K, V2> {
1955    final Map<K, V1> fromMap;
1956    final EntryTransformer<? super K, ? super V1, V2> transformer;
1957
1958    TransformedEntriesMap(
1959        Map<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) {
1960      this.fromMap = checkNotNull(fromMap);
1961      this.transformer = checkNotNull(transformer);
1962    }
1963
1964    @Override
1965    public int size() {
1966      return fromMap.size();
1967    }
1968
1969    @Override
1970    public boolean containsKey(Object key) {
1971      return fromMap.containsKey(key);
1972    }
1973
1974    // safe as long as the user followed the <b>Warning</b> in the javadoc
1975    @SuppressWarnings("unchecked")
1976    @Override
1977    public V2 get(Object key) {
1978      V1 value = fromMap.get(key);
1979      return (value != null || fromMap.containsKey(key))
1980          ? transformer.transformEntry((K) key, value)
1981          : null;
1982    }
1983
1984    // safe as long as the user followed the <b>Warning</b> in the javadoc
1985    @SuppressWarnings("unchecked")
1986    @Override
1987    public V2 remove(Object key) {
1988      return fromMap.containsKey(key)
1989          ? transformer.transformEntry((K) key, fromMap.remove(key))
1990          : null;
1991    }
1992
1993    @Override
1994    public void clear() {
1995      fromMap.clear();
1996    }
1997
1998    @Override
1999    public Set<K> keySet() {
2000      return fromMap.keySet();
2001    }
2002
2003    @Override
2004    Iterator<Entry<K, V2>> entryIterator() {
2005      return Iterators.transform(
2006          fromMap.entrySet().iterator(), Maps.<K, V1, V2>asEntryToEntryFunction(transformer));
2007    }
2008
2009    @Override
2010    public Collection<V2> values() {
2011      return new Values<K, V2>(this);
2012    }
2013  }
2014
2015  static class TransformedEntriesSortedMap<K, V1, V2> extends TransformedEntriesMap<K, V1, V2>
2016      implements SortedMap<K, V2> {
2017
2018    protected SortedMap<K, V1> fromMap() {
2019      return (SortedMap<K, V1>) fromMap;
2020    }
2021
2022    TransformedEntriesSortedMap(
2023        SortedMap<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) {
2024      super(fromMap, transformer);
2025    }
2026
2027    @Override
2028    public Comparator<? super K> comparator() {
2029      return fromMap().comparator();
2030    }
2031
2032    @Override
2033    public K firstKey() {
2034      return fromMap().firstKey();
2035    }
2036
2037    @Override
2038    public SortedMap<K, V2> headMap(K toKey) {
2039      return transformEntries(fromMap().headMap(toKey), transformer);
2040    }
2041
2042    @Override
2043    public K lastKey() {
2044      return fromMap().lastKey();
2045    }
2046
2047    @Override
2048    public SortedMap<K, V2> subMap(K fromKey, K toKey) {
2049      return transformEntries(fromMap().subMap(fromKey, toKey), transformer);
2050    }
2051
2052    @Override
2053    public SortedMap<K, V2> tailMap(K fromKey) {
2054      return transformEntries(fromMap().tailMap(fromKey), transformer);
2055    }
2056  }
2057
2058  @GwtIncompatible // NavigableMap
2059  private static class TransformedEntriesNavigableMap<K, V1, V2>
2060      extends TransformedEntriesSortedMap<K, V1, V2> implements NavigableMap<K, V2> {
2061
2062    TransformedEntriesNavigableMap(
2063        NavigableMap<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) {
2064      super(fromMap, transformer);
2065    }
2066
2067    @Override
2068    public Entry<K, V2> ceilingEntry(K key) {
2069      return transformEntry(fromMap().ceilingEntry(key));
2070    }
2071
2072    @Override
2073    public K ceilingKey(K key) {
2074      return fromMap().ceilingKey(key);
2075    }
2076
2077    @Override
2078    public NavigableSet<K> descendingKeySet() {
2079      return fromMap().descendingKeySet();
2080    }
2081
2082    @Override
2083    public NavigableMap<K, V2> descendingMap() {
2084      return transformEntries(fromMap().descendingMap(), transformer);
2085    }
2086
2087    @Override
2088    public Entry<K, V2> firstEntry() {
2089      return transformEntry(fromMap().firstEntry());
2090    }
2091
2092    @Override
2093    public Entry<K, V2> floorEntry(K key) {
2094      return transformEntry(fromMap().floorEntry(key));
2095    }
2096
2097    @Override
2098    public K floorKey(K key) {
2099      return fromMap().floorKey(key);
2100    }
2101
2102    @Override
2103    public NavigableMap<K, V2> headMap(K toKey) {
2104      return headMap(toKey, false);
2105    }
2106
2107    @Override
2108    public NavigableMap<K, V2> headMap(K toKey, boolean inclusive) {
2109      return transformEntries(fromMap().headMap(toKey, inclusive), transformer);
2110    }
2111
2112    @Override
2113    public Entry<K, V2> higherEntry(K key) {
2114      return transformEntry(fromMap().higherEntry(key));
2115    }
2116
2117    @Override
2118    public K higherKey(K key) {
2119      return fromMap().higherKey(key);
2120    }
2121
2122    @Override
2123    public Entry<K, V2> lastEntry() {
2124      return transformEntry(fromMap().lastEntry());
2125    }
2126
2127    @Override
2128    public Entry<K, V2> lowerEntry(K key) {
2129      return transformEntry(fromMap().lowerEntry(key));
2130    }
2131
2132    @Override
2133    public K lowerKey(K key) {
2134      return fromMap().lowerKey(key);
2135    }
2136
2137    @Override
2138    public NavigableSet<K> navigableKeySet() {
2139      return fromMap().navigableKeySet();
2140    }
2141
2142    @Override
2143    public Entry<K, V2> pollFirstEntry() {
2144      return transformEntry(fromMap().pollFirstEntry());
2145    }
2146
2147    @Override
2148    public Entry<K, V2> pollLastEntry() {
2149      return transformEntry(fromMap().pollLastEntry());
2150    }
2151
2152    @Override
2153    public NavigableMap<K, V2> subMap(
2154        K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
2155      return transformEntries(
2156          fromMap().subMap(fromKey, fromInclusive, toKey, toInclusive), transformer);
2157    }
2158
2159    @Override
2160    public NavigableMap<K, V2> subMap(K fromKey, K toKey) {
2161      return subMap(fromKey, true, toKey, false);
2162    }
2163
2164    @Override
2165    public NavigableMap<K, V2> tailMap(K fromKey) {
2166      return tailMap(fromKey, true);
2167    }
2168
2169    @Override
2170    public NavigableMap<K, V2> tailMap(K fromKey, boolean inclusive) {
2171      return transformEntries(fromMap().tailMap(fromKey, inclusive), transformer);
2172    }
2173
2174    @Nullable
2175    private Entry<K, V2> transformEntry(@Nullable Entry<K, V1> entry) {
2176      return (entry == null) ? null : Maps.transformEntry(transformer, entry);
2177    }
2178
2179    @Override
2180    protected NavigableMap<K, V1> fromMap() {
2181      return (NavigableMap<K, V1>) super.fromMap();
2182    }
2183  }
2184
2185  static <K> Predicate<Entry<K, ?>> keyPredicateOnEntries(Predicate<? super K> keyPredicate) {
2186    return compose(keyPredicate, Maps.<K>keyFunction());
2187  }
2188
2189  static <V> Predicate<Entry<?, V>> valuePredicateOnEntries(Predicate<? super V> valuePredicate) {
2190    return compose(valuePredicate, Maps.<V>valueFunction());
2191  }
2192
2193  /**
2194   * Returns a map containing the mappings in {@code unfiltered} whose keys
2195   * satisfy a predicate. The returned map is a live view of {@code unfiltered};
2196   * changes to one affect the other.
2197   *
2198   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2199   * values()} views have iterators that don't support {@code remove()}, but all
2200   * other methods are supported by the map and its views. When given a key that
2201   * doesn't satisfy the predicate, the map's {@code put()} and {@code putAll()}
2202   * methods throw an {@link IllegalArgumentException}.
2203   *
2204   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2205   * on the filtered map or its views, only mappings whose keys satisfy the
2206   * filter will be removed from the underlying map.
2207   *
2208   * <p>The returned map isn't threadsafe or serializable, even if {@code
2209   * unfiltered} is.
2210   *
2211   * <p>Many of the filtered map's methods, such as {@code size()},
2212   * iterate across every key/value mapping in the underlying map and determine
2213   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2214   * faster to copy the filtered map and use the copy.
2215   *
2216   * <p><b>Warning:</b> {@code keyPredicate} must be <i>consistent with
2217   * equals</i>, as documented at {@link Predicate#apply}. Do not provide a
2218   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is
2219   * inconsistent with equals.
2220   */
2221  public static <K, V> Map<K, V> filterKeys(
2222      Map<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
2223    checkNotNull(keyPredicate);
2224    Predicate<Entry<K, ?>> entryPredicate = keyPredicateOnEntries(keyPredicate);
2225    return (unfiltered instanceof AbstractFilteredMap)
2226        ? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate)
2227        : new FilteredKeyMap<K, V>(checkNotNull(unfiltered), keyPredicate, entryPredicate);
2228  }
2229
2230  /**
2231   * Returns a sorted map containing the mappings in {@code unfiltered} whose
2232   * keys satisfy a predicate. The returned map is a live view of {@code
2233   * unfiltered}; changes to one affect the other.
2234   *
2235   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2236   * values()} views have iterators that don't support {@code remove()}, but all
2237   * other methods are supported by the map and its views. When given a key that
2238   * doesn't satisfy the predicate, the map's {@code put()} and {@code putAll()}
2239   * methods throw an {@link IllegalArgumentException}.
2240   *
2241   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2242   * on the filtered map or its views, only mappings whose keys satisfy the
2243   * filter will be removed from the underlying map.
2244   *
2245   * <p>The returned map isn't threadsafe or serializable, even if {@code
2246   * unfiltered} is.
2247   *
2248   * <p>Many of the filtered map's methods, such as {@code size()},
2249   * iterate across every key/value mapping in the underlying map and determine
2250   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2251   * faster to copy the filtered map and use the copy.
2252   *
2253   * <p><b>Warning:</b> {@code keyPredicate} must be <i>consistent with
2254   * equals</i>, as documented at {@link Predicate#apply}. Do not provide a
2255   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is
2256   * inconsistent with equals.
2257   *
2258   * @since 11.0
2259   */
2260  public static <K, V> SortedMap<K, V> filterKeys(
2261      SortedMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
2262    // TODO(lowasser): Return a subclass of Maps.FilteredKeyMap for slightly better
2263    // performance.
2264    return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate));
2265  }
2266
2267  /**
2268   * Returns a navigable map containing the mappings in {@code unfiltered} whose
2269   * keys satisfy a predicate. The returned map is a live view of {@code
2270   * unfiltered}; changes to one affect the other.
2271   *
2272   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2273   * values()} views have iterators that don't support {@code remove()}, but all
2274   * other methods are supported by the map and its views. When given a key that
2275   * doesn't satisfy the predicate, the map's {@code put()} and {@code putAll()}
2276   * methods throw an {@link IllegalArgumentException}.
2277   *
2278   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2279   * on the filtered map or its views, only mappings whose keys satisfy the
2280   * filter will be removed from the underlying map.
2281   *
2282   * <p>The returned map isn't threadsafe or serializable, even if {@code
2283   * unfiltered} is.
2284   *
2285   * <p>Many of the filtered map's methods, such as {@code size()},
2286   * iterate across every key/value mapping in the underlying map and determine
2287   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2288   * faster to copy the filtered map and use the copy.
2289   *
2290   * <p><b>Warning:</b> {@code keyPredicate} must be <i>consistent with
2291   * equals</i>, as documented at {@link Predicate#apply}. Do not provide a
2292   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is
2293   * inconsistent with equals.
2294   *
2295   * @since 14.0
2296   */
2297  @GwtIncompatible // NavigableMap
2298  public static <K, V> NavigableMap<K, V> filterKeys(
2299      NavigableMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
2300    // TODO(lowasser): Return a subclass of Maps.FilteredKeyMap for slightly better
2301    // performance.
2302    return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate));
2303  }
2304
2305  /**
2306   * Returns a bimap containing the mappings in {@code unfiltered} whose keys satisfy a predicate.
2307   * The returned bimap is a live view of {@code unfiltered}; changes to one affect the other.
2308   *
2309   * <p>The resulting bimap's {@code keySet()}, {@code entrySet()}, and {@code values()} views have
2310   * iterators that don't support {@code remove()}, but all other methods are supported by the
2311   * bimap and its views. When given a key that doesn't satisfy the predicate, the bimap's {@code
2312   * put()}, {@code forcePut()} and {@code putAll()} methods throw an {@link
2313   * IllegalArgumentException}.
2314   *
2315   * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered
2316   * bimap or its views, only mappings that satisfy the filter will be removed from the underlying
2317   * bimap.
2318   *
2319   * <p>The returned bimap isn't threadsafe or serializable, even if {@code unfiltered} is.
2320   *
2321   * <p>Many of the filtered bimap's methods, such as {@code size()}, iterate across every key in
2322   * the underlying bimap and determine which satisfy the filter. When a live view is <i>not</i>
2323   * needed, it may be faster to copy the filtered bimap and use the copy.
2324   *
2325   * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with equals </i>, as
2326   * documented at {@link Predicate#apply}.
2327   *
2328   * @since 14.0
2329   */
2330  public static <K, V> BiMap<K, V> filterKeys(
2331      BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
2332    checkNotNull(keyPredicate);
2333    return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate));
2334  }
2335
2336  /**
2337   * Returns a map containing the mappings in {@code unfiltered} whose values
2338   * satisfy a predicate. The returned map is a live view of {@code unfiltered};
2339   * changes to one affect the other.
2340   *
2341   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2342   * values()} views have iterators that don't support {@code remove()}, but all
2343   * other methods are supported by the map and its views. When given a value
2344   * that doesn't satisfy the predicate, the map's {@code put()}, {@code
2345   * putAll()}, and {@link Entry#setValue} methods throw an {@link
2346   * IllegalArgumentException}.
2347   *
2348   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2349   * on the filtered map or its views, only mappings whose values satisfy the
2350   * filter will be removed from the underlying map.
2351   *
2352   * <p>The returned map isn't threadsafe or serializable, even if {@code
2353   * unfiltered} is.
2354   *
2355   * <p>Many of the filtered map's methods, such as {@code size()},
2356   * iterate across every key/value mapping in the underlying map and determine
2357   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2358   * faster to copy the filtered map and use the copy.
2359   *
2360   * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with
2361   * equals</i>, as documented at {@link Predicate#apply}. Do not provide a
2362   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is
2363   * inconsistent with equals.
2364   */
2365  public static <K, V> Map<K, V> filterValues(
2366      Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
2367    return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
2368  }
2369
2370  /**
2371   * Returns a sorted map containing the mappings in {@code unfiltered} whose
2372   * values satisfy a predicate. The returned map is a live view of {@code
2373   * unfiltered}; changes to one affect the other.
2374   *
2375   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2376   * values()} views have iterators that don't support {@code remove()}, but all
2377   * other methods are supported by the map and its views. When given a value
2378   * that doesn't satisfy the predicate, the map's {@code put()}, {@code
2379   * putAll()}, and {@link Entry#setValue} methods throw an {@link
2380   * IllegalArgumentException}.
2381   *
2382   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2383   * on the filtered map or its views, only mappings whose values satisfy the
2384   * filter will be removed from the underlying map.
2385   *
2386   * <p>The returned map isn't threadsafe or serializable, even if {@code
2387   * unfiltered} is.
2388   *
2389   * <p>Many of the filtered map's methods, such as {@code size()},
2390   * iterate across every key/value mapping in the underlying map and determine
2391   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2392   * faster to copy the filtered map and use the copy.
2393   *
2394   * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with
2395   * equals</i>, as documented at {@link Predicate#apply}. Do not provide a
2396   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is
2397   * inconsistent with equals.
2398   *
2399   * @since 11.0
2400   */
2401  public static <K, V> SortedMap<K, V> filterValues(
2402      SortedMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
2403    return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
2404  }
2405
2406  /**
2407   * Returns a navigable map containing the mappings in {@code unfiltered} whose
2408   * values satisfy a predicate. The returned map is a live view of {@code
2409   * unfiltered}; changes to one affect the other.
2410   *
2411   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2412   * values()} views have iterators that don't support {@code remove()}, but all
2413   * other methods are supported by the map and its views. When given a value
2414   * that doesn't satisfy the predicate, the map's {@code put()}, {@code
2415   * putAll()}, and {@link Entry#setValue} methods throw an {@link
2416   * IllegalArgumentException}.
2417   *
2418   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2419   * on the filtered map or its views, only mappings whose values satisfy the
2420   * filter will be removed from the underlying map.
2421   *
2422   * <p>The returned map isn't threadsafe or serializable, even if {@code
2423   * unfiltered} is.
2424   *
2425   * <p>Many of the filtered map's methods, such as {@code size()},
2426   * iterate across every key/value mapping in the underlying map and determine
2427   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2428   * faster to copy the filtered map and use the copy.
2429   *
2430   * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with
2431   * equals</i>, as documented at {@link Predicate#apply}. Do not provide a
2432   * predicate such as {@code Predicates.instanceOf(ArrayList.class)}, which is
2433   * inconsistent with equals.
2434   *
2435   * @since 14.0
2436   */
2437  @GwtIncompatible // NavigableMap
2438  public static <K, V> NavigableMap<K, V> filterValues(
2439      NavigableMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
2440    return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
2441  }
2442
2443  /**
2444   * Returns a bimap containing the mappings in {@code unfiltered} whose values satisfy a
2445   * predicate. The returned bimap is a live view of {@code unfiltered}; changes to one affect the
2446   * other.
2447   *
2448   * <p>The resulting bimap's {@code keySet()}, {@code entrySet()}, and {@code values()} views have
2449   * iterators that don't support {@code remove()}, but all other methods are supported by the
2450   * bimap and its views. When given a value that doesn't satisfy the predicate, the bimap's
2451   * {@code put()}, {@code forcePut()} and {@code putAll()} methods throw an {@link
2452   * IllegalArgumentException}. Similarly, the map's entries have a {@link Entry#setValue} method
2453   * that throws an {@link IllegalArgumentException} when the provided value doesn't satisfy the
2454   * predicate.
2455   *
2456   * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered
2457   * bimap or its views, only mappings that satisfy the filter will be removed from the underlying
2458   * bimap.
2459   *
2460   * <p>The returned bimap isn't threadsafe or serializable, even if {@code unfiltered} is.
2461   *
2462   * <p>Many of the filtered bimap's methods, such as {@code size()}, iterate across every value in
2463   * the underlying bimap and determine which satisfy the filter. When a live view is <i>not</i>
2464   * needed, it may be faster to copy the filtered bimap and use the copy.
2465   *
2466   * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with equals </i>, as
2467   * documented at {@link Predicate#apply}.
2468   *
2469   * @since 14.0
2470   */
2471  public static <K, V> BiMap<K, V> filterValues(
2472      BiMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
2473    return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
2474  }
2475
2476  /**
2477   * Returns a map containing the mappings in {@code unfiltered} that satisfy a
2478   * predicate. The returned map is a live view of {@code unfiltered}; changes
2479   * to one affect the other.
2480   *
2481   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2482   * values()} views have iterators that don't support {@code remove()}, but all
2483   * other methods are supported by the map and its views. When given a
2484   * key/value pair that doesn't satisfy the predicate, the map's {@code put()}
2485   * and {@code putAll()} methods throw an {@link IllegalArgumentException}.
2486   * Similarly, the map's entries have a {@link Entry#setValue} method that
2487   * throws an {@link IllegalArgumentException} when the existing key and the
2488   * provided value don't satisfy the predicate.
2489   *
2490   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2491   * on the filtered map or its views, only mappings that satisfy the filter
2492   * will be removed from the underlying map.
2493   *
2494   * <p>The returned map isn't threadsafe or serializable, even if {@code
2495   * unfiltered} is.
2496   *
2497   * <p>Many of the filtered map's methods, such as {@code size()},
2498   * iterate across every key/value mapping in the underlying map and determine
2499   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2500   * faster to copy the filtered map and use the copy.
2501   *
2502   * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with
2503   * equals</i>, as documented at {@link Predicate#apply}.
2504   */
2505  public static <K, V> Map<K, V> filterEntries(
2506      Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2507    checkNotNull(entryPredicate);
2508    return (unfiltered instanceof AbstractFilteredMap)
2509        ? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate)
2510        : new FilteredEntryMap<K, V>(checkNotNull(unfiltered), entryPredicate);
2511  }
2512
2513  /**
2514   * Returns a sorted map containing the mappings in {@code unfiltered} that
2515   * satisfy a predicate. The returned map is a live view of {@code unfiltered};
2516   * changes to one affect the other.
2517   *
2518   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2519   * values()} views have iterators that don't support {@code remove()}, but all
2520   * other methods are supported by the map and its views. When given a
2521   * key/value pair that doesn't satisfy the predicate, the map's {@code put()}
2522   * and {@code putAll()} methods throw an {@link IllegalArgumentException}.
2523   * Similarly, the map's entries have a {@link Entry#setValue} method that
2524   * throws an {@link IllegalArgumentException} when the existing key and the
2525   * provided value don't satisfy the predicate.
2526   *
2527   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2528   * on the filtered map or its views, only mappings that satisfy the filter
2529   * will be removed from the underlying map.
2530   *
2531   * <p>The returned map isn't threadsafe or serializable, even if {@code
2532   * unfiltered} is.
2533   *
2534   * <p>Many of the filtered map's methods, such as {@code size()},
2535   * iterate across every key/value mapping in the underlying map and determine
2536   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2537   * faster to copy the filtered map and use the copy.
2538   *
2539   * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with
2540   * equals</i>, as documented at {@link Predicate#apply}.
2541   *
2542   * @since 11.0
2543   */
2544  public static <K, V> SortedMap<K, V> filterEntries(
2545      SortedMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2546    checkNotNull(entryPredicate);
2547    return (unfiltered instanceof FilteredEntrySortedMap)
2548        ? filterFiltered((FilteredEntrySortedMap<K, V>) unfiltered, entryPredicate)
2549        : new FilteredEntrySortedMap<K, V>(checkNotNull(unfiltered), entryPredicate);
2550  }
2551
2552  /**
2553   * Returns a sorted map containing the mappings in {@code unfiltered} that
2554   * satisfy a predicate. The returned map is a live view of {@code unfiltered};
2555   * changes to one affect the other.
2556   *
2557   * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code
2558   * values()} views have iterators that don't support {@code remove()}, but all
2559   * other methods are supported by the map and its views. When given a
2560   * key/value pair that doesn't satisfy the predicate, the map's {@code put()}
2561   * and {@code putAll()} methods throw an {@link IllegalArgumentException}.
2562   * Similarly, the map's entries have a {@link Entry#setValue} method that
2563   * throws an {@link IllegalArgumentException} when the existing key and the
2564   * provided value don't satisfy the predicate.
2565   *
2566   * <p>When methods such as {@code removeAll()} and {@code clear()} are called
2567   * on the filtered map or its views, only mappings that satisfy the filter
2568   * will be removed from the underlying map.
2569   *
2570   * <p>The returned map isn't threadsafe or serializable, even if {@code
2571   * unfiltered} is.
2572   *
2573   * <p>Many of the filtered map's methods, such as {@code size()},
2574   * iterate across every key/value mapping in the underlying map and determine
2575   * which satisfy the filter. When a live view is <i>not</i> needed, it may be
2576   * faster to copy the filtered map and use the copy.
2577   *
2578   * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with
2579   * equals</i>, as documented at {@link Predicate#apply}.
2580   *
2581   * @since 14.0
2582   */
2583  @GwtIncompatible // NavigableMap
2584  public static <K, V> NavigableMap<K, V> filterEntries(
2585      NavigableMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2586    checkNotNull(entryPredicate);
2587    return (unfiltered instanceof FilteredEntryNavigableMap)
2588        ? filterFiltered((FilteredEntryNavigableMap<K, V>) unfiltered, entryPredicate)
2589        : new FilteredEntryNavigableMap<K, V>(checkNotNull(unfiltered), entryPredicate);
2590  }
2591
2592  /**
2593   * Returns a bimap containing the mappings in {@code unfiltered} that satisfy a predicate. The
2594   * returned bimap is a live view of {@code unfiltered}; changes to one affect the other.
2595   *
2596   * <p>The resulting bimap's {@code keySet()}, {@code entrySet()}, and {@code values()} views have
2597   * iterators that don't support {@code remove()}, but all other methods are supported by the bimap
2598   * and its views. When given a key/value pair that doesn't satisfy the predicate, the bimap's
2599   * {@code put()}, {@code forcePut()} and {@code putAll()} methods throw an
2600   * {@link IllegalArgumentException}. Similarly, the map's entries have an {@link Entry#setValue}
2601   * method that throws an {@link IllegalArgumentException} when the existing key and the provided
2602   * value don't satisfy the predicate.
2603   *
2604   * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered
2605   * bimap or its views, only mappings that satisfy the filter will be removed from the underlying
2606   * bimap.
2607   *
2608   * <p>The returned bimap isn't threadsafe or serializable, even if {@code unfiltered} is.
2609   *
2610   * <p>Many of the filtered bimap's methods, such as {@code size()}, iterate across every
2611   * key/value mapping in the underlying bimap and determine which satisfy the filter. When a live
2612   * view is <i>not</i> needed, it may be faster to copy the filtered bimap and use the copy.
2613   *
2614   * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with equals </i>, as
2615   * documented at {@link Predicate#apply}.
2616   *
2617   * @since 14.0
2618   */
2619  public static <K, V> BiMap<K, V> filterEntries(
2620      BiMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2621    checkNotNull(unfiltered);
2622    checkNotNull(entryPredicate);
2623    return (unfiltered instanceof FilteredEntryBiMap)
2624        ? filterFiltered((FilteredEntryBiMap<K, V>) unfiltered, entryPredicate)
2625        : new FilteredEntryBiMap<K, V>(unfiltered, entryPredicate);
2626  }
2627
2628  /**
2629   * Support {@code clear()}, {@code removeAll()}, and {@code retainAll()} when
2630   * filtering a filtered map.
2631   */
2632  private static <K, V> Map<K, V> filterFiltered(
2633      AbstractFilteredMap<K, V> map, Predicate<? super Entry<K, V>> entryPredicate) {
2634    return new FilteredEntryMap<K, V>(
2635        map.unfiltered, Predicates.<Entry<K, V>>and(map.predicate, entryPredicate));
2636  }
2637
2638  private abstract static class AbstractFilteredMap<K, V> extends ViewCachingAbstractMap<K, V> {
2639    final Map<K, V> unfiltered;
2640    final Predicate<? super Entry<K, V>> predicate;
2641
2642    AbstractFilteredMap(Map<K, V> unfiltered, Predicate<? super Entry<K, V>> predicate) {
2643      this.unfiltered = unfiltered;
2644      this.predicate = predicate;
2645    }
2646
2647    boolean apply(@Nullable Object key, @Nullable V value) {
2648      // This method is called only when the key is in the map, implying that
2649      // key is a K.
2650      @SuppressWarnings("unchecked")
2651      K k = (K) key;
2652      return predicate.apply(Maps.immutableEntry(k, value));
2653    }
2654
2655    @Override
2656    public V put(K key, V value) {
2657      checkArgument(apply(key, value));
2658      return unfiltered.put(key, value);
2659    }
2660
2661    @Override
2662    public void putAll(Map<? extends K, ? extends V> map) {
2663      for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
2664        checkArgument(apply(entry.getKey(), entry.getValue()));
2665      }
2666      unfiltered.putAll(map);
2667    }
2668
2669    @Override
2670    public boolean containsKey(Object key) {
2671      return unfiltered.containsKey(key) && apply(key, unfiltered.get(key));
2672    }
2673
2674    @Override
2675    public V get(Object key) {
2676      V value = unfiltered.get(key);
2677      return ((value != null) && apply(key, value)) ? value : null;
2678    }
2679
2680    @Override
2681    public boolean isEmpty() {
2682      return entrySet().isEmpty();
2683    }
2684
2685    @Override
2686    public V remove(Object key) {
2687      return containsKey(key) ? unfiltered.remove(key) : null;
2688    }
2689
2690    @Override
2691    Collection<V> createValues() {
2692      return new FilteredMapValues<K, V>(this, unfiltered, predicate);
2693    }
2694  }
2695
2696  private static final class FilteredMapValues<K, V> extends Maps.Values<K, V> {
2697    final Map<K, V> unfiltered;
2698    final Predicate<? super Entry<K, V>> predicate;
2699
2700    FilteredMapValues(
2701        Map<K, V> filteredMap, Map<K, V> unfiltered, Predicate<? super Entry<K, V>> predicate) {
2702      super(filteredMap);
2703      this.unfiltered = unfiltered;
2704      this.predicate = predicate;
2705    }
2706
2707    @Override
2708    public boolean remove(Object o) {
2709      Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator();
2710      while (entryItr.hasNext()) {
2711        Entry<K, V> entry = entryItr.next();
2712        if (predicate.apply(entry) && Objects.equal(entry.getValue(), o)) {
2713          entryItr.remove();
2714          return true;
2715        }
2716      }
2717      return false;
2718    }
2719
2720    @Override
2721    public boolean removeAll(Collection<?> collection) {
2722      Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator();
2723      boolean result = false;
2724      while (entryItr.hasNext()) {
2725        Entry<K, V> entry = entryItr.next();
2726        if (predicate.apply(entry) && collection.contains(entry.getValue())) {
2727          entryItr.remove();
2728          result = true;
2729        }
2730      }
2731      return result;
2732    }
2733
2734    @Override
2735    public boolean retainAll(Collection<?> collection) {
2736      Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator();
2737      boolean result = false;
2738      while (entryItr.hasNext()) {
2739        Entry<K, V> entry = entryItr.next();
2740        if (predicate.apply(entry) && !collection.contains(entry.getValue())) {
2741          entryItr.remove();
2742          result = true;
2743        }
2744      }
2745      return result;
2746    }
2747
2748    @Override
2749    public Object[] toArray() {
2750      // creating an ArrayList so filtering happens once
2751      return Lists.newArrayList(iterator()).toArray();
2752    }
2753
2754    @Override
2755    public <T> T[] toArray(T[] array) {
2756      return Lists.newArrayList(iterator()).toArray(array);
2757    }
2758  }
2759
2760  private static class FilteredKeyMap<K, V> extends AbstractFilteredMap<K, V> {
2761    final Predicate<? super K> keyPredicate;
2762
2763    FilteredKeyMap(
2764        Map<K, V> unfiltered,
2765        Predicate<? super K> keyPredicate,
2766        Predicate<? super Entry<K, V>> entryPredicate) {
2767      super(unfiltered, entryPredicate);
2768      this.keyPredicate = keyPredicate;
2769    }
2770
2771    @Override
2772    protected Set<Entry<K, V>> createEntrySet() {
2773      return Sets.filter(unfiltered.entrySet(), predicate);
2774    }
2775
2776    @Override
2777    Set<K> createKeySet() {
2778      return Sets.filter(unfiltered.keySet(), keyPredicate);
2779    }
2780
2781    // The cast is called only when the key is in the unfiltered map, implying
2782    // that key is a K.
2783    @Override
2784    @SuppressWarnings("unchecked")
2785    public boolean containsKey(Object key) {
2786      return unfiltered.containsKey(key) && keyPredicate.apply((K) key);
2787    }
2788  }
2789
2790  static class FilteredEntryMap<K, V> extends AbstractFilteredMap<K, V> {
2791    /**
2792     * Entries in this set satisfy the predicate, but they don't validate the
2793     * input to {@code Entry.setValue()}.
2794     */
2795    final Set<Entry<K, V>> filteredEntrySet;
2796
2797    FilteredEntryMap(Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2798      super(unfiltered, entryPredicate);
2799      filteredEntrySet = Sets.filter(unfiltered.entrySet(), predicate);
2800    }
2801
2802    @Override
2803    protected Set<Entry<K, V>> createEntrySet() {
2804      return new EntrySet();
2805    }
2806
2807    @WeakOuter
2808    private class EntrySet extends ForwardingSet<Entry<K, V>> {
2809      @Override
2810      protected Set<Entry<K, V>> delegate() {
2811        return filteredEntrySet;
2812      }
2813
2814      @Override
2815      public Iterator<Entry<K, V>> iterator() {
2816        return new TransformedIterator<Entry<K, V>, Entry<K, V>>(filteredEntrySet.iterator()) {
2817          @Override
2818          Entry<K, V> transform(final Entry<K, V> entry) {
2819            return new ForwardingMapEntry<K, V>() {
2820              @Override
2821              protected Entry<K, V> delegate() {
2822                return entry;
2823              }
2824
2825              @Override
2826              public V setValue(V newValue) {
2827                checkArgument(apply(getKey(), newValue));
2828                return super.setValue(newValue);
2829              }
2830            };
2831          }
2832        };
2833      }
2834    }
2835
2836    @Override
2837    Set<K> createKeySet() {
2838      return new KeySet();
2839    }
2840    
2841    static <K, V> boolean removeAllKeys(
2842        Map<K, V> map, Predicate<? super Entry<K, V>> entryPredicate, Collection<?> keyCollection) {
2843      Iterator<Entry<K, V>> entryItr = map.entrySet().iterator();
2844      boolean result = false;
2845      while (entryItr.hasNext()) {
2846        Entry<K, V> entry = entryItr.next();
2847        if (entryPredicate.apply(entry) && keyCollection.contains(entry.getKey())) {
2848          entryItr.remove();
2849          result = true;
2850        }
2851      }
2852      return result;
2853    }
2854    
2855    static <K, V> boolean retainAllKeys(
2856        Map<K, V> map, Predicate<? super Entry<K, V>> entryPredicate, Collection<?> keyCollection) {
2857      Iterator<Entry<K, V>> entryItr = map.entrySet().iterator();
2858      boolean result = false;
2859      while (entryItr.hasNext()) {
2860        Entry<K, V> entry = entryItr.next();
2861        if (entryPredicate.apply(entry) && !keyCollection.contains(entry.getKey())) {
2862          entryItr.remove();
2863          result = true;
2864        }
2865      }
2866      return result;
2867    }
2868
2869    @WeakOuter
2870    class KeySet extends Maps.KeySet<K, V> {
2871      KeySet() {
2872        super(FilteredEntryMap.this);
2873      }
2874
2875      @Override
2876      public boolean remove(Object o) {
2877        if (containsKey(o)) {
2878          unfiltered.remove(o);
2879          return true;
2880        }
2881        return false;
2882      }
2883
2884      @Override
2885      public boolean removeAll(Collection<?> collection) {
2886        return removeAllKeys(unfiltered, predicate, collection);
2887      }
2888
2889      @Override
2890      public boolean retainAll(Collection<?> collection) {
2891        return retainAllKeys(unfiltered, predicate, collection);
2892      }
2893
2894      @Override
2895      public Object[] toArray() {
2896        // creating an ArrayList so filtering happens once
2897        return Lists.newArrayList(iterator()).toArray();
2898      }
2899
2900      @Override
2901      public <T> T[] toArray(T[] array) {
2902        return Lists.newArrayList(iterator()).toArray(array);
2903      }
2904    }
2905  }
2906
2907  /**
2908   * Support {@code clear()}, {@code removeAll()}, and {@code retainAll()} when
2909   * filtering a filtered sorted map.
2910   */
2911  private static <K, V> SortedMap<K, V> filterFiltered(
2912      FilteredEntrySortedMap<K, V> map, Predicate<? super Entry<K, V>> entryPredicate) {
2913    Predicate<Entry<K, V>> predicate = Predicates.<Entry<K, V>>and(map.predicate, entryPredicate);
2914    return new FilteredEntrySortedMap<K, V>(map.sortedMap(), predicate);
2915  }
2916
2917  private static class FilteredEntrySortedMap<K, V> extends FilteredEntryMap<K, V>
2918      implements SortedMap<K, V> {
2919
2920    FilteredEntrySortedMap(
2921        SortedMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2922      super(unfiltered, entryPredicate);
2923    }
2924
2925    SortedMap<K, V> sortedMap() {
2926      return (SortedMap<K, V>) unfiltered;
2927    }
2928
2929    @Override
2930    public SortedSet<K> keySet() {
2931      return (SortedSet<K>) super.keySet();
2932    }
2933
2934    @Override
2935    SortedSet<K> createKeySet() {
2936      return new SortedKeySet();
2937    }
2938
2939    @WeakOuter
2940    class SortedKeySet extends KeySet implements SortedSet<K> {
2941      @Override
2942      public Comparator<? super K> comparator() {
2943        return sortedMap().comparator();
2944      }
2945
2946      @Override
2947      public SortedSet<K> subSet(K fromElement, K toElement) {
2948        return (SortedSet<K>) subMap(fromElement, toElement).keySet();
2949      }
2950
2951      @Override
2952      public SortedSet<K> headSet(K toElement) {
2953        return (SortedSet<K>) headMap(toElement).keySet();
2954      }
2955
2956      @Override
2957      public SortedSet<K> tailSet(K fromElement) {
2958        return (SortedSet<K>) tailMap(fromElement).keySet();
2959      }
2960
2961      @Override
2962      public K first() {
2963        return firstKey();
2964      }
2965
2966      @Override
2967      public K last() {
2968        return lastKey();
2969      }
2970    }
2971
2972    @Override
2973    public Comparator<? super K> comparator() {
2974      return sortedMap().comparator();
2975    }
2976
2977    @Override
2978    public K firstKey() {
2979      // correctly throws NoSuchElementException when filtered map is empty.
2980      return keySet().iterator().next();
2981    }
2982
2983    @Override
2984    public K lastKey() {
2985      SortedMap<K, V> headMap = sortedMap();
2986      while (true) {
2987        // correctly throws NoSuchElementException when filtered map is empty.
2988        K key = headMap.lastKey();
2989        if (apply(key, unfiltered.get(key))) {
2990          return key;
2991        }
2992        headMap = sortedMap().headMap(key);
2993      }
2994    }
2995
2996    @Override
2997    public SortedMap<K, V> headMap(K toKey) {
2998      return new FilteredEntrySortedMap<K, V>(sortedMap().headMap(toKey), predicate);
2999    }
3000
3001    @Override
3002    public SortedMap<K, V> subMap(K fromKey, K toKey) {
3003      return new FilteredEntrySortedMap<K, V>(sortedMap().subMap(fromKey, toKey), predicate);
3004    }
3005
3006    @Override
3007    public SortedMap<K, V> tailMap(K fromKey) {
3008      return new FilteredEntrySortedMap<K, V>(sortedMap().tailMap(fromKey), predicate);
3009    }
3010  }
3011
3012  /**
3013   * Support {@code clear()}, {@code removeAll()}, and {@code retainAll()} when
3014   * filtering a filtered navigable map.
3015   */
3016  @GwtIncompatible // NavigableMap
3017  private static <K, V> NavigableMap<K, V> filterFiltered(
3018      FilteredEntryNavigableMap<K, V> map, Predicate<? super Entry<K, V>> entryPredicate) {
3019    Predicate<Entry<K, V>> predicate =
3020        Predicates.<Entry<K, V>>and(map.entryPredicate, entryPredicate);
3021    return new FilteredEntryNavigableMap<K, V>(map.unfiltered, predicate);
3022  }
3023
3024  @GwtIncompatible // NavigableMap
3025  private static class FilteredEntryNavigableMap<K, V> extends AbstractNavigableMap<K, V> {
3026    /*
3027     * It's less code to extend AbstractNavigableMap and forward the filtering logic to
3028     * FilteredEntryMap than to extend FilteredEntrySortedMap and reimplement all the NavigableMap
3029     * methods.
3030     */
3031
3032    private final NavigableMap<K, V> unfiltered;
3033    private final Predicate<? super Entry<K, V>> entryPredicate;
3034    private final Map<K, V> filteredDelegate;
3035
3036    FilteredEntryNavigableMap(
3037        NavigableMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
3038      this.unfiltered = checkNotNull(unfiltered);
3039      this.entryPredicate = entryPredicate;
3040      this.filteredDelegate = new FilteredEntryMap<K, V>(unfiltered, entryPredicate);
3041    }
3042
3043    @Override
3044    public Comparator<? super K> comparator() {
3045      return unfiltered.comparator();
3046    }
3047
3048    @Override
3049    public NavigableSet<K> navigableKeySet() {
3050      return new Maps.NavigableKeySet<K, V>(this) {
3051        @Override
3052        public boolean removeAll(Collection<?> collection) {
3053          return FilteredEntryMap.removeAllKeys(unfiltered, entryPredicate, collection);
3054        }
3055
3056        @Override
3057        public boolean retainAll(Collection<?> collection) {
3058          return FilteredEntryMap.retainAllKeys(unfiltered, entryPredicate, collection);
3059        }
3060      };
3061    }
3062
3063    @Override
3064    public Collection<V> values() {
3065      return new FilteredMapValues<K, V>(this, unfiltered, entryPredicate);
3066    }
3067
3068    @Override
3069    Iterator<Entry<K, V>> entryIterator() {
3070      return Iterators.filter(unfiltered.entrySet().iterator(), entryPredicate);
3071    }
3072
3073    @Override
3074    Iterator<Entry<K, V>> descendingEntryIterator() {
3075      return Iterators.filter(unfiltered.descendingMap().entrySet().iterator(), entryPredicate);
3076    }
3077
3078    @Override
3079    public int size() {
3080      return filteredDelegate.size();
3081    }
3082
3083    @Override
3084    public boolean isEmpty() {
3085      return !Iterables.any(unfiltered.entrySet(), entryPredicate);
3086    }
3087
3088    @Override
3089    @Nullable
3090    public V get(@Nullable Object key) {
3091      return filteredDelegate.get(key);
3092    }
3093
3094    @Override
3095    public boolean containsKey(@Nullable Object key) {
3096      return filteredDelegate.containsKey(key);
3097    }
3098
3099    @Override
3100    public V put(K key, V value) {
3101      return filteredDelegate.put(key, value);
3102    }
3103
3104    @Override
3105    public V remove(@Nullable Object key) {
3106      return filteredDelegate.remove(key);
3107    }
3108
3109    @Override
3110    public void putAll(Map<? extends K, ? extends V> m) {
3111      filteredDelegate.putAll(m);
3112    }
3113
3114    @Override
3115    public void clear() {
3116      filteredDelegate.clear();
3117    }
3118
3119    @Override
3120    public Set<Entry<K, V>> entrySet() {
3121      return filteredDelegate.entrySet();
3122    }
3123
3124    @Override
3125    public Entry<K, V> pollFirstEntry() {
3126      return Iterables.removeFirstMatching(unfiltered.entrySet(), entryPredicate);
3127    }
3128
3129    @Override
3130    public Entry<K, V> pollLastEntry() {
3131      return Iterables.removeFirstMatching(unfiltered.descendingMap().entrySet(), entryPredicate);
3132    }
3133
3134    @Override
3135    public NavigableMap<K, V> descendingMap() {
3136      return filterEntries(unfiltered.descendingMap(), entryPredicate);
3137    }
3138
3139    @Override
3140    public NavigableMap<K, V> subMap(
3141        K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
3142      return filterEntries(
3143          unfiltered.subMap(fromKey, fromInclusive, toKey, toInclusive), entryPredicate);
3144    }
3145
3146    @Override
3147    public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
3148      return filterEntries(unfiltered.headMap(toKey, inclusive), entryPredicate);
3149    }
3150
3151    @Override
3152    public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
3153      return filterEntries(unfiltered.tailMap(fromKey, inclusive), entryPredicate);
3154    }
3155  }
3156
3157  /**
3158   * Support {@code clear()}, {@code removeAll()}, and {@code retainAll()} when
3159   * filtering a filtered map.
3160   */
3161  private static <K, V> BiMap<K, V> filterFiltered(
3162      FilteredEntryBiMap<K, V> map, Predicate<? super Entry<K, V>> entryPredicate) {
3163    Predicate<Entry<K, V>> predicate = Predicates.<Entry<K, V>>and(map.predicate, entryPredicate);
3164    return new FilteredEntryBiMap<K, V>(map.unfiltered(), predicate);
3165  }
3166
3167  static final class FilteredEntryBiMap<K, V> extends FilteredEntryMap<K, V>
3168      implements BiMap<K, V> {
3169    @RetainedWith
3170    private final BiMap<V, K> inverse;
3171
3172    private static <K, V> Predicate<Entry<V, K>> inversePredicate(
3173        final Predicate<? super Entry<K, V>> forwardPredicate) {
3174      return new Predicate<Entry<V, K>>() {
3175        @Override
3176        public boolean apply(Entry<V, K> input) {
3177          return forwardPredicate.apply(Maps.immutableEntry(input.getValue(), input.getKey()));
3178        }
3179      };
3180    }
3181
3182    FilteredEntryBiMap(BiMap<K, V> delegate, Predicate<? super Entry<K, V>> predicate) {
3183      super(delegate, predicate);
3184      this.inverse =
3185          new FilteredEntryBiMap<V, K>(delegate.inverse(), inversePredicate(predicate), this);
3186    }
3187
3188    private FilteredEntryBiMap(
3189        BiMap<K, V> delegate, Predicate<? super Entry<K, V>> predicate, BiMap<V, K> inverse) {
3190      super(delegate, predicate);
3191      this.inverse = inverse;
3192    }
3193
3194    BiMap<K, V> unfiltered() {
3195      return (BiMap<K, V>) unfiltered;
3196    }
3197
3198    @Override
3199    public V forcePut(@Nullable K key, @Nullable V value) {
3200      checkArgument(apply(key, value));
3201      return unfiltered().forcePut(key, value);
3202    }
3203
3204    @Override
3205    public BiMap<V, K> inverse() {
3206      return inverse;
3207    }
3208
3209    @Override
3210    public Set<V> values() {
3211      return inverse.keySet();
3212    }
3213  }
3214
3215  /**
3216   * Returns an unmodifiable view of the specified navigable map. Query operations on the returned
3217   * map read through to the specified map, and attempts to modify the returned map, whether direct
3218   * or via its views, result in an {@code UnsupportedOperationException}.
3219   *
3220   * <p>The returned navigable map will be serializable if the specified navigable map is
3221   * serializable.
3222   *
3223   * <p>This method's signature will not permit you to convert a {@code NavigableMap<? extends K,
3224   * V>} to a {@code NavigableMap<K, V>}. If it permitted this, the returned map's {@code
3225   * comparator()} method might return a {@code Comparator<? extends K>}, which works only on a
3226   * particular subtype of {@code K}, but promise that it's a {@code Comparator<? super K>}, which
3227   * must work on any type of {@code K}.
3228   *
3229   * @param map the navigable map for which an unmodifiable view is to be returned
3230   * @return an unmodifiable view of the specified navigable map
3231   * @since 12.0
3232   */
3233  @GwtIncompatible // NavigableMap
3234  public static <K, V> NavigableMap<K, V> unmodifiableNavigableMap(
3235      NavigableMap<K, ? extends V> map) {
3236    checkNotNull(map);
3237    if (map instanceof UnmodifiableNavigableMap) {
3238      @SuppressWarnings("unchecked") // covariant
3239      NavigableMap<K, V> result = (NavigableMap) map;
3240      return result;
3241    } else {
3242      return new UnmodifiableNavigableMap<K, V>(map);
3243    }
3244  }
3245
3246  @Nullable
3247  private static <K, V> Entry<K, V> unmodifiableOrNull(@Nullable Entry<K, ? extends V> entry) {
3248    return (entry == null) ? null : Maps.unmodifiableEntry(entry);
3249  }
3250
3251  @GwtIncompatible // NavigableMap
3252  static class UnmodifiableNavigableMap<K, V> extends ForwardingSortedMap<K, V>
3253      implements NavigableMap<K, V>, Serializable {
3254    private final NavigableMap<K, ? extends V> delegate;
3255
3256    UnmodifiableNavigableMap(NavigableMap<K, ? extends V> delegate) {
3257      this.delegate = delegate;
3258    }
3259
3260    UnmodifiableNavigableMap(
3261        NavigableMap<K, ? extends V> delegate, UnmodifiableNavigableMap<K, V> descendingMap) {
3262      this.delegate = delegate;
3263      this.descendingMap = descendingMap;
3264    }
3265
3266    @Override
3267    protected SortedMap<K, V> delegate() {
3268      return Collections.unmodifiableSortedMap(delegate);
3269    }
3270
3271    @Override
3272    public Entry<K, V> lowerEntry(K key) {
3273      return unmodifiableOrNull(delegate.lowerEntry(key));
3274    }
3275
3276    @Override
3277    public K lowerKey(K key) {
3278      return delegate.lowerKey(key);
3279    }
3280
3281    @Override
3282    public Entry<K, V> floorEntry(K key) {
3283      return unmodifiableOrNull(delegate.floorEntry(key));
3284    }
3285
3286    @Override
3287    public K floorKey(K key) {
3288      return delegate.floorKey(key);
3289    }
3290
3291    @Override
3292    public Entry<K, V> ceilingEntry(K key) {
3293      return unmodifiableOrNull(delegate.ceilingEntry(key));
3294    }
3295
3296    @Override
3297    public K ceilingKey(K key) {
3298      return delegate.ceilingKey(key);
3299    }
3300
3301    @Override
3302    public Entry<K, V> higherEntry(K key) {
3303      return unmodifiableOrNull(delegate.higherEntry(key));
3304    }
3305
3306    @Override
3307    public K higherKey(K key) {
3308      return delegate.higherKey(key);
3309    }
3310
3311    @Override
3312    public Entry<K, V> firstEntry() {
3313      return unmodifiableOrNull(delegate.firstEntry());
3314    }
3315
3316    @Override
3317    public Entry<K, V> lastEntry() {
3318      return unmodifiableOrNull(delegate.lastEntry());
3319    }
3320
3321    @Override
3322    public final Entry<K, V> pollFirstEntry() {
3323      throw new UnsupportedOperationException();
3324    }
3325
3326    @Override
3327    public final Entry<K, V> pollLastEntry() {
3328      throw new UnsupportedOperationException();
3329    }
3330
3331    private transient UnmodifiableNavigableMap<K, V> descendingMap;
3332
3333    @Override
3334    public NavigableMap<K, V> descendingMap() {
3335      UnmodifiableNavigableMap<K, V> result = descendingMap;
3336      return (result == null)
3337          ? descendingMap = new UnmodifiableNavigableMap<K, V>(delegate.descendingMap(), this)
3338          : result;
3339    }
3340
3341    @Override
3342    public Set<K> keySet() {
3343      return navigableKeySet();
3344    }
3345
3346    @Override
3347    public NavigableSet<K> navigableKeySet() {
3348      return Sets.unmodifiableNavigableSet(delegate.navigableKeySet());
3349    }
3350
3351    @Override
3352    public NavigableSet<K> descendingKeySet() {
3353      return Sets.unmodifiableNavigableSet(delegate.descendingKeySet());
3354    }
3355
3356    @Override
3357    public SortedMap<K, V> subMap(K fromKey, K toKey) {
3358      return subMap(fromKey, true, toKey, false);
3359    }
3360
3361    @Override
3362    public SortedMap<K, V> headMap(K toKey) {
3363      return headMap(toKey, false);
3364    }
3365
3366    @Override
3367    public SortedMap<K, V> tailMap(K fromKey) {
3368      return tailMap(fromKey, true);
3369    }
3370
3371    @Override
3372    public NavigableMap<K, V> subMap(
3373        K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
3374      return Maps.unmodifiableNavigableMap(
3375          delegate.subMap(fromKey, fromInclusive, toKey, toInclusive));
3376    }
3377
3378    @Override
3379    public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
3380      return Maps.unmodifiableNavigableMap(delegate.headMap(toKey, inclusive));
3381    }
3382
3383    @Override
3384    public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
3385      return Maps.unmodifiableNavigableMap(delegate.tailMap(fromKey, inclusive));
3386    }
3387  }
3388
3389  /**
3390   * Returns a synchronized (thread-safe) navigable map backed by the specified
3391   * navigable map.  In order to guarantee serial access, it is critical that
3392   * <b>all</b> access to the backing navigable map is accomplished
3393   * through the returned navigable map (or its views).
3394   *
3395   * <p>It is imperative that the user manually synchronize on the returned
3396   * navigable map when iterating over any of its collection views, or the
3397   * collections views of any of its {@code descendingMap}, {@code subMap},
3398   * {@code headMap} or {@code tailMap} views. <pre>   {@code
3399   *
3400   *   NavigableMap<K, V> map = synchronizedNavigableMap(new TreeMap<K, V>());
3401   *
3402   *   // Needn't be in synchronized block
3403   *   NavigableSet<K> set = map.navigableKeySet();
3404   *
3405   *   synchronized (map) { // Synchronizing on map, not set!
3406   *     Iterator<K> it = set.iterator(); // Must be in synchronized block
3407   *     while (it.hasNext()) {
3408   *       foo(it.next());
3409   *     }
3410   *   }}</pre>
3411   *
3412   * <p>or: <pre>   {@code
3413   *
3414   *   NavigableMap<K, V> map = synchronizedNavigableMap(new TreeMap<K, V>());
3415   *   NavigableMap<K, V> map2 = map.subMap(foo, false, bar, true);
3416   *
3417   *   // Needn't be in synchronized block
3418   *   NavigableSet<K> set2 = map2.descendingKeySet();
3419   *
3420   *   synchronized (map) { // Synchronizing on map, not map2 or set2!
3421   *     Iterator<K> it = set2.iterator(); // Must be in synchronized block
3422   *     while (it.hasNext()) {
3423   *       foo(it.next());
3424   *     }
3425   *   }}</pre>
3426   *
3427   * <p>Failure to follow this advice may result in non-deterministic behavior.
3428   *
3429   * <p>The returned navigable map will be serializable if the specified
3430   * navigable map is serializable.
3431   *
3432   * @param navigableMap the navigable map to be "wrapped" in a synchronized
3433   *    navigable map.
3434   * @return a synchronized view of the specified navigable map.
3435   * @since 13.0
3436   */
3437  @GwtIncompatible // NavigableMap
3438  public static <K, V> NavigableMap<K, V> synchronizedNavigableMap(
3439      NavigableMap<K, V> navigableMap) {
3440    return Synchronized.navigableMap(navigableMap);
3441  }
3442
3443  /**
3444   * {@code AbstractMap} extension that makes it easy to cache customized keySet, values,
3445   * and entrySet views.
3446   */
3447  @GwtCompatible
3448  abstract static class ViewCachingAbstractMap<K, V> extends AbstractMap<K, V> {
3449    /**
3450     * Creates the entry set to be returned by {@link #entrySet()}. This method
3451     * is invoked at most once on a given map, at the time when {@code entrySet}
3452     * is first called.
3453     */
3454    abstract Set<Entry<K, V>> createEntrySet();
3455
3456    private transient Set<Entry<K, V>> entrySet;
3457
3458    @Override
3459    public Set<Entry<K, V>> entrySet() {
3460      Set<Entry<K, V>> result = entrySet;
3461      return (result == null) ? entrySet = createEntrySet() : result;
3462    }
3463
3464    private transient Set<K> keySet;
3465
3466    @Override
3467    public Set<K> keySet() {
3468      Set<K> result = keySet;
3469      return (result == null) ? keySet = createKeySet() : result;
3470    }
3471
3472    Set<K> createKeySet() {
3473      return new KeySet<K, V>(this);
3474    }
3475
3476    private transient Collection<V> values;
3477
3478    @Override
3479    public Collection<V> values() {
3480      Collection<V> result = values;
3481      return (result == null) ? values = createValues() : result;
3482    }
3483
3484    Collection<V> createValues() {
3485      return new Values<K, V>(this);
3486    }
3487  }
3488
3489  abstract static class IteratorBasedAbstractMap<K, V> extends AbstractMap<K, V> {
3490    @Override
3491    public abstract int size();
3492
3493    abstract Iterator<Entry<K, V>> entryIterator();
3494
3495    @Override
3496    public Set<Entry<K, V>> entrySet() {
3497      return new EntrySet<K, V>() {
3498        @Override
3499        Map<K, V> map() {
3500          return IteratorBasedAbstractMap.this;
3501        }
3502
3503        @Override
3504        public Iterator<Entry<K, V>> iterator() {
3505          return entryIterator();
3506        }
3507      };
3508    }
3509
3510    @Override
3511    public void clear() {
3512      Iterators.clear(entryIterator());
3513    }
3514  }
3515
3516  /**
3517   * Delegates to {@link Map#get}. Returns {@code null} on {@code
3518   * ClassCastException} and {@code NullPointerException}.
3519   */
3520  static <V> V safeGet(Map<?, V> map, @Nullable Object key) {
3521    checkNotNull(map);
3522    try {
3523      return map.get(key);
3524    } catch (ClassCastException e) {
3525      return null;
3526    } catch (NullPointerException e) {
3527      return null;
3528    }
3529  }
3530
3531  /**
3532   * Delegates to {@link Map#containsKey}. Returns {@code false} on {@code
3533   * ClassCastException} and {@code NullPointerException}.
3534   */
3535  static boolean safeContainsKey(Map<?, ?> map, Object key) {
3536    checkNotNull(map);
3537    try {
3538      return map.containsKey(key);
3539    } catch (ClassCastException e) {
3540      return false;
3541    } catch (NullPointerException e) {
3542      return false;
3543    }
3544  }
3545
3546  /**
3547   * Delegates to {@link Map#remove}. Returns {@code null} on {@code
3548   * ClassCastException} and {@code NullPointerException}.
3549   */
3550  static <V> V safeRemove(Map<?, V> map, Object key) {
3551    checkNotNull(map);
3552    try {
3553      return map.remove(key);
3554    } catch (ClassCastException e) {
3555      return null;
3556    } catch (NullPointerException e) {
3557      return null;
3558    }
3559  }
3560
3561  /**
3562   * An admittedly inefficient implementation of {@link Map#containsKey}.
3563   */
3564  static boolean containsKeyImpl(Map<?, ?> map, @Nullable Object key) {
3565    return Iterators.contains(keyIterator(map.entrySet().iterator()), key);
3566  }
3567
3568  /**
3569   * An implementation of {@link Map#containsValue}.
3570   */
3571  static boolean containsValueImpl(Map<?, ?> map, @Nullable Object value) {
3572    return Iterators.contains(valueIterator(map.entrySet().iterator()), value);
3573  }
3574
3575  /**
3576   * Implements {@code Collection.contains} safely for forwarding collections of
3577   * map entries. If {@code o} is an instance of {@code Map.Entry}, it is
3578   * wrapped using {@link #unmodifiableEntry} to protect against a possible
3579   * nefarious equals method.
3580   *
3581   * <p>Note that {@code c} is the backing (delegate) collection, rather than
3582   * the forwarding collection.
3583   *
3584   * @param c the delegate (unwrapped) collection of map entries
3585   * @param o the object that might be contained in {@code c}
3586   * @return {@code true} if {@code c} contains {@code o}
3587   */
3588  static <K, V> boolean containsEntryImpl(Collection<Entry<K, V>> c, Object o) {
3589    if (!(o instanceof Entry)) {
3590      return false;
3591    }
3592    return c.contains(unmodifiableEntry((Entry<?, ?>) o));
3593  }
3594
3595  /**
3596   * Implements {@code Collection.remove} safely for forwarding collections of
3597   * map entries. If {@code o} is an instance of {@code Map.Entry}, it is
3598   * wrapped using {@link #unmodifiableEntry} to protect against a possible
3599   * nefarious equals method.
3600   *
3601   * <p>Note that {@code c} is backing (delegate) collection, rather than the
3602   * forwarding collection.
3603   *
3604   * @param c the delegate (unwrapped) collection of map entries
3605   * @param o the object to remove from {@code c}
3606   * @return {@code true} if {@code c} was changed
3607   */
3608  static <K, V> boolean removeEntryImpl(Collection<Entry<K, V>> c, Object o) {
3609    if (!(o instanceof Entry)) {
3610      return false;
3611    }
3612    return c.remove(unmodifiableEntry((Entry<?, ?>) o));
3613  }
3614
3615  /**
3616   * An implementation of {@link Map#equals}.
3617   */
3618  static boolean equalsImpl(Map<?, ?> map, Object object) {
3619    if (map == object) {
3620      return true;
3621    } else if (object instanceof Map) {
3622      Map<?, ?> o = (Map<?, ?>) object;
3623      return map.entrySet().equals(o.entrySet());
3624    }
3625    return false;
3626  }
3627
3628  /**
3629   * An implementation of {@link Map#toString}.
3630   */
3631  static String toStringImpl(Map<?, ?> map) {
3632    StringBuilder sb = Collections2.newStringBuilderForCollection(map.size()).append('{');
3633    boolean first = true;
3634    for (Map.Entry<?, ?> entry : map.entrySet()) {
3635      if (!first) {
3636        sb.append(", ");
3637      }
3638      first = false;
3639      sb.append(entry.getKey()).append('=').append(entry.getValue());
3640    }
3641    return sb.append('}').toString();
3642  }
3643
3644  /**
3645   * An implementation of {@link Map#putAll}.
3646   */
3647  static <K, V> void putAllImpl(Map<K, V> self, Map<? extends K, ? extends V> map) {
3648    for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
3649      self.put(entry.getKey(), entry.getValue());
3650    }
3651  }
3652
3653  static class KeySet<K, V> extends Sets.ImprovedAbstractSet<K> {
3654    @Weak final Map<K, V> map;
3655
3656    KeySet(Map<K, V> map) {
3657      this.map = checkNotNull(map);
3658    }
3659
3660    Map<K, V> map() {
3661      return map;
3662    }
3663
3664    @Override
3665    public Iterator<K> iterator() {
3666      return keyIterator(map().entrySet().iterator());
3667    }
3668
3669    @Override
3670    public int size() {
3671      return map().size();
3672    }
3673
3674    @Override
3675    public boolean isEmpty() {
3676      return map().isEmpty();
3677    }
3678
3679    @Override
3680    public boolean contains(Object o) {
3681      return map().containsKey(o);
3682    }
3683
3684    @Override
3685    public boolean remove(Object o) {
3686      if (contains(o)) {
3687        map().remove(o);
3688        return true;
3689      }
3690      return false;
3691    }
3692
3693    @Override
3694    public void clear() {
3695      map().clear();
3696    }
3697  }
3698
3699  @Nullable
3700  static <K> K keyOrNull(@Nullable Entry<K, ?> entry) {
3701    return (entry == null) ? null : entry.getKey();
3702  }
3703
3704  @Nullable
3705  static <V> V valueOrNull(@Nullable Entry<?, V> entry) {
3706    return (entry == null) ? null : entry.getValue();
3707  }
3708
3709  static class SortedKeySet<K, V> extends KeySet<K, V> implements SortedSet<K> {
3710    SortedKeySet(SortedMap<K, V> map) {
3711      super(map);
3712    }
3713
3714    @Override
3715    SortedMap<K, V> map() {
3716      return (SortedMap<K, V>) super.map();
3717    }
3718
3719    @Override
3720    public Comparator<? super K> comparator() {
3721      return map().comparator();
3722    }
3723
3724    @Override
3725    public SortedSet<K> subSet(K fromElement, K toElement) {
3726      return new SortedKeySet<K, V>(map().subMap(fromElement, toElement));
3727    }
3728
3729    @Override
3730    public SortedSet<K> headSet(K toElement) {
3731      return new SortedKeySet<K, V>(map().headMap(toElement));
3732    }
3733
3734    @Override
3735    public SortedSet<K> tailSet(K fromElement) {
3736      return new SortedKeySet<K, V>(map().tailMap(fromElement));
3737    }
3738
3739    @Override
3740    public K first() {
3741      return map().firstKey();
3742    }
3743
3744    @Override
3745    public K last() {
3746      return map().lastKey();
3747    }
3748  }
3749
3750  @GwtIncompatible // NavigableMap
3751  static class NavigableKeySet<K, V> extends SortedKeySet<K, V> implements NavigableSet<K> {
3752    NavigableKeySet(NavigableMap<K, V> map) {
3753      super(map);
3754    }
3755
3756    @Override
3757    NavigableMap<K, V> map() {
3758      return (NavigableMap<K, V>) map;
3759    }
3760
3761    @Override
3762    public K lower(K e) {
3763      return map().lowerKey(e);
3764    }
3765
3766    @Override
3767    public K floor(K e) {
3768      return map().floorKey(e);
3769    }
3770
3771    @Override
3772    public K ceiling(K e) {
3773      return map().ceilingKey(e);
3774    }
3775
3776    @Override
3777    public K higher(K e) {
3778      return map().higherKey(e);
3779    }
3780
3781    @Override
3782    public K pollFirst() {
3783      return keyOrNull(map().pollFirstEntry());
3784    }
3785
3786    @Override
3787    public K pollLast() {
3788      return keyOrNull(map().pollLastEntry());
3789    }
3790
3791    @Override
3792    public NavigableSet<K> descendingSet() {
3793      return map().descendingKeySet();
3794    }
3795
3796    @Override
3797    public Iterator<K> descendingIterator() {
3798      return descendingSet().iterator();
3799    }
3800
3801    @Override
3802    public NavigableSet<K> subSet(
3803        K fromElement, boolean fromInclusive, K toElement, boolean toInclusive) {
3804      return map().subMap(fromElement, fromInclusive, toElement, toInclusive).navigableKeySet();
3805    }
3806
3807    @Override
3808    public NavigableSet<K> headSet(K toElement, boolean inclusive) {
3809      return map().headMap(toElement, inclusive).navigableKeySet();
3810    }
3811
3812    @Override
3813    public NavigableSet<K> tailSet(K fromElement, boolean inclusive) {
3814      return map().tailMap(fromElement, inclusive).navigableKeySet();
3815    }
3816
3817    @Override
3818    public SortedSet<K> subSet(K fromElement, K toElement) {
3819      return subSet(fromElement, true, toElement, false);
3820    }
3821
3822    @Override
3823    public SortedSet<K> headSet(K toElement) {
3824      return headSet(toElement, false);
3825    }
3826
3827    @Override
3828    public SortedSet<K> tailSet(K fromElement) {
3829      return tailSet(fromElement, true);
3830    }
3831  }
3832
3833  static class Values<K, V> extends AbstractCollection<V> {
3834    @Weak final Map<K, V> map;
3835
3836    Values(Map<K, V> map) {
3837      this.map = checkNotNull(map);
3838    }
3839
3840    final Map<K, V> map() {
3841      return map;
3842    }
3843
3844    @Override
3845    public Iterator<V> iterator() {
3846      return valueIterator(map().entrySet().iterator());
3847    }
3848
3849    @Override
3850    public boolean remove(Object o) {
3851      try {
3852        return super.remove(o);
3853      } catch (UnsupportedOperationException e) {
3854        for (Entry<K, V> entry : map().entrySet()) {
3855          if (Objects.equal(o, entry.getValue())) {
3856            map().remove(entry.getKey());
3857            return true;
3858          }
3859        }
3860        return false;
3861      }
3862    }
3863
3864    @Override
3865    public boolean removeAll(Collection<?> c) {
3866      try {
3867        return super.removeAll(checkNotNull(c));
3868      } catch (UnsupportedOperationException e) {
3869        Set<K> toRemove = Sets.newHashSet();
3870        for (Entry<K, V> entry : map().entrySet()) {
3871          if (c.contains(entry.getValue())) {
3872            toRemove.add(entry.getKey());
3873          }
3874        }
3875        return map().keySet().removeAll(toRemove);
3876      }
3877    }
3878
3879    @Override
3880    public boolean retainAll(Collection<?> c) {
3881      try {
3882        return super.retainAll(checkNotNull(c));
3883      } catch (UnsupportedOperationException e) {
3884        Set<K> toRetain = Sets.newHashSet();
3885        for (Entry<K, V> entry : map().entrySet()) {
3886          if (c.contains(entry.getValue())) {
3887            toRetain.add(entry.getKey());
3888          }
3889        }
3890        return map().keySet().retainAll(toRetain);
3891      }
3892    }
3893
3894    @Override
3895    public int size() {
3896      return map().size();
3897    }
3898
3899    @Override
3900    public boolean isEmpty() {
3901      return map().isEmpty();
3902    }
3903
3904    @Override
3905    public boolean contains(@Nullable Object o) {
3906      return map().containsValue(o);
3907    }
3908
3909    @Override
3910    public void clear() {
3911      map().clear();
3912    }
3913  }
3914
3915  abstract static class EntrySet<K, V> extends Sets.ImprovedAbstractSet<Entry<K, V>> {
3916    abstract Map<K, V> map();
3917
3918    @Override
3919    public int size() {
3920      return map().size();
3921    }
3922
3923    @Override
3924    public void clear() {
3925      map().clear();
3926    }
3927
3928    @Override
3929    public boolean contains(Object o) {
3930      if (o instanceof Entry) {
3931        Entry<?, ?> entry = (Entry<?, ?>) o;
3932        Object key = entry.getKey();
3933        V value = Maps.safeGet(map(), key);
3934        return Objects.equal(value, entry.getValue()) && (value != null || map().containsKey(key));
3935      }
3936      return false;
3937    }
3938
3939    @Override
3940    public boolean isEmpty() {
3941      return map().isEmpty();
3942    }
3943
3944    @Override
3945    public boolean remove(Object o) {
3946      if (contains(o)) {
3947        Entry<?, ?> entry = (Entry<?, ?>) o;
3948        return map().keySet().remove(entry.getKey());
3949      }
3950      return false;
3951    }
3952
3953    @Override
3954    public boolean removeAll(Collection<?> c) {
3955      try {
3956        return super.removeAll(checkNotNull(c));
3957      } catch (UnsupportedOperationException e) {
3958        // if the iterators don't support remove
3959        return Sets.removeAllImpl(this, c.iterator());
3960      }
3961    }
3962
3963    @Override
3964    public boolean retainAll(Collection<?> c) {
3965      try {
3966        return super.retainAll(checkNotNull(c));
3967      } catch (UnsupportedOperationException e) {
3968        // if the iterators don't support remove
3969        Set<Object> keys = Sets.newHashSetWithExpectedSize(c.size());
3970        for (Object o : c) {
3971          if (contains(o)) {
3972            Entry<?, ?> entry = (Entry<?, ?>) o;
3973            keys.add(entry.getKey());
3974          }
3975        }
3976        return map().keySet().retainAll(keys);
3977      }
3978    }
3979  }
3980
3981  @GwtIncompatible // NavigableMap
3982  abstract static class DescendingMap<K, V> extends ForwardingMap<K, V>
3983      implements NavigableMap<K, V> {
3984
3985    abstract NavigableMap<K, V> forward();
3986
3987    @Override
3988    protected final Map<K, V> delegate() {
3989      return forward();
3990    }
3991
3992    private transient Comparator<? super K> comparator;
3993
3994    @SuppressWarnings("unchecked")
3995    @Override
3996    public Comparator<? super K> comparator() {
3997      Comparator<? super K> result = comparator;
3998      if (result == null) {
3999        Comparator<? super K> forwardCmp = forward().comparator();
4000        if (forwardCmp == null) {
4001          forwardCmp = (Comparator) Ordering.natural();
4002        }
4003        result = comparator = reverse(forwardCmp);
4004      }
4005      return result;
4006    }
4007
4008    // If we inline this, we get a javac error.
4009    private static <T> Ordering<T> reverse(Comparator<T> forward) {
4010      return Ordering.from(forward).reverse();
4011    }
4012
4013    @Override
4014    public K firstKey() {
4015      return forward().lastKey();
4016    }
4017
4018    @Override
4019    public K lastKey() {
4020      return forward().firstKey();
4021    }
4022
4023    @Override
4024    public Entry<K, V> lowerEntry(K key) {
4025      return forward().higherEntry(key);
4026    }
4027
4028    @Override
4029    public K lowerKey(K key) {
4030      return forward().higherKey(key);
4031    }
4032
4033    @Override
4034    public Entry<K, V> floorEntry(K key) {
4035      return forward().ceilingEntry(key);
4036    }
4037
4038    @Override
4039    public K floorKey(K key) {
4040      return forward().ceilingKey(key);
4041    }
4042
4043    @Override
4044    public Entry<K, V> ceilingEntry(K key) {
4045      return forward().floorEntry(key);
4046    }
4047
4048    @Override
4049    public K ceilingKey(K key) {
4050      return forward().floorKey(key);
4051    }
4052
4053    @Override
4054    public Entry<K, V> higherEntry(K key) {
4055      return forward().lowerEntry(key);
4056    }
4057
4058    @Override
4059    public K higherKey(K key) {
4060      return forward().lowerKey(key);
4061    }
4062
4063    @Override
4064    public Entry<K, V> firstEntry() {
4065      return forward().lastEntry();
4066    }
4067
4068    @Override
4069    public Entry<K, V> lastEntry() {
4070      return forward().firstEntry();
4071    }
4072
4073    @Override
4074    public Entry<K, V> pollFirstEntry() {
4075      return forward().pollLastEntry();
4076    }
4077
4078    @Override
4079    public Entry<K, V> pollLastEntry() {
4080      return forward().pollFirstEntry();
4081    }
4082
4083    @Override
4084    public NavigableMap<K, V> descendingMap() {
4085      return forward();
4086    }
4087
4088    private transient Set<Entry<K, V>> entrySet;
4089
4090    @Override
4091    public Set<Entry<K, V>> entrySet() {
4092      Set<Entry<K, V>> result = entrySet;
4093      return (result == null) ? entrySet = createEntrySet() : result;
4094    }
4095
4096    abstract Iterator<Entry<K, V>> entryIterator();
4097
4098    Set<Entry<K, V>> createEntrySet() {
4099      @WeakOuter
4100      class EntrySetImpl extends EntrySet<K, V> {
4101        @Override
4102        Map<K, V> map() {
4103          return DescendingMap.this;
4104        }
4105
4106        @Override
4107        public Iterator<Entry<K, V>> iterator() {
4108          return entryIterator();
4109        }
4110      }
4111      return new EntrySetImpl();
4112    }
4113
4114    @Override
4115    public Set<K> keySet() {
4116      return navigableKeySet();
4117    }
4118
4119    private transient NavigableSet<K> navigableKeySet;
4120
4121    @Override
4122    public NavigableSet<K> navigableKeySet() {
4123      NavigableSet<K> result = navigableKeySet;
4124      return (result == null) ? navigableKeySet = new NavigableKeySet<K, V>(this) : result;
4125    }
4126
4127    @Override
4128    public NavigableSet<K> descendingKeySet() {
4129      return forward().navigableKeySet();
4130    }
4131
4132    @Override
4133    public NavigableMap<K, V> subMap(
4134        K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
4135      return forward().subMap(toKey, toInclusive, fromKey, fromInclusive).descendingMap();
4136    }
4137
4138    @Override
4139    public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
4140      return forward().tailMap(toKey, inclusive).descendingMap();
4141    }
4142
4143    @Override
4144    public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
4145      return forward().headMap(fromKey, inclusive).descendingMap();
4146    }
4147
4148    @Override
4149    public SortedMap<K, V> subMap(K fromKey, K toKey) {
4150      return subMap(fromKey, true, toKey, false);
4151    }
4152
4153    @Override
4154    public SortedMap<K, V> headMap(K toKey) {
4155      return headMap(toKey, false);
4156    }
4157
4158    @Override
4159    public SortedMap<K, V> tailMap(K fromKey) {
4160      return tailMap(fromKey, true);
4161    }
4162
4163    @Override
4164    public Collection<V> values() {
4165      return new Values<K, V>(this);
4166    }
4167
4168    @Override
4169    public String toString() {
4170      return standardToString();
4171    }
4172  }
4173
4174  /**
4175   * Returns a map from the ith element of list to i.
4176   */
4177  static <E> ImmutableMap<E, Integer> indexMap(Collection<E> list) {
4178    ImmutableMap.Builder<E, Integer> builder = new ImmutableMap.Builder<E, Integer>(list.size());
4179    int i = 0;
4180    for (E e : list) {
4181      builder.put(e, i++);
4182    }
4183    return builder.build();
4184  }
4185
4186  /**
4187   * Returns a view of the portion of {@code map} whose keys are contained by {@code range}.
4188   *
4189   * <p>This method delegates to the appropriate methods of {@link NavigableMap} (namely
4190   * {@link NavigableMap#subMap(Object, boolean, Object, boolean) subMap()},
4191   * {@link NavigableMap#tailMap(Object, boolean) tailMap()}, and
4192   * {@link NavigableMap#headMap(Object, boolean) headMap()}) to actually construct the view.
4193   * Consult these methods for a full description of the returned view's behavior.
4194   *
4195   * <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
4196   * ordering. {@code NavigableMap} on the other hand can specify a custom ordering via a
4197   * {@link Comparator}, which can violate the natural ordering. Using this method (or in general
4198   * using {@code Range}) with unnaturally-ordered maps can lead to unexpected and undefined
4199   * behavior.
4200   *
4201   * @since 20.0
4202   */
4203  @Beta
4204  @GwtIncompatible // NavigableMap
4205  public static <K extends Comparable<? super K>, V> NavigableMap<K, V> subMap(
4206      NavigableMap<K, V> map, Range<K> range) {
4207    if (map.comparator() != null
4208        && map.comparator() != Ordering.natural()
4209        && range.hasLowerBound()
4210        && range.hasUpperBound()) {
4211      checkArgument(
4212          map.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0,
4213          "map is using a custom comparator which is inconsistent with the natural ordering.");
4214    }
4215    if (range.hasLowerBound() && range.hasUpperBound()) {
4216      return map.subMap(
4217          range.lowerEndpoint(),
4218          range.lowerBoundType() == BoundType.CLOSED,
4219          range.upperEndpoint(),
4220          range.upperBoundType() == BoundType.CLOSED);
4221    } else if (range.hasLowerBound()) {
4222      return map.tailMap(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
4223    } else if (range.hasUpperBound()) {
4224      return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
4225    }
4226    return checkNotNull(map);
4227  }
4228}