View Javadoc
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    *      http://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  /**
27   * Implements {@code Bag}, using a {@link HashMap} to provide the
28   * data storage. This is the standard implementation of a bag.
29   * <p>
30   * A {@code Bag} 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. It is important to read the
33   * interface Javadoc carefully as several methods violate the
34   * {@link Collection} interface specification.
35   * </p>
36   *
37   * @param <E> the type of elements in this bag
38   * @since 3.0 (previously in main package v2.0)
39   */
40  public class HashBag<E> extends AbstractMapBag<E> implements Serializable {
41  
42      /** Serial version lock */
43      private static final long serialVersionUID = -6561115435802554013L;
44  
45      /**
46       * Constructs an empty {@link HashBag}.
47       */
48      public HashBag() {
49          super(new HashMap<>());
50      }
51  
52      /**
53       * Constructs a bag containing all the members of the given collection.
54       *
55       * @param coll  a collection to copy into this bag
56       */
57      public HashBag(final Collection<? extends E> coll) {
58          this();
59          addAll(coll);
60      }
61  
62      /**
63       * Read the bag in using a custom routine.
64       *
65       * @param in  the input stream
66       * @throws IOException if an error occurs while reading from the stream
67       * @throws ClassNotFoundException if an object read from the stream can not be loaded
68       */
69      private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
70          in.defaultReadObject();
71          super.doReadObject(new HashMap<>(), in);
72      }
73  
74      /**
75       * Write the bag out using a custom routine.
76       *
77       * @param out  the output stream
78       * @throws IOException if an error occurs while writing to the stream
79       */
80      private void writeObject(final ObjectOutputStream out) throws IOException {
81          out.defaultWriteObject();
82          super.doWriteObject(out);
83      }
84  
85  }