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