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 org.jspecify.annotations.Nullable;
021
022/**
023 * A {@code Multiset} implementation with predictable iteration order. Its iterator orders elements
024 * according to when the first occurrence of the element was added. When the multiset contains
025 * multiple instances of an element, those instances are consecutive in the iteration order. If all
026 * occurrences of an element are removed, after which that element is added to the multiset, the
027 * element will appear at the end of the iteration.
028 *
029 * <p>See the Guava User Guide article on <a href=
030 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset">{@code Multiset}</a>.
031 *
032 * @author Kevin Bourrillion
033 * @author Jared Levy
034 * @since 2.0
035 */
036@GwtCompatible(serializable = true, emulated = true)
037public final class LinkedHashMultiset<E extends @Nullable Object>
038    extends AbstractMapBasedMultiset<E> {
039
040  /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */
041  public static <E extends @Nullable Object> LinkedHashMultiset<E> create() {
042    return create(ObjectCountHashMap.DEFAULT_SIZE);
043  }
044
045  /**
046   * Creates a new, empty {@code LinkedHashMultiset} 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> LinkedHashMultiset<E> create(int distinctElements) {
053    return new LinkedHashMultiset<>(distinctElements);
054  }
055
056  /**
057   * Creates a new {@code LinkedHashMultiset} 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> LinkedHashMultiset<E> create(
064      Iterable<? extends E> elements) {
065    LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
066    Iterables.addAll(multiset, elements);
067    return multiset;
068  }
069
070  LinkedHashMultiset(int distinctElements) {
071    super(distinctElements);
072  }
073
074  @Override
075  ObjectCountHashMap<E> newBackingMap(int distinctElements) {
076    return new ObjectCountLinkedHashMap<>(distinctElements);
077  }
078}