• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 
21 /**
22  * A {@code Multiset} implementation with predictable iteration order. Its iterator orders elements
23  * according to when the first occurrence of the element was added. When the multiset contains
24  * multiple instances of an element, those instances are consecutive in the iteration order. If all
25  * occurrences of an element are removed, after which that element is added to the multiset, the
26  * element will appear at the end of the iteration.
27  *
28  * <p>See the Guava User Guide article on <a href=
29  * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> {@code
30  * Multiset}</a>.
31  *
32  * @author Kevin Bourrillion
33  * @author Jared Levy
34  * @since 2.0
35  */
36 @GwtCompatible(serializable = true, emulated = true)
37 public final class LinkedHashMultiset<E> extends AbstractMapBasedMultiset<E> {
38 
39   /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */
create()40   public static <E> LinkedHashMultiset<E> create() {
41     return create(ObjectCountHashMap.DEFAULT_SIZE);
42   }
43 
44   /**
45    * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct
46    * elements.
47    *
48    * @param distinctElements the expected number of distinct elements
49    * @throws IllegalArgumentException if {@code distinctElements} is negative
50    */
create(int distinctElements)51   public static <E> LinkedHashMultiset<E> create(int distinctElements) {
52     return new LinkedHashMultiset<E>(distinctElements);
53   }
54 
55   /**
56    * Creates a new {@code LinkedHashMultiset} containing the specified elements.
57    *
58    * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}.
59    *
60    * @param elements the elements that the multiset should contain
61    */
create(Iterable<? extends E> elements)62   public static <E> LinkedHashMultiset<E> create(Iterable<? extends E> elements) {
63     LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
64     Iterables.addAll(multiset, elements);
65     return multiset;
66   }
67 
LinkedHashMultiset(int distinctElements)68   LinkedHashMultiset(int distinctElements) {
69     super(distinctElements);
70   }
71 
72   @Override
init(int distinctElements)73   void init(int distinctElements) {
74     backingMap = new ObjectCountLinkedHashMap<>(distinctElements);
75   }
76 }
77