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