KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > HashBag


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.bag;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.commons.collections.Bag;
26
27 /**
28  * Implements <code>Bag</code>, using a <code>HashMap</code> to provide the
29  * data storage. This is the standard implementation of a bag.
30  * <p>
31  * A <code>Bag</code> stores each object in the collection together with a
32  * count of occurrences. Extra methods on the interface allow multiple copies
33  * of an object to be added or removed at once. It is important to read the
34  * interface javadoc carefully as several methods violate the
35  * <code>Collection</code> interface specification.
36  *
37  * @since Commons Collections 3.0 (previously in main package v2.0)
38  * @version $Revision: 1.8 $ $Date: 2004/02/18 00:56:25 $
39  *
40  * @author Chuck Burdick
41  * @author Stephen Colebourne
42  */

43 public class HashBag
44         extends AbstractMapBag implements Bag, Serializable JavaDoc {
45
46     /** Serial version lock */
47     static final long serialVersionUID = -6561115435802554013L;
48     
49     /**
50      * Constructs an empty <code>HashBag</code>.
51      */

52     public HashBag() {
53         super(new HashMap JavaDoc());
54     }
55
56     /**
57      * Constructs a bag containing all the members of the given collection.
58      *
59      * @param coll a collection to copy into this bag
60      */

61     public HashBag(Collection JavaDoc coll) {
62         this();
63         addAll(coll);
64     }
65
66     //-----------------------------------------------------------------------
67
/**
68      * Write the bag out using a custom routine.
69      */

70     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
71         out.defaultWriteObject();
72         super.doWriteObject(out);
73     }
74
75     /**
76      * Read the bag in using a custom routine.
77      */

78     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
79         in.defaultReadObject();
80         super.doReadObject(new HashMap JavaDoc(), in);
81     }
82     
83 }
84
Popular Tags