Skip to content

Commit 23ff918

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Under J2CL, make EnumMultiset work. Previously, it was present but failed at runtime.
The easiest fix I could find was to change the GWT/J2CL implementation to use `AbstractMapBasedMultiset`. It's possible that that performs worse than the existing custom multiset implementation under GWT, but I don't know. It at least has the virtue of working under J2CL. RELNOTES=`collect`: Under j2cl, made `EnumMultiset` work. Previously, it was present but failed at runtime. PiperOrigin-RevId: 535711434
1 parent 3de12be commit 23ff918

File tree

1 file changed

+54
-0
lines changed
  • guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2007 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.collect;
16+
17+
import static com.google.common.base.Preconditions.checkArgument;
18+
import static com.google.common.collect.Platform.getDeclaringClassOrObjectForJ2cl;
19+
20+
import com.google.common.annotations.GwtCompatible;
21+
import com.google.common.annotations.J2ktIncompatible;
22+
import java.io.Serializable;
23+
import java.util.EnumMap;
24+
import java.util.Iterator;
25+
26+
@GwtCompatible(emulated = true)
27+
@J2ktIncompatible
28+
@ElementTypesAreNonnullByDefault
29+
public final class EnumMultiset<E extends Enum<E>> extends AbstractMapBasedMultiset<E>
30+
implements Serializable {
31+
public static <E extends Enum<E>> EnumMultiset<E> create(Class<E> type) {
32+
return new EnumMultiset<>(new EnumMap<E, Count>(type));
33+
}
34+
35+
public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements) {
36+
Iterator<E> iterator = elements.iterator();
37+
checkArgument(iterator.hasNext(), "EnumMultiset constructor passed empty Iterable");
38+
EnumMap<E, Count> map = new EnumMap<>(getDeclaringClassOrObjectForJ2cl(iterator.next()));
39+
EnumMultiset<E> multiset = new EnumMultiset<>(map);
40+
Iterables.addAll(multiset, elements);
41+
return multiset;
42+
}
43+
44+
public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements, Class<E> type) {
45+
EnumMultiset<E> result = create(type);
46+
Iterables.addAll(result, elements);
47+
return result;
48+
}
49+
50+
/** Creates an empty {@code EnumMultiset}. */
51+
private EnumMultiset(EnumMap<E, Count> map) {
52+
super(map);
53+
}
54+
}

0 commit comments

Comments
 (0)