Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…api/java.base/java/util/EnumMap.html#%3Cinit%3E(java.util.Map)) throw if passed an empty `Map` (other than an `EnumMap`).

My motivation is that I'm migrating Guava's tests to run under J2CL, and we have [a test](https://github.com/google/guava/blob/b567b26e2bd6ea679b8cf69711cc8a41ecf219c4/guava-tests/test/com/google/common/collect/MapsTest.java#L359-L366) that expects an `IllegalArgumentException` in this case. Now, that's really more a test of the implementation of `EnumMap` than it is of a test of Guava, so I could see deleting the test or at least not running it under J2CL. But I figured I'd at least run this CL by you first to see if you thought the behavior was worth considering changing. (Not that I love the JDK's design of this API, and not that J2CL actually needs a non-empty map.)

PiperOrigin-RevId: 570769285
  • Loading branch information
cpovirk authored and copybara-github committed Oct 4, 2023
1 parent e648613 commit d7dd370
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jre/java/java/util/EnumMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkArgument;

/**
* A {@link java.util.Map} of {@link Enum}s. <a
Expand All @@ -33,6 +34,7 @@ public EnumMap(Class<K> type) {}

public EnumMap(Map<K, ? extends V> m) {
putAll(m);
checkArgument(m instanceof EnumMap || !isEmpty());
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions jre/javatests/com/google/j2cl/jre/java/util/EnumMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ public void testOrdering() {
lastOrdinal = newOrdinal;
}
}

public void testConstructorSucceedsGivenEmptyEnumMap() {
var unused = new EnumMap<>(new EnumMap<Numbers, Integer>(Numbers.class));
}

public void testConstructorThrowsGivenEmptyOtherMap() {
try {
new EnumMap<>(new HashMap<Numbers, Integer>());
fail();
} catch (IllegalArgumentException expected) {
}
}
}

0 comments on commit d7dd370

Please sign in to comment.