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