View Javadoc
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  import com.google.common.annotations.GwtIncompatible;
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.util.LinkedHashMap;
25  
26  /**
27   * A {@code Multiset} implementation with predictable iteration order. Its
28   * iterator orders elements according to when the first occurrence of the
29   * element was added. When the multiset contains multiple instances of an
30   * element, those instances are consecutive in the iteration order. If all
31   * occurrences of an element are removed, after which that element is added to
32   * the multiset, the element will appear at the end of the iteration.
33   *
34   * <p>See the Guava User Guide article on <a href=
35   * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset">
36   * {@code Multiset}</a>.
37   *
38   * @author Kevin Bourrillion
39   * @author Jared Levy
40   * @since 2.0
41   */
42  @GwtCompatible(serializable = true, emulated = true)
43  @SuppressWarnings("serial") // we're overriding default serialization
44  public final class LinkedHashMultiset<E> extends AbstractMapBasedMultiset<E> {
45  
46    /**
47     * Creates a new, empty {@code LinkedHashMultiset} using the default initial
48     * capacity.
49     */
50    public static <E> LinkedHashMultiset<E> create() {
51      return new LinkedHashMultiset<E>();
52    }
53  
54    /**
55     * Creates a new, empty {@code LinkedHashMultiset} with the specified expected
56     * number of distinct elements.
57     *
58     * @param distinctElements the expected number of distinct elements
59     * @throws IllegalArgumentException if {@code distinctElements} is negative
60     */
61    public static <E> LinkedHashMultiset<E> create(int distinctElements) {
62      return new LinkedHashMultiset<E>(distinctElements);
63    }
64  
65    /**
66     * Creates a new {@code LinkedHashMultiset} containing the specified elements.
67     *
68     * <p>This implementation is highly efficient when {@code elements} is itself
69     * a {@link Multiset}.
70     *
71     * @param elements the elements that the multiset should contain
72     */
73    public static <E> LinkedHashMultiset<E> create(Iterable<? extends E> elements) {
74      LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
75      Iterables.addAll(multiset, elements);
76      return multiset;
77    }
78  
79    private LinkedHashMultiset() {
80      super(new LinkedHashMap<E, Count>());
81    }
82  
83    private LinkedHashMultiset(int distinctElements) {
84      super(Maps.<E, Count>newLinkedHashMapWithExpectedSize(distinctElements));
85    }
86  
87    /**
88     * @serialData the number of distinct elements, the first element, its count,
89     *     the second element, its count, and so on
90     */
91    @GwtIncompatible // java.io.ObjectOutputStream
92    private void writeObject(ObjectOutputStream stream) throws IOException {
93      stream.defaultWriteObject();
94      Serialization.writeMultiset(this, stream);
95    }
96  
97    @GwtIncompatible // java.io.ObjectInputStream
98    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
99      stream.defaultReadObject();
100     int distinctElements = Serialization.readCount(stream);
101     setBackingMap(new LinkedHashMap<E, Count>());
102     Serialization.populateMultiset(this, stream, distinctElements);
103   }
104 
105   @GwtIncompatible // not needed in emulated source
106   private static final long serialVersionUID = 0;
107 }