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 static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.collect.CollectPreconditions.checkNonnegative;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.primitives.Ints;
025import com.google.errorprone.annotations.CanIgnoreReturnValue;
026import java.io.IOException;
027import java.io.ObjectInputStream;
028import java.io.ObjectOutputStream;
029import java.io.Serializable;
030import java.util.ConcurrentModificationException;
031import java.util.Iterator;
032import java.util.NoSuchElementException;
033import org.checkerframework.checker.nullness.compatqual.NullableDecl;
034
035/**
036 * Multiset implementation that uses hashing for key and entry access.
037 *
038 * @author Kevin Bourrillion
039 * @author Jared Levy
040 * @since 2.0
041 */
042@GwtCompatible(serializable = true, emulated = true)
043public class HashMultiset<E> extends AbstractMapBasedMultiset<E> {
044
045  /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */
046  public static <E> HashMultiset<E> create() {
047    return create(ObjectCountHashMap.DEFAULT_SIZE);
048  }
049
050  /**
051   * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct
052   * elements.
053   *
054   * @param distinctElements the expected number of distinct elements
055   * @throws IllegalArgumentException if {@code distinctElements} is negative
056   */
057  public static <E> HashMultiset<E> create(int distinctElements) {
058    return new HashMultiset<E>(distinctElements);
059  }
060
061  /**
062   * Creates a new {@code HashMultiset} containing the specified elements.
063   *
064   * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}.
065   *
066   * @param elements the elements that the multiset should contain
067   */
068  public static <E> HashMultiset<E> create(Iterable<? extends E> elements) {
069    HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
070    Iterables.addAll(multiset, elements);
071    return multiset;
072  }
073
074  HashMultiset(int distinctElements) {
075    super(distinctElements);
076  }
077
078  @Override
079  void init(int distinctElements) {
080    backingMap = new ObjectCountHashMap<>(distinctElements);
081  }
082
083  @GwtIncompatible // Not needed in emulated source.
084  private static final long serialVersionUID = 0;
085}