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.bag;
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 import org.apache.commons.collections4.multiset.HashMultiSet;
27
28 /**
29 * Implements {@code Bag}, using a {@link HashMap} to provide the
30 * data storage. This is the standard implementation of a bag.
31 * <p>
32 * A {@code Bag} stores each object in the collection together with a
33 * count of occurrences. Extra methods on the interface allow multiple copies
34 * of an object to be added or removed at once. It is important to read the
35 * interface Javadoc carefully as several methods violate the
36 * {@link Collection} interface specification.
37 * </p>
38 * <p>
39 * <strong>Note that HashBag is not synchronized and is not thread-safe.</strong>
40 * If you wish to use this bag from multiple threads concurrently, you must use
41 * appropriate synchronization. The simplest approach is to wrap this bag using
42 * {@link org.apache.commons.collections4.BagUtils#synchronizedBag(org.apache.commons.collections4.Bag)
43 * BagUtils.synchronizedBag(Bag)}.
44 * Unsynchronized concurrent modification can corrupt the structure of the backing
45 * {@link HashMap}, which may cause subsequent operations to throw exceptions,
46 * return incorrect results, or loop indefinitely.
47 * </p>
48 *
49 * @param <E> The type of elements in this bag
50 * @since 3.0 (previously in main package v2.0)
51 * @deprecated Since 4.6.0, use {@link HashMultiSet} instead.
52 */
53 @Deprecated
54 public class HashBag<E> extends AbstractMapBag<E> implements Serializable {
55
56 /** Serial version lock */
57 private static final long serialVersionUID = -6561115435802554013L;
58
59 /**
60 * Constructs an empty {@link HashBag}.
61 */
62 public HashBag() {
63 super(new HashMap<>());
64 }
65
66 /**
67 * Constructs a bag containing all the members of the given Collection.
68 *
69 * @param collection A collection to copy into this bag.
70 */
71 public HashBag(final Collection<? extends E> collection) {
72 this();
73 addAll(collection);
74 }
75
76 /**
77 * Constructs a bag containing all the members of the given Iterable.
78 *
79 * @param iterable An iterable to copy into this bag.
80 * @since 4.5.0-M3
81 */
82 public HashBag(final Iterable<? extends E> iterable) {
83 super(new HashMap<>(), iterable);
84 }
85
86 /**
87 * Deserializes the bag in using a custom routine.
88 *
89 * @param in The input stream
90 * @throws IOException Thrown if an error occurs while reading from the stream
91 * @throws ClassNotFoundException if an object read from the stream cannot be loaded
92 */
93 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
94 in.defaultReadObject();
95 super.doReadObject(new HashMap<>(), in);
96 }
97
98 /**
99 * Serializes this object to an ObjectOutputStream.
100 *
101 * @param out The target ObjectOutputStream.
102 * @throws IOException thrown when an I/O errors occur writing to the target stream.
103 */
104 private void writeObject(final ObjectOutputStream out) throws IOException {
105 out.defaultWriteObject();
106 super.doWriteObject(out);
107 }
108
109 }