Coverage Summary for Class: HashMultiset (com.google.common.collect)

Class Class, % Method, % Line, %
HashMultiset 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.HashMap; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26  27 /** 28  * Multiset implementation backed by a {@link HashMap}. 29  * 30  * @author Kevin Bourrillion 31  * @author Jared Levy 32  * @since 2.0 33  */ 34 @GwtCompatible(serializable = true, emulated = true) 35 @ElementTypesAreNonnullByDefault 36 public final class HashMultiset<E extends @Nullable Object> extends AbstractMapBasedMultiset<E> { 37  38  /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */ 39  public static <E extends @Nullable Object> HashMultiset<E> create() { 40  return new HashMultiset<E>(); 41  } 42  43  /** 44  * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct 45  * elements. 46  * 47  * @param distinctElements the expected number of distinct elements 48  * @throws IllegalArgumentException if {@code distinctElements} is negative 49  */ 50  public static <E extends @Nullable Object> HashMultiset<E> create(int distinctElements) { 51  return new HashMultiset<E>(distinctElements); 52  } 53  54  /** 55  * Creates a new {@code HashMultiset} containing the specified elements. 56  * 57  * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. 58  * 59  * @param elements the elements that the multiset should contain 60  */ 61  public static <E extends @Nullable Object> HashMultiset<E> create( 62  Iterable<? extends E> elements) { 63  HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); 64  Iterables.addAll(multiset, elements); 65  return multiset; 66  } 67  68  private HashMultiset() { 69  super(new HashMap<E, Count>()); 70  } 71  72  private HashMultiset(int distinctElements) { 73  super(Maps.<E, Count>newHashMapWithExpectedSize(distinctElements)); 74  } 75  76  /** 77  * @serialData the number of distinct elements, the first element, its count, the second element, 78  * its count, and so on 79  */ 80  @GwtIncompatible // java.io.ObjectOutputStream 81  private void writeObject(ObjectOutputStream stream) throws IOException { 82  stream.defaultWriteObject(); 83  Serialization.writeMultiset(this, stream); 84  } 85  86  @GwtIncompatible // java.io.ObjectInputStream 87  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 88  stream.defaultReadObject(); 89  int distinctElements = Serialization.readCount(stream); 90  setBackingMap(Maps.<E, Count>newHashMap()); 91  Serialization.populateMultiset(this, stream, distinctElements); 92  } 93  94  @GwtIncompatible // Not needed in emulated source. 95  private static final long serialVersionUID = 0; 96 }