Returns an immutable map containing the same entries as {@code map}. The returned map iterates over entries in the same order as the {@code entrySet} of the original map. If {@code map} somehow contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose comparator is not <i
(Map<? extends K, ? extends V> map)
| 685 | * @throws NullPointerException if any key or value in {@code map} is null |
| 686 | */ |
| 687 | public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) { |
| 688 | if ((map instanceof ImmutableMap) && !(map instanceof SortedMap)) { |
| 689 | @SuppressWarnings(class="st">"unchecked") class="cm">// safe since map is not writable |
| 690 | ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) map; |
| 691 | if (!kvMap.isPartialView()) { |
| 692 | return kvMap; |
| 693 | } |
| 694 | } else if (map instanceof EnumMap) { |
| 695 | EnumMap<?, ? extends V> clone = ((EnumMap<?, ? extends V>) map).clone(); |
| 696 | for (Entry<?, ?> entry : clone.entrySet()) { |
| 697 | checkEntryNotNull(entry.getKey(), entry.getValue()); |
| 698 | } |
| 699 | ImmutableMap<?, ? extends V> untypedResult = ImmutableEnumMap.asImmutable(clone); |
| 700 | /* |
| 701 | * The result has the same type argument we started with. We just couldn't express EnumMap<K, |
| 702 | * ...> or ImmutableEnumMap<K, ...> along the way because our own <K> isn't <K extends |
| 703 | * Enum<K>>. |
| 704 | * |
| 705 | * We are also performing a covariant cast, potentially from ImmutableMap<K, Sub> to |
| 706 | * ImmutableMap<K, Super>. That is safe because no one can add elements to the map. |
| 707 | */ |
| 708 | @SuppressWarnings(class="st">"unchecked") |
| 709 | ImmutableMap<K, V> result = (ImmutableMap<K, V>) untypedResult; |
| 710 | return result; |
| 711 | } |
| 712 | return copyOf(map.entrySet()); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Returns an immutable map containing the specified entries. The returned map iterates over |
no test coverage detected