    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.multiset;
    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
    026/**
    027 * Implements {@code MultiSet}, using a {@link HashMap} to provide the
    028 * data storage. This is the standard implementation of a multiset.
    029 * <p>
    030 * A {@code MultiSet} stores each object in the collection together with a
    031 * count of occurrences. Extra methods on the interface allow multiple copies
    032 * of an object to be added or removed at once.
    033 * </p>
    034 * <p>
    035 * <strong>Note that HashMultiSet is not synchronized and is not thread-safe.</strong>
    036 * If you wish to use this multiset from multiple threads concurrently, you must use
    037 * appropriate synchronization. The simplest approach is to wrap this multiset using
    038 * {@link org.apache.commons.collections4.MultiSetUtils#synchronizedMultiSet(org.apache.commons.collections4.MultiSet)
    039 * MultiSetUtils.synchronizedMultiSet(MultiSet)}.
    040 * Unsynchronized concurrent modification can corrupt the structure of the backing
    041 * {@link HashMap}, which may cause subsequent operations to throw exceptions,
    042 * return incorrect results, or loop indefinitely.
    043 * </p>
    044 *
    045 * @param <E> The type held in the multiset
    046 * @since 4.1
    047 */
    048public class HashMultiSet<E> extends AbstractMapMultiSet<E> implements Serializable {
    049
    050    /** Serial version lock */
    051    private static final long serialVersionUID = 20150610L;
    052
    053    /**
    054     * Constructs an empty {@link HashMultiSet}.
    055     */
    056    public HashMultiSet() {
    057        super(new HashMap<>());
    058    }
    059
    060    /**
    061     * Constructs a multiset containing all the members of the given collection.
    062     *
    063     * @param coll  A collection to copy into this multiset
    064     */
    065    public HashMultiSet(final Collection<? extends E> coll) {
    066        this();
    067        addAll(coll);
    068    }
    069
    070    /**
    071     * Constructs a multiset containing all the members of the given Iterable.
    072     *
    073     * @param iterable An iterable to copy into this multiset.
    074     * @since 4.6.0
    075     */
    076    public HashMultiSet(final Iterable<? extends E> iterable) {
    077        super(new HashMap<>(), iterable);
    078    }
    079
    080    /**
    081     * Deserializes the multiset in using a custom routine.
    082     *
    083     * @param in The input stream
    084     * @throws IOException Thrown if an error occurs while reading from the stream
    085     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
    086     */
    087    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    088        in.defaultReadObject();
    089        setMap(new HashMap<>());
    090        super.doReadObject(in);
    091    }
    092
    093    /**
    094     * Serializes this object to an ObjectOutputStream.
    095     *
    096     * @param out The target ObjectOutputStream.
    097     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    098     */
    099    private void writeObject(final ObjectOutputStream out) throws IOException {
    100        out.defaultWriteObject();
    101        super.doWriteObject(out);
    102    }
    103
    104}