1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.multiset;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.HashMap;
25
26 /**
27 * Implements {@code MultiSet}, using a {@link HashMap} to provide the
28 * data storage. This is the standard implementation of a multiset.
29 * <p>
30 * A {@code MultiSet} stores each object in the collection together with a
31 * count of occurrences. Extra methods on the interface allow multiple copies
32 * of an object to be added or removed at once.
33 * </p>
34 * <p>
35 * <strong>Note that HashMultiSet is not synchronized and is not thread-safe.</strong>
36 * If you wish to use this multiset from multiple threads concurrently, you must use
37 * appropriate synchronization. The simplest approach is to wrap this multiset using
38 * {@link org.apache.commons.collections4.MultiSetUtils#synchronizedMultiSet(org.apache.commons.collections4.MultiSet)
39 * MultiSetUtils.synchronizedMultiSet(MultiSet)}.
40 * Unsynchronized concurrent modification can corrupt the structure of the backing
41 * {@link HashMap}, which may cause subsequent operations to throw exceptions,
42 * return incorrect results, or loop indefinitely.
43 * </p>
44 *
45 * @param <E> The type held in the multiset
46 * @since 4.1
47 */
48 public class HashMultiSet<E> extends AbstractMapMultiSet<E> implements Serializable {
49
50 /** Serial version lock */
51 private static final long serialVersionUID = 20150610L;
52
53 /**
54 * Constructs an empty {@link HashMultiSet}.
55 */
56 public HashMultiSet() {
57 super(new HashMap<>());
58 }
59
60 /**
61 * Constructs a multiset containing all the members of the given collection.
62 *
63 * @param coll A collection to copy into this multiset
64 */
65 public HashMultiSet(final Collection<? extends E> coll) {
66 this();
67 addAll(coll);
68 }
69
70 /**
71 * Constructs a multiset containing all the members of the given Iterable.
72 *
73 * @param iterable An iterable to copy into this multiset.
74 * @since 4.6.0
75 */
76 public HashMultiSet(final Iterable<? extends E> iterable) {
77 super(new HashMap<>(), iterable);
78 }
79
80 /**
81 * Deserializes the multiset in using a custom routine.
82 *
83 * @param in The input stream
84 * @throws IOException Thrown if an error occurs while reading from the stream
85 * @throws ClassNotFoundException if an object read from the stream cannot be loaded
86 */
87 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
88 in.defaultReadObject();
89 setMap(new HashMap<>());
90 super.doReadObject(in);
91 }
92
93 /**
94 * Serializes this object to an ObjectOutputStream.
95 *
96 * @param out The target ObjectOutputStream.
97 * @throws IOException thrown when an I/O errors occur writing to the target stream.
98 */
99 private void writeObject(final ObjectOutputStream out) throws IOException {
100 out.defaultWriteObject();
101 super.doWriteObject(out);
102 }
103
104 }