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;
021
022import java.io.IOException;
023import java.io.ObjectInputStream;
024import java.io.ObjectOutputStream;
025import java.util.EnumMap;
026import java.util.Iterator;
027
028/**
029 * Multiset implementation backed by an {@link EnumMap}.
030 * 
031 * <p>See the Guava User Guide article on <a href=
032 * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multiset">
033 * {@code Multiset}</a>.
034 *
035 * @author Jared Levy
036 * @since 2.0 (imported from Google Collections Library)
037 */
038@GwtCompatible(emulated = true)
039public final class EnumMultiset<E extends Enum<E>> extends AbstractMapBasedMultiset<E> {
040  /** Creates an empty {@code EnumMultiset}. */
041  public static <E extends Enum<E>> EnumMultiset<E> create(Class<E> type) {
042    return new EnumMultiset<E>(type);
043  }
044
045  /**
046   * Creates a new {@code EnumMultiset} containing the specified elements.
047   *
048   * <p>This implementation is highly efficient when {@code elements} is itself a {@link
049   * Multiset}.
050   *
051   * @param elements the elements that the multiset should contain
052   * @throws IllegalArgumentException if {@code elements} is empty
053   */
054  public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements) {
055    Iterator<E> iterator = elements.iterator();
056    checkArgument(iterator.hasNext(), "EnumMultiset constructor passed empty Iterable");
057    EnumMultiset<E> multiset = new EnumMultiset<E>(iterator.next().getDeclaringClass());
058    Iterables.addAll(multiset, elements);
059    return multiset;
060  }
061  
062  /**
063   * Returns a new {@code EnumMultiset} instance containing the given elements.  Unlike
064   * {@link EnumMultiset#create(Iterable)}, this method does not produce an exception on an empty
065   * iterable.
066   * 
067   * @since 14.0
068   */
069  public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements, Class<E> type) {
070    EnumMultiset<E> result = create(type);
071    Iterables.addAll(result, elements);
072    return result;
073  }
074
075  private transient Class<E> type;
076
077  /** Creates an empty {@code EnumMultiset}. */
078  private EnumMultiset(Class<E> type) {
079    super(WellBehavedMap.wrap(new EnumMap<E, Count>(type)));
080    this.type = type;
081  }
082
083  @GwtIncompatible("java.io.ObjectOutputStream")
084  private void writeObject(ObjectOutputStream stream) throws IOException {
085    stream.defaultWriteObject();
086    stream.writeObject(type);
087    Serialization.writeMultiset(this, stream);
088  }
089
090  /**
091   * @serialData the {@code Class<E>} for the enum type, the number of distinct
092   *             elements, the first element, its count, the second element, its
093   *             count, and so on
094   */
095  @GwtIncompatible("java.io.ObjectInputStream")
096  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
097    stream.defaultReadObject();
098    @SuppressWarnings("unchecked") // reading data stored by writeObject
099    Class<E> localType = (Class<E>) stream.readObject();
100    type = localType;
101    setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type)));
102    Serialization.populateMultiset(this, stream);
103  }
104
105  @GwtIncompatible("Not needed in emulated source")
106  private static final long serialVersionUID = 0;
107}