Skip to content

Commit

Permalink
Under j2cl, make Maps.immutableEnumMap and toImmutableEnumMap wor…
Browse files Browse the repository at this point in the history
…k. Previously, they were present but failed at runtime.

RELNOTES=`collect`: Under j2cl, made `Maps.immutableEnumMap` and `toImmutableEnumMap` work.
PiperOrigin-RevId: 534145451
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 22, 2023
1 parent 56dc928 commit 852a7d3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
import static java.util.Collections.singletonMap;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -163,9 +164,8 @@ public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
K key1 = entry1.getKey();
V value1 = entry1.getValue();
checkEntryNotNull(key1, value1);
Class<K> clazz = key1.getDeclaringClass();
EnumMap<K, V> enumMap = new EnumMap<>(clazz);
enumMap.put(key1, value1);
// Do something that works for j2cl, where we can't call getDeclaredClass():
EnumMap<K, V> enumMap = new EnumMap<>(singletonMap(key1, value1));
while (entryItr.hasNext()) {
Entry<K, ? extends V> entry = entryItr.next();
K key = entry.getKey();
Expand Down
6 changes: 4 additions & 2 deletions guava/src/com/google/common/collect/CollectCollectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.collectingAndThen;

import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -326,9 +327,10 @@ private static class EnumMapAccumulator<K extends Enum<K>, V> {

void put(K key, V value) {
if (map == null) {
map = new EnumMap<>(key.getDeclaringClass());
map = new EnumMap<>(singletonMap(key, value));
} else {
map.merge(key, value, mergeFunction);
}
map.merge(key, value, mergeFunction);
}

EnumMapAccumulator<K, V> combine(EnumMapAccumulator<K, V> other) {
Expand Down
6 changes: 3 additions & 3 deletions guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
import static java.util.Collections.singletonMap;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -170,9 +171,8 @@ public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
K key1 = entry1.getKey();
V value1 = entry1.getValue();
checkEntryNotNull(key1, value1);
Class<K> clazz = key1.getDeclaringClass();
EnumMap<K, V> enumMap = new EnumMap<>(clazz);
enumMap.put(key1, value1);
// Do something that works for j2cl, where we can't call getDeclaredClass():
EnumMap<K, V> enumMap = new EnumMap<>(singletonMap(key1, value1));
while (entryItr.hasNext()) {
Entry<K, ? extends V> entry = entryItr.next();
K key = entry.getKey();
Expand Down

0 comments on commit 852a7d3

Please sign in to comment.