Coverage Summary for Class: LinkedHashMultiset (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| LinkedHashMultiset | 100% (1/1) | 28.6% (2/7) | 17.6% (3/17) |
1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import java.io.IOException; 22 import java.io.ObjectInputStream; 23 import java.io.ObjectOutputStream; 24 import java.util.LinkedHashMap; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26 27 /** 28 * A {@code Multiset} implementation with predictable iteration order. Its iterator orders elements 29 * according to when the first occurrence of the element was added. When the multiset contains 30 * multiple instances of an element, those instances are consecutive in the iteration order. If all 31 * occurrences of an element are removed, after which that element is added to the multiset, the 32 * element will appear at the end of the iteration. 33 * 34 * <p>See the Guava User Guide article on <a href= 35 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> {@code 36 * Multiset}</a>. 37 * 38 * @author Kevin Bourrillion 39 * @author Jared Levy 40 * @since 2.0 41 */ 42 @GwtCompatible(serializable = true, emulated = true) 43 @ElementTypesAreNonnullByDefault 44 public final class LinkedHashMultiset<E extends @Nullable Object> 45 extends AbstractMapBasedMultiset<E> { 46 47 /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */ 48 public static <E extends @Nullable Object> LinkedHashMultiset<E> create() { 49 return new LinkedHashMultiset<E>(); 50 } 51 52 /** 53 * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct 54 * elements. 55 * 56 * @param distinctElements the expected number of distinct elements 57 * @throws IllegalArgumentException if {@code distinctElements} is negative 58 */ 59 public static <E extends @Nullable Object> LinkedHashMultiset<E> create(int distinctElements) { 60 return new LinkedHashMultiset<E>(distinctElements); 61 } 62 63 /** 64 * Creates a new {@code LinkedHashMultiset} containing the specified elements. 65 * 66 * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. 67 * 68 * @param elements the elements that the multiset should contain 69 */ 70 public static <E extends @Nullable Object> LinkedHashMultiset<E> create( 71 Iterable<? extends E> elements) { 72 LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); 73 Iterables.addAll(multiset, elements); 74 return multiset; 75 } 76 77 private LinkedHashMultiset() { 78 super(new LinkedHashMap<E, Count>()); 79 } 80 81 private LinkedHashMultiset(int distinctElements) { 82 super(Maps.<E, Count>newLinkedHashMapWithExpectedSize(distinctElements)); 83 } 84 85 /** 86 * @serialData the number of distinct elements, the first element, its count, the second element, 87 * its count, and so on 88 */ 89 @GwtIncompatible // java.io.ObjectOutputStream 90 private void writeObject(ObjectOutputStream stream) throws IOException { 91 stream.defaultWriteObject(); 92 Serialization.writeMultiset(this, stream); 93 } 94 95 @GwtIncompatible // java.io.ObjectInputStream 96 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 97 stream.defaultReadObject(); 98 int distinctElements = Serialization.readCount(stream); 99 setBackingMap(new LinkedHashMap<E, Count>()); 100 Serialization.populateMultiset(this, stream, distinctElements); 101 } 102 103 @GwtIncompatible // not needed in emulated source 104 private static final long serialVersionUID = 0; 105 }