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 * http://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 026/** 027 * Implements {@code Bag}, using a {@link HashMap} to provide the 028 * data storage. This is the standard implementation of a bag. 029 * <p> 030 * A {@code Bag} 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. It is important to read the 033 * interface Javadoc carefully as several methods violate the 034 * {@link Collection} interface specification. 035 * </p> 036 * 037 * @param <E> the type of elements in this bag 038 * @since 3.0 (previously in main package v2.0) 039 */ 040public class HashBag<E> extends AbstractMapBag<E> implements Serializable { 041 042 /** Serial version lock */ 043 private static final long serialVersionUID = -6561115435802554013L; 044 045 /** 046 * Constructs an empty {@link HashBag}. 047 */ 048 public HashBag() { 049 super(new HashMap<>()); 050 } 051 052 /** 053 * Constructs a bag containing all the members of the given Collection. 054 * 055 * @param collection a collection to copy into this bag. 056 */ 057 public HashBag(final Collection<? extends E> collection) { 058 this(); 059 addAll(collection); 060 } 061 062 /** 063 * Constructs a bag containing all the members of the given Iterable. 064 * 065 * @param iterable an iterable to copy into this bag. 066 * @since 4.5.0-M3 067 */ 068 public HashBag(final Iterable<? extends E> iterable) { 069 super(new HashMap<>(), iterable); 070 } 071 072 /** 073 * Deserializes the bag in using a custom routine. 074 * 075 * @param in the input stream 076 * @throws IOException if an error occurs while reading from the stream 077 * @throws ClassNotFoundException if an object read from the stream cannot be loaded 078 */ 079 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 080 in.defaultReadObject(); 081 super.doReadObject(new HashMap<>(), in); 082 } 083 084 /** 085 * Serializes this object to an ObjectOutputStream. 086 * 087 * @param out the target ObjectOutputStream. 088 * @throws IOException thrown when an I/O errors occur writing to the target stream. 089 */ 090 private void writeObject(final ObjectOutputStream out) throws IOException { 091 out.defaultWriteObject(); 092 super.doWriteObject(out); 093 } 094 095}