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;
020
021/**
022 * A {@code Multiset} implementation with predictable iteration order. Its iterator orders elements
023 * according to when the first occurrence of the element was added. When the multiset contains
024 * multiple instances of an element, those instances are consecutive in the iteration order. If all
025 * occurrences of an element are removed, after which that element is added to the multiset, the
026 * element will appear at the end of the iteration.
027 *
028 * <p>See the Guava User Guide article on <a href=
029 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> {@code
030 * 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 AbstractMapBasedMultiset<E> {
038
039  /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */
040  public static <E> LinkedHashMultiset<E> create() {
041    return create(ObjectCountHashMap.DEFAULT_SIZE);
042  }
043
044  /**
045   * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct
046   * elements.
047   *
048   * @param distinctElements the expected number of distinct elements
049   * @throws IllegalArgumentException if {@code distinctElements} is negative
050   */
051  public static <E> LinkedHashMultiset<E> create(int distinctElements) {
052    return new LinkedHashMultiset<E>(distinctElements);
053  }
054
055  /**
056   * Creates a new {@code LinkedHashMultiset} containing the specified elements.
057   *
058   * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}.
059   *
060   * @param elements the elements that the multiset should contain
061   */
062  public static <E> LinkedHashMultiset<E> create(Iterable<? extends E> elements) {
063    LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
064    Iterables.addAll(multiset, elements);
065    return multiset;
066  }
067
068  LinkedHashMultiset(int distinctElements) {
069    super(distinctElements);
070  }
071
072  @Override
073  void init(int distinctElements) {
074    backingMap = new ObjectCountLinkedHashMap<>(distinctElements);
075  }
076}