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