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 com.google.common.annotations.GwtIncompatible;
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectOutputStream;
024import java.util.LinkedHashMap;
025
026/**
027 * A {@code Multiset} implementation with predictable iteration order. Its iterator orders elements
028 * according to when the first occurrence of the element was added. When the multiset contains
029 * multiple instances of an element, those instances are consecutive in the iteration order. If all
030 * occurrences of an element are removed, after which that element is added to the multiset, the
031 * element will appear at the end of the iteration.
032 *
033 * <p>See the Guava User Guide article on <a href=
034 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> {@code
035 * Multiset}</a>.
036 *
037 * @author Kevin Bourrillion
038 * @author Jared Levy
039 * @since 2.0
040 */
041@GwtCompatible(serializable = true, emulated = true)
042public final class LinkedHashMultiset<E> extends AbstractMapBasedMultiset<E> {
043
044  /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */
045  public static <E> LinkedHashMultiset<E> create() {
046    return new LinkedHashMultiset<E>();
047  }
048
049  /**
050   * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct
051   * elements.
052   *
053   * @param distinctElements the expected number of distinct elements
054   * @throws IllegalArgumentException if {@code distinctElements} is negative
055   */
056  public static <E> LinkedHashMultiset<E> create(int distinctElements) {
057    return new LinkedHashMultiset<E>(distinctElements);
058  }
059
060  /**
061   * Creates a new {@code LinkedHashMultiset} containing the specified elements.
062   *
063   * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}.
064   *
065   * @param elements the elements that the multiset should contain
066   */
067  public static <E> LinkedHashMultiset<E> create(Iterable<? extends E> elements) {
068    LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
069    Iterables.addAll(multiset, elements);
070    return multiset;
071  }
072
073  private LinkedHashMultiset() {
074    super(new LinkedHashMap<E, Count>());
075  }
076
077  private LinkedHashMultiset(int distinctElements) {
078    super(Maps.<E, Count>newLinkedHashMapWithExpectedSize(distinctElements));
079  }
080
081  /**
082   * @serialData the number of distinct elements, the first element, its count, the second element,
083   *     its count, and so on
084   */
085  @GwtIncompatible // java.io.ObjectOutputStream
086  private void writeObject(ObjectOutputStream stream) throws IOException {
087    stream.defaultWriteObject();
088    Serialization.writeMultiset(this, stream);
089  }
090
091  @GwtIncompatible // java.io.ObjectInputStream
092  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
093    stream.defaultReadObject();
094    int distinctElements = Serialization.readCount(stream);
095    setBackingMap(new LinkedHashMap<E, Count>());
096    Serialization.populateMultiset(this, stream, distinctElements);
097  }
098
099  @GwtIncompatible // not needed in emulated source
100  private static final long serialVersionUID = 0;
101}