HashMultiSet.java

  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.multiset;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.util.Collection;
  23. import java.util.HashMap;

  24. /**
  25.  * Implements {@code MultiSet}, using a {@link HashMap} to provide the
  26.  * data storage. This is the standard implementation of a multiset.
  27.  * <p>
  28.  * A {@code MultiSet} stores each object in the collection together with a
  29.  * count of occurrences. Extra methods on the interface allow multiple copies
  30.  * of an object to be added or removed at once.
  31.  * </p>
  32.  *
  33.  * @param <E> the type held in the multiset
  34.  * @since 4.1
  35.  */
  36. public class HashMultiSet<E> extends AbstractMapMultiSet<E> implements Serializable {

  37.     /** Serial version lock */
  38.     private static final long serialVersionUID = 20150610L;

  39.     /**
  40.      * Constructs an empty {@link HashMultiSet}.
  41.      */
  42.     public HashMultiSet() {
  43.         super(new HashMap<>());
  44.     }

  45.     /**
  46.      * Constructs a multiset containing all the members of the given collection.
  47.      *
  48.      * @param coll  a collection to copy into this multiset
  49.      */
  50.     public HashMultiSet(final Collection<? extends E> coll) {
  51.         this();
  52.         addAll(coll);
  53.     }

  54.     /**
  55.      * Deserializes the multiset in using a custom routine.
  56.      *
  57.      * @param in the input stream
  58.      * @throws IOException if an error occurs while reading from the stream
  59.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  60.      */
  61.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  62.         in.defaultReadObject();
  63.         setMap(new HashMap<>());
  64.         super.doReadObject(in);
  65.     }

  66.     /**
  67.      * Serializes this object to an ObjectOutputStream.
  68.      *
  69.      * @param out the target ObjectOutputStream.
  70.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  71.      */
  72.     private void writeObject(final ObjectOutputStream out) throws IOException {
  73.         out.defaultWriteObject();
  74.         super.doWriteObject(out);
  75.     }

  76. }