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 java.io.IOException; 022import java.io.ObjectInputStream; 023import java.io.ObjectOutputStream; 024import java.util.HashMap; 025 026/** 027 * Multiset implementation backed by a {@link HashMap}. 028 * 029 * @author Kevin Bourrillion 030 * @author Jared Levy 031 * @since 2.0 032 */ 033@GwtCompatible(serializable = true, emulated = true) 034public final class HashMultiset<E> extends AbstractMapBasedMultiset<E> { 035 036 /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */ 037 public static <E> HashMultiset<E> create() { 038 return new HashMultiset<E>(); 039 } 040 041 /** 042 * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct 043 * elements. 044 * 045 * @param distinctElements the expected number of distinct elements 046 * @throws IllegalArgumentException if {@code distinctElements} is negative 047 */ 048 public static <E> HashMultiset<E> create(int distinctElements) { 049 return new HashMultiset<E>(distinctElements); 050 } 051 052 /** 053 * Creates a new {@code HashMultiset} containing the specified elements. 054 * 055 * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. 056 * 057 * @param elements the elements that the multiset should contain 058 */ 059 public static <E> HashMultiset<E> create(Iterable<? extends E> elements) { 060 HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); 061 Iterables.addAll(multiset, elements); 062 return multiset; 063 } 064 065 private HashMultiset() { 066 super(new HashMap<E, Count>()); 067 } 068 069 private HashMultiset(int distinctElements) { 070 super(Maps.<E, Count>newHashMapWithExpectedSize(distinctElements)); 071 } 072 073 /** 074 * @serialData the number of distinct elements, the first element, its count, the second element, 075 * its count, and so on 076 */ 077 @GwtIncompatible // java.io.ObjectOutputStream 078 private void writeObject(ObjectOutputStream stream) throws IOException { 079 stream.defaultWriteObject(); 080 Serialization.writeMultiset(this, stream); 081 } 082 083 @GwtIncompatible // java.io.ObjectInputStream 084 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 085 stream.defaultReadObject(); 086 int distinctElements = Serialization.readCount(stream); 087 setBackingMap(Maps.<E, Count>newHashMap()); 088 Serialization.populateMultiset(this, stream, distinctElements); 089 } 090 091 @GwtIncompatible // Not needed in emulated source. 092 private static final long serialVersionUID = 0; 093}