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;
021
022import java.io.IOException;
023import java.io.ObjectInputStream;
024import java.io.ObjectOutputStream;
025import java.util.HashMap;
026
027/**
028 * Multiset implementation backed by a {@link HashMap}.
029 *
030 * @author Kevin Bourrillion
031 * @author Jared Levy
032 * @since 2.0
033 */
034@GwtCompatible(serializable = true, emulated = true)
035public final class HashMultiset<E> extends AbstractMapBasedMultiset<E> {
036
037  /**
038   * Creates a new, empty {@code HashMultiset} using the default initial
039   * capacity.
040   */
041  public static <E> HashMultiset<E> create() {
042    return new HashMultiset<E>();
043  }
044
045  /**
046   * Creates a new, empty {@code HashMultiset} with the specified expected
047   * number of distinct elements.
048   *
049   * @param distinctElements the expected number of distinct elements
050   * @throws IllegalArgumentException if {@code distinctElements} is negative
051   */
052  public static <E> HashMultiset<E> create(int distinctElements) {
053    return new HashMultiset<E>(distinctElements);
054  }
055
056  /**
057   * Creates a new {@code HashMultiset} containing the specified elements.
058   *
059   * <p>This implementation is highly efficient when {@code elements} is itself
060   * a {@link Multiset}.
061   *
062   * @param elements the elements that the multiset should contain
063   */
064  public static <E> HashMultiset<E> create(Iterable<? extends E> elements) {
065    HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
066    Iterables.addAll(multiset, elements);
067    return multiset;
068  }
069
070  private HashMultiset() {
071    super(new HashMap<E, Count>());
072  }
073
074  private HashMultiset(int distinctElements) {
075    super(Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
076  }
077
078  /**
079   * @serialData the number of distinct elements, the first element, its count,
080   *     the second element, its count, and so on
081   */
082  @GwtIncompatible("java.io.ObjectOutputStream")
083  private void writeObject(ObjectOutputStream stream) throws IOException {
084    stream.defaultWriteObject();
085    Serialization.writeMultiset(this, stream);
086  }
087
088  @GwtIncompatible("java.io.ObjectInputStream")
089  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
090    stream.defaultReadObject();
091    int distinctElements = Serialization.readCount(stream);
092    setBackingMap(Maps.<E, Count>newHashMap());
093    Serialization.populateMultiset(this, stream, distinctElements);
094  }
095
096  @GwtIncompatible("Not needed in emulated source.")
097  private static final long serialVersionUID = 0;
098}