001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.bag;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023import java.util.Collection;
024import java.util.HashMap;
025
026import org.apache.commons.collections4.multiset.HashMultiSet;
027
028/**
029 * Implements {@code Bag}, using a {@link HashMap} to provide the
030 * data storage. This is the standard implementation of a bag.
031 * <p>
032 * A {@code Bag} stores each object in the collection together with a
033 * count of occurrences. Extra methods on the interface allow multiple copies
034 * of an object to be added or removed at once. It is important to read the
035 * interface Javadoc carefully as several methods violate the
036 * {@link Collection} interface specification.
037 * </p>
038 * <p>
039 * <strong>Note that HashBag is not synchronized and is not thread-safe.</strong>
040 * If you wish to use this bag from multiple threads concurrently, you must use
041 * appropriate synchronization. The simplest approach is to wrap this bag using
042 * {@link org.apache.commons.collections4.BagUtils#synchronizedBag(org.apache.commons.collections4.Bag)
043 * BagUtils.synchronizedBag(Bag)}.
044 * Unsynchronized concurrent modification can corrupt the structure of the backing
045 * {@link HashMap}, which may cause subsequent operations to throw exceptions,
046 * return incorrect results, or loop indefinitely.
047 * </p>
048 *
049 * @param <E> The type of elements in this bag
050 * @since 3.0 (previously in main package v2.0)
051 * @deprecated Since 4.6.0, use {@link HashMultiSet} instead.
052 */
053@Deprecated
054public class HashBag<E> extends AbstractMapBag<E> implements Serializable {
055
056    /** Serial version lock */
057    private static final long serialVersionUID = -6561115435802554013L;
058
059    /**
060     * Constructs an empty {@link HashBag}.
061     */
062    public HashBag() {
063        super(new HashMap<>());
064    }
065
066    /**
067     * Constructs a bag containing all the members of the given Collection.
068     *
069     * @param collection A collection to copy into this bag.
070     */
071    public HashBag(final Collection<? extends E> collection) {
072        this();
073        addAll(collection);
074    }
075
076    /**
077     * Constructs a bag containing all the members of the given Iterable.
078     *
079     * @param iterable An iterable to copy into this bag.
080     * @since 4.5.0-M3
081     */
082    public HashBag(final Iterable<? extends E> iterable) {
083        super(new HashMap<>(), iterable);
084    }
085
086    /**
087     * Deserializes the bag in using a custom routine.
088     *
089     * @param in  The input stream
090     * @throws IOException Thrown if an error occurs while reading from the stream
091     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
092     */
093    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
094        in.defaultReadObject();
095        super.doReadObject(new HashMap<>(), in);
096    }
097
098    /**
099     * 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}