diff options
Diffstat (limited to 'guava/src/com/google/common/collect/EnumHashBiMap.java')
-rw-r--r-- | guava/src/com/google/common/collect/EnumHashBiMap.java | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/guava/src/com/google/common/collect/EnumHashBiMap.java b/guava/src/com/google/common/collect/EnumHashBiMap.java index 5fbbb70c6..33b45b95c 100644 --- a/guava/src/com/google/common/collect/EnumHashBiMap.java +++ b/guava/src/com/google/common/collect/EnumHashBiMap.java @@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.io.IOException; import java.io.ObjectInputStream; @@ -42,10 +43,11 @@ import org.checkerframework.checker.nullness.qual.Nullable; * @since 2.0 */ @GwtCompatible(emulated = true) +@J2ktIncompatible @ElementTypesAreNonnullByDefault public final class EnumHashBiMap<K extends Enum<K>, V extends @Nullable Object> extends AbstractBiMap<K, V> { - private transient Class<K> keyType; + transient Class<K> keyTypeOrObjectUnderJ2cl; /** * Returns a new, empty {@code EnumHashBiMap} using the specified key type. @@ -69,16 +71,15 @@ public final class EnumHashBiMap<K extends Enum<K>, V extends @Nullable Object> */ public static <K extends Enum<K>, V extends @Nullable Object> EnumHashBiMap<K, V> create( Map<K, ? extends V> map) { - EnumHashBiMap<K, V> bimap = create(EnumBiMap.inferKeyType(map)); + EnumHashBiMap<K, V> bimap = create(EnumBiMap.inferKeyTypeOrObjectUnderJ2cl(map)); bimap.putAll(map); return bimap; } private EnumHashBiMap(Class<K> keyType) { - super( - new EnumMap<K, V>(keyType), - Maps.<V, K>newHashMapWithExpectedSize(keyType.getEnumConstants().length)); - this.keyType = keyType; + super(new EnumMap<K, V>(keyType), new HashMap<V, K>()); + // TODO: cpovirk - Pre-size the HashMap based on the number of enum values? + this.keyTypeOrObjectUnderJ2cl = keyType; } // Overriding these 3 methods to show that values may be null (but not keys) @@ -107,8 +108,9 @@ public final class EnumHashBiMap<K extends Enum<K>, V extends @Nullable Object> } /** Returns the associated key type. */ + @GwtIncompatible public Class<K> keyType() { - return keyType; + return keyTypeOrObjectUnderJ2cl; } /** @@ -118,7 +120,7 @@ public final class EnumHashBiMap<K extends Enum<K>, V extends @Nullable Object> @GwtIncompatible // java.io.ObjectOutputStream private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); - stream.writeObject(keyType); + stream.writeObject(keyTypeOrObjectUnderJ2cl); Serialization.writeMap(this, stream); } @@ -126,9 +128,13 @@ public final class EnumHashBiMap<K extends Enum<K>, V extends @Nullable Object> @GwtIncompatible // java.io.ObjectInputStream private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); - keyType = (Class<K>) stream.readObject(); - setDelegates( - new EnumMap<K, V>(keyType), new HashMap<V, K>(keyType.getEnumConstants().length * 3 / 2)); + keyTypeOrObjectUnderJ2cl = (Class<K>) stream.readObject(); + /* + * TODO: cpovirk - Pre-size the HashMap based on the number of enum values? (But *not* based on + * the number of entries in the map, as that makes it easy for hostile inputs to trigger lots of + * allocation—not that any program should be deserializing hostile inputs to begin with!) + */ + setDelegates(new EnumMap<K, V>(keyTypeOrObjectUnderJ2cl), new HashMap<V, K>()); Serialization.populateMap(this, stream); } |