1 /* 2 * Copyright (C) 2007 Google Inc. 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 21 import java.io.IOException; 22 import java.io.ObjectInputStream; 23 import java.io.ObjectOutputStream; 24 import java.util.HashMap; 25 import java.util.concurrent.atomic.AtomicInteger; 26 27 /** 28 * Multiset implementation backed by a {@link HashMap}. 29 * 30 * @author Kevin Bourrillion 31 * @author Jared Levy 32 * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library) 33 */ 34 @GwtCompatible(serializable = true) 35 public final class HashMultiset<E> extends AbstractMapBasedMultiset<E> { 36 37 /** 38 * Creates a new, empty {@code HashMultiset} using the default initial 39 * capacity. 40 */ create()41 public static <E> HashMultiset<E> create() { 42 return new HashMultiset<E>(); 43 } 44 45 /** 46 * Creates a new, empty {@code HashMultiset} with the specified expected 47 * number of distinct elements. 48 * 49 * @param distinctElements the expected number of distinct elements 50 * @throws IllegalArgumentException if {@code distinctElements} is negative 51 */ create(int distinctElements)52 public static <E> HashMultiset<E> create(int distinctElements) { 53 return new HashMultiset<E>(distinctElements); 54 } 55 56 /** 57 * Creates a new {@code HashMultiset} containing the specified elements. 58 * 59 * @param elements the elements that the multiset should contain 60 */ create(Iterable<? extends E> elements)61 public static <E> HashMultiset<E> create(Iterable<? extends E> elements) { 62 HashMultiset<E> multiset = 63 create(Multisets.inferDistinctElements(elements)); 64 Iterables.addAll(multiset, elements); 65 return multiset; 66 } 67 HashMultiset()68 private HashMultiset() { 69 super(new HashMap<E, AtomicInteger>()); 70 } 71 HashMultiset(int distinctElements)72 private HashMultiset(int distinctElements) { 73 super(new HashMap<E, AtomicInteger>(Maps.capacity(distinctElements))); 74 } 75 76 /** 77 * @serialData the number of distinct elements, the first element, its count, 78 * the second element, its count, and so on 79 */ writeObject(ObjectOutputStream stream)80 private void writeObject(ObjectOutputStream stream) throws IOException { 81 stream.defaultWriteObject(); 82 Serialization.writeMultiset(this, stream); 83 } 84 readObject(ObjectInputStream stream)85 private void readObject(ObjectInputStream stream) 86 throws IOException, ClassNotFoundException { 87 stream.defaultReadObject(); 88 int distinctElements = Serialization.readCount(stream); 89 setBackingMap( 90 Maps.<E, AtomicInteger>newHashMapWithExpectedSize(distinctElements)); 91 Serialization.populateMultiset(this, stream, distinctElements); 92 } 93 94 private static final long serialVersionUID = 0; 95 } 96