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 collection a collection to copy into this bag. 56 */ 57 public HashBag(final Collection<? extends E> collection) { 58 this(); 59 addAll(collection); 60 } 61 62 /** 63 * Constructs a bag containing all the members of the given Iterable. 64 * 65 * @param iterable an iterable to copy into this bag. 66 * @since 4.5.0-M3 67 */ 68 public HashBag(final Iterable<? extends E> iterable) { 69 super(new HashMap<>(), iterable); 70 } 71 72 /** 73 * Deserializes the bag in using a custom routine. 74 * 75 * @param in the input stream 76 * @throws IOException if an error occurs while reading from the stream 77 * @throws ClassNotFoundException if an object read from the stream cannot be loaded 78 */ 79 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 80 in.defaultReadObject(); 81 super.doReadObject(new HashMap<>(), in); 82 } 83 84 /** 85 * Serializes this object to an ObjectOutputStream. 86 * 87 * @param out the target ObjectOutputStream. 88 * @throws IOException thrown when an I/O errors occur writing to the target stream. 89 */ 90 private void writeObject(final ObjectOutputStream out) throws IOException { 91 out.defaultWriteObject(); 92 super.doWriteObject(out); 93 } 94 95 }