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