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 */
017 package org.apache.commons.collections.bag;
018
019 import java.io.IOException;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.io.Serializable;
023 import java.util.Collection;
024 import java.util.HashMap;
025
026 import org.apache.commons.collections.Bag;
027
028 /**
029 * Implements <code>Bag</code>, using a <code>HashMap</code> to provide the
030 * data storage. This is the standard implementation of a bag.
031 * <p>
032 * A <code>Bag</code> 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 * <code>Collection</code> interface specification.
037 *
038 * @since Commons Collections 3.0 (previously in main package v2.0)
039 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
040 *
041 * @author Chuck Burdick
042 * @author Stephen Colebourne
043 */
044 public class HashBag
045 extends AbstractMapBag implements Bag, Serializable {
046
047 /** Serial version lock */
048 private static final long serialVersionUID = -6561115435802554013L;
049
050 /**
051 * Constructs an empty <code>HashBag</code>.
052 */
053 public HashBag() {
054 super(new HashMap());
055 }
056
057 /**
058 * Constructs a bag containing all the members of the given collection.
059 *
060 * @param coll a collection to copy into this bag
061 */
062 public HashBag(Collection coll) {
063 this();
064 addAll(coll);
065 }
066
067 //-----------------------------------------------------------------------
068 /**
069 * Write the bag out using a custom routine.
070 */
071 private void writeObject(ObjectOutputStream out) throws IOException {
072 out.defaultWriteObject();
073 super.doWriteObject(out);
074 }
075
076 /**
077 * Read the bag in using a custom routine.
078 */
079 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
080 in.defaultReadObject();
081 super.doReadObject(new HashMap(), in);
082 }
083
084 }