001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
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.HashMap;
025
026/**
027 * Multiset implementation backed by a {@link HashMap}.
028 *
029 * @author Kevin Bourrillion
030 * @author Jared Levy
031 * @since 2.0
032 */
033@GwtCompatible(serializable = true, emulated = true)
034public final class HashMultiset<E> extends AbstractMapBasedMultiset<E> {
035
036  /**
037   * Creates a new, empty {@code HashMultiset} using the default initial
038   * capacity.
039   */
040  public static <E> HashMultiset<E> create() {
041    return new HashMultiset<E>();
042  }
043
044  /**
045   * Creates a new, empty {@code HashMultiset} with the specified expected
046   * number of distinct elements.
047   *
048   * @param distinctElements the expected number of distinct elements
049   * @throws IllegalArgumentException if {@code distinctElements} is negative
050   */
051  public static <E> HashMultiset<E> create(int distinctElements) {
052    return new HashMultiset<E>(distinctElements);
053  }
054
055  /**
056   * Creates a new {@code HashMultiset} containing the specified elements.
057   *
058   * <p>This implementation is highly efficient when {@code elements} is itself
059   * a {@link Multiset}.
060   *
061   * @param elements the elements that the multiset should contain
062   */
063  public static <E> HashMultiset<E> create(Iterable<? extends E> elements) {
064    HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
065    Iterables.addAll(multiset, elements);
066    return multiset;
067  }
068
069  private HashMultiset() {
070    super(new HashMap<E, Count>());
071  }
072
073  private HashMultiset(int distinctElements) {
074    super(Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
075  }
076
077  /**
078   * @serialData the number of distinct elements, the first element, its count,
079   *     the second element, its count, and so on
080   */
081  @GwtIncompatible // java.io.ObjectOutputStream
082  private void writeObject(ObjectOutputStream stream) throws IOException {
083    stream.defaultWriteObject();
084    Serialization.writeMultiset(this, stream);
085  }
086
087  @GwtIncompatible // java.io.ObjectInputStream
088  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
089    stream.defaultReadObject();
090    int distinctElements = Serialization.readCount(stream);
091    setBackingMap(Maps.<E, Count>newHashMap());
092    Serialization.populateMultiset(this, stream, distinctElements);
093  }
094
095  @GwtIncompatible // Not needed in emulated source.
096  private static final long serialVersionUID = 0;
097}