|
| 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