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