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.checkerframework.checker.nullness.qual.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)
037@ElementTypesAreNonnullByDefault
038public final class LinkedHashMultiset<E extends @Nullable Object>
039    extends AbstractMapBasedMultiset<E> {
040
041  /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */
042  public static <E extends @Nullable Object> LinkedHashMultiset<E> create() {
043    return create(ObjectCountHashMap.DEFAULT_SIZE);
044  }
045
046  /**
047   * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct
048   * elements.
049   *
050   * @param distinctElements the expected number of distinct elements
051   * @throws IllegalArgumentException if {@code distinctElements} is negative
052   */
053  public static <E extends @Nullable Object> LinkedHashMultiset<E> create(int distinctElements) {
054    return new LinkedHashMultiset<>(distinctElements);
055  }
056
057  /**
058   * Creates a new {@code LinkedHashMultiset} containing the specified elements.
059   *
060   * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}.
061   *
062   * @param elements the elements that the multiset should contain
063   */
064  public static <E extends @Nullable Object> LinkedHashMultiset<E> create(
065      Iterable<? extends E> elements) {
066    LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
067    Iterables.addAll(multiset, elements);
068    return multiset;
069  }
070
071  LinkedHashMultiset(int distinctElements) {
072    super(distinctElements);
073  }
074
075  @Override
076  ObjectCountHashMap<E> newBackingMap(int distinctElements) {
077    return new ObjectCountLinkedHashMap<>(distinctElements);
078  }
079}