001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect;
016
017import static com.google.common.base.Preconditions.checkArgument;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.common.annotations.GwtIncompatible;
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectOutputStream;
024import java.util.EnumMap;
025import java.util.Iterator;
026
027/**
028 * Multiset implementation backed by an {@link EnumMap}.
029 *
030 * <p>See the Guava User Guide article on <a href=
031 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset">
032 * {@code Multiset}</a>.
033 *
034 * @author Jared Levy
035 * @since 2.0
036 */
037@GwtCompatible(emulated = true)
038public final class EnumMultiset<E extends Enum<E>> extends AbstractMapBasedMultiset<E> {
039  /** Creates an empty {@code EnumMultiset}. */
040  public static <E extends Enum<E>> EnumMultiset<E> create(Class<E> type) {
041    return new EnumMultiset<E>(type);
042  }
043
044  /**
045   * Creates a new {@code EnumMultiset} containing the specified elements.
046   *
047   * <p>This implementation is highly efficient when {@code elements} is itself a {@link
048   * Multiset}.
049   *
050   * @param elements the elements that the multiset should contain
051   * @throws IllegalArgumentException if {@code elements} is empty
052   */
053  public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements) {
054    Iterator<E> iterator = elements.iterator();
055    checkArgument(iterator.hasNext(), "EnumMultiset constructor passed empty Iterable");
056    EnumMultiset<E> multiset = new EnumMultiset<E>(iterator.next().getDeclaringClass());
057    Iterables.addAll(multiset, elements);
058    return multiset;
059  }
060
061  /**
062   * Returns a new {@code EnumMultiset} instance containing the given elements.  Unlike
063   * {@link EnumMultiset#create(Iterable)}, this method does not produce an exception on an empty
064   * iterable.
065   *
066   * @since 14.0
067   */
068  public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements, Class<E> type) {
069    EnumMultiset<E> result = create(type);
070    Iterables.addAll(result, elements);
071    return result;
072  }
073
074  private transient Class<E> type;
075
076  /** Creates an empty {@code EnumMultiset}. */
077  private EnumMultiset(Class<E> type) {
078    super(WellBehavedMap.wrap(new EnumMap<E, Count>(type)));
079    this.type = type;
080  }
081
082  @GwtIncompatible // java.io.ObjectOutputStream
083  private void writeObject(ObjectOutputStream stream) throws IOException {
084    stream.defaultWriteObject();
085    stream.writeObject(type);
086    Serialization.writeMultiset(this, stream);
087  }
088
089  /**
090   * @serialData the {@code Class<E>} for the enum type, the number of distinct
091   *             elements, the first element, its count, the second element, its
092   *             count, and so on
093   */
094  @GwtIncompatible // java.io.ObjectInputStream
095  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
096    stream.defaultReadObject();
097    @SuppressWarnings("unchecked") // reading data stored by writeObject
098    Class<E> localType = (Class<E>) stream.readObject();
099    type = localType;
100    setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type)));
101    Serialization.populateMultiset(this, stream);
102  }
103
104  @GwtIncompatible // Not needed in emulated source
105  private static final long serialVersionUID = 0;
106}