1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.IOException;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.ObjectInputStream;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.ObjectOutputStream;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.HashMap;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Multiset implementation backed by a {@link HashMap}.
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class HashMultiset<E> extends AbstractMapBasedMultiset<E> {
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Creates a new, empty {@code HashMultiset} using the default initial
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * capacity.
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <E> HashMultiset<E> create() {
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new HashMultiset<E>();
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Creates a new, empty {@code HashMultiset} with the specified expected
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * number of distinct elements.
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param distinctElements the expected number of distinct elements
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code distinctElements} is negative
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <E> HashMultiset<E> create(int distinctElements) {
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new HashMultiset<E>(distinctElements);
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Creates a new {@code HashMultiset} containing the specified elements.
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>This implementation is highly efficient when {@code elements} is itself
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * a {@link Multiset}.
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elements the elements that the multiset should contain
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <E> HashMultiset<E> create(Iterable<? extends E> elements) {
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    HashMultiset<E> multiset =
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        create(Multisets.inferDistinctElements(elements));
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Iterables.addAll(multiset, elements);
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return multiset;
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private HashMultiset() {
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    super(new HashMap<E, Count>());
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private HashMultiset(int distinctElements) {
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    super(Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @serialData the number of distinct elements, the first element, its count,
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     the second element, its count, and so on
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.io.ObjectOutputStream")
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private void writeObject(ObjectOutputStream stream) throws IOException {
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    stream.defaultWriteObject();
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Serialization.writeMultiset(this, stream);
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.io.ObjectInputStream")
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private void readObject(ObjectInputStream stream)
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      throws IOException, ClassNotFoundException {
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    stream.defaultReadObject();
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int distinctElements = Serialization.readCount(stream);
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    setBackingMap(
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Serialization.populateMultiset(this, stream, distinctElements);
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("Not needed in emulated source.")
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final long serialVersionUID = 0;
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
102