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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.annotations.GwtIncompatible;
21
22import java.io.IOException;
23import java.io.ObjectInputStream;
24import java.io.ObjectOutputStream;
25import java.util.HashMap;
26
27/**
28 * Multiset implementation backed by a {@link HashMap}.
29 *
30 * @author Kevin Bourrillion
31 * @author Jared Levy
32 * @since 2.0 (imported from Google Collections Library)
33 */
34@GwtCompatible(serializable = true, emulated = true)
35public final class HashMultiset<E> extends AbstractMapBasedMultiset<E> {
36
37  /**
38   * Creates a new, empty {@code HashMultiset} using the default initial
39   * capacity.
40   */
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   */
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   * <p>This implementation is highly efficient when {@code elements} is itself
60   * a {@link Multiset}.
61   *
62   * @param elements the elements that the multiset should contain
63   */
64  public static <E> HashMultiset<E> create(Iterable<? extends E> elements) {
65    HashMultiset<E> multiset =
66        create(Multisets.inferDistinctElements(elements));
67    Iterables.addAll(multiset, elements);
68    return multiset;
69  }
70
71  private HashMultiset() {
72    super(new HashMap<E, Count>());
73  }
74
75  private HashMultiset(int distinctElements) {
76    super(Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
77  }
78
79  /**
80   * @serialData the number of distinct elements, the first element, its count,
81   *     the second element, its count, and so on
82   */
83  @GwtIncompatible("java.io.ObjectOutputStream")
84  private void writeObject(ObjectOutputStream stream) throws IOException {
85    stream.defaultWriteObject();
86    Serialization.writeMultiset(this, stream);
87  }
88
89  @GwtIncompatible("java.io.ObjectInputStream")
90  private void readObject(ObjectInputStream stream)
91      throws IOException, ClassNotFoundException {
92    stream.defaultReadObject();
93    int distinctElements = Serialization.readCount(stream);
94    setBackingMap(
95        Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
96    Serialization.populateMultiset(this, stream, distinctElements);
97  }
98
99  @GwtIncompatible("Not needed in emulated source.")
100  private static final long serialVersionUID = 0;
101}
102