Returns an immutable map instance containing the given entries. Internally, the returned map will be backed by an {@link EnumMap}. <p>The iteration order of the returned map follows the enum's iteration order, not the order in which the elements appear in the given map. @param map the map to make
(
Map<K, ? extends V> map)
| 131 | * @since 14.0 |
| 132 | */ |
| 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(class="st">"unchecked") class="cm">// 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="cm">// Do something that works for j2cl, where we can't call getDeclaredClass(): |
| 149 | EnumMap<K, V> enumMap = new EnumMap<>(singletonMap(key1, value1)); |
| 150 | while (entryItr.hasNext()) { |
| 151 | Entry<K, ? extends V> entry = entryItr.next(); |
| 152 | K key = entry.getKey(); |
| 153 | V value = entry.getValue(); |
| 154 | checkEntryNotNull(key, value); |
| 155 | enumMap.put(key, value); |
| 156 | } |
| 157 | return ImmutableEnumMap.asImmutable(enumMap); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys |