• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.checkerframework.checker.nullness.qual.Nullable;
22 
23 /**
24  * Multiset implementation that uses hashing for key and entry access.
25  *
26  * @author Kevin Bourrillion
27  * @author Jared Levy
28  * @since 2.0
29  */
30 @GwtCompatible(serializable = true, emulated = true)
31 @ElementTypesAreNonnullByDefault
32 public final class HashMultiset<E extends @Nullable Object> extends AbstractMapBasedMultiset<E> {
33 
34   /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */
create()35   public static <E extends @Nullable Object> HashMultiset<E> create() {
36     return create(ObjectCountHashMap.DEFAULT_SIZE);
37   }
38 
39   /**
40    * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct
41    * elements.
42    *
43    * @param distinctElements the expected number of distinct elements
44    * @throws IllegalArgumentException if {@code distinctElements} is negative
45    */
create(int distinctElements)46   public static <E extends @Nullable Object> HashMultiset<E> create(int distinctElements) {
47     return new HashMultiset<E>(distinctElements);
48   }
49 
50   /**
51    * Creates a new {@code HashMultiset} containing the specified elements.
52    *
53    * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}.
54    *
55    * @param elements the elements that the multiset should contain
56    */
create( Iterable<? extends E> elements)57   public static <E extends @Nullable Object> HashMultiset<E> create(
58       Iterable<? extends E> elements) {
59     HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements));
60     Iterables.addAll(multiset, elements);
61     return multiset;
62   }
63 
HashMultiset(int distinctElements)64   HashMultiset(int distinctElements) {
65     super(distinctElements);
66   }
67 
68   @Override
newBackingMap(int distinctElements)69   ObjectCountHashMap<E> newBackingMap(int distinctElements) {
70     return new ObjectCountHashMap<>(distinctElements);
71   }
72 
73   @GwtIncompatible // Not needed in emulated source.
74   private static final long serialVersionUID = 0;
75 }
76