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