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.collect.Maps.newHashMapWithExpectedSize;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import java.io.IOException;
025import java.io.ObjectInputStream;
026import java.io.ObjectOutputStream;
027import java.util.HashMap;
028import org.jspecify.annotations.Nullable;
029
030/**
031 * Multiset implementation backed by a {@link HashMap}.
032 *
033 * @author Kevin Bourrillion
034 * @author Jared Levy
035 * @since 2.0
036 */
037@GwtCompatible
038public final class HashMultiset<E extends @Nullable Object> extends AbstractMapBasedMultiset<E> {
039
040  /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */
041  public static <E extends @Nullable Object> HashMultiset<E> create() {
042    return new HashMultiset<>();
043  }
044
045  /**
046   * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct
047   * elements.
048   *
049   * @param distinctElements the expected number of distinct elements
050   * @throws IllegalArgumentException if {@code distinctElements} is negative
051   */
052  public static <E extends @Nullable Object> HashMultiset<E> create(int distinctElements) {
053    return new HashMultiset<>(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 a {@link Multiset}.
060   *
061   * @param elements the elements that the multiset should contain
062   */
063  public static <E extends @Nullable Object> HashMultiset<E> create(
064      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<>());
072  }
073
074  private HashMultiset(int distinctElements) {
075    super(newHashMapWithExpectedSize(distinctElements));
076  }
077
078  /**
079   * @serialData the number of distinct elements, the first element, its count, the second element,
080   *     its count, and so on
081   */
082  @GwtIncompatible
083  @J2ktIncompatible
084    private void writeObject(ObjectOutputStream stream) throws IOException {
085    stream.defaultWriteObject();
086    Serialization.writeMultiset(this, stream);
087  }
088
089  @GwtIncompatible
090  @J2ktIncompatible
091    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
092    stream.defaultReadObject();
093    int distinctElements = stream.readInt();
094    setBackingMap(new HashMap<>());
095    Serialization.populateMultiset(this, stream, distinctElements);
096  }
097
098  @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
099}