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