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