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