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