01: /*
02: * Copyright 2002-2005 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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: 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: 348299 $ $Date: 2005-11-22 23:51:45 +0000 (Tue, 22 Nov 2005) $
39: *
40: * @author Chuck Burdick
41: * @author Stephen Colebourne
42: */
43: public class HashBag extends AbstractMapBag implements Bag,
44: Serializable {
45:
46: /** Serial version lock */
47: private static final long serialVersionUID = -6561115435802554013L;
48:
49: /**
50: * Constructs an empty <code>HashBag</code>.
51: */
52: public HashBag() {
53: super (new HashMap());
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 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 out) throws IOException {
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 in) throws IOException,
79: ClassNotFoundException {
80: in.defaultReadObject();
81: super .doReadObject(new HashMap(), in);
82: }
83:
84: }
|