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