KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
24 import java.util.SortedMap JavaDoc;
25 import java.util.TreeMap JavaDoc;
26
27 import org.apache.commons.collections.SortedBag;
28
29 /**
30  * Implements <code>SortedBag</code>, using a <code>TreeMap</code> to provide
31  * the data storage. This is the standard implementation of a sorted bag.
32  * <p>
33  * Order will be maintained among the bag members and can be viewed through the
34  * iterator.
35  * <p>
36  * A <code>Bag</code> stores each object in the collection together with a
37  * count of occurrences. Extra methods on the interface allow multiple copies
38  * of an object to be added or removed at once. It is important to read the
39  * interface javadoc carefully as several methods violate the
40  * <code>Collection</code> interface specification.
41  *
42  * @since Commons Collections 3.0 (previously in main package v2.0)
43  * @version $Revision: 1.9 $ $Date: 2004/02/18 00:56:25 $
44  *
45  * @author Chuck Burdick
46  * @author Stephen Colebourne
47  */

48 public class TreeBag
49         extends AbstractMapBag implements SortedBag, Serializable JavaDoc {
50
51     /** Serial version lock */
52     static final long serialVersionUID = -7740146511091606676L;
53     
54     /**
55      * Constructs an empty <code>TreeBag</code>.
56      */

57     public TreeBag() {
58         super(new TreeMap JavaDoc());
59     }
60
61     /**
62      * Constructs an empty bag that maintains order on its unique
63      * representative members according to the given {@link Comparator}.
64      *
65      * @param comparator the comparator to use
66      */

67     public TreeBag(Comparator JavaDoc comparator) {
68         super(new TreeMap JavaDoc(comparator));
69     }
70
71     /**
72      * Constructs a <code>TreeBag</code> containing all the members of the
73      * specified collection.
74      *
75      * @param coll the collection to copy into the bag
76      */

77     public TreeBag(Collection JavaDoc coll) {
78         this();
79         addAll(coll);
80     }
81
82     //-----------------------------------------------------------------------
83
public Object JavaDoc first() {
84         return ((SortedMap JavaDoc) getMap()).firstKey();
85     }
86
87     public Object JavaDoc last() {
88         return ((SortedMap JavaDoc) getMap()).lastKey();
89     }
90
91     public Comparator JavaDoc comparator() {
92         return ((SortedMap JavaDoc) getMap()).comparator();
93     }
94
95     //-----------------------------------------------------------------------
96
/**
97      * Write the bag out using a custom routine.
98      */

99     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
100         out.defaultWriteObject();
101         out.writeObject(comparator());
102         super.doWriteObject(out);
103     }
104
105     /**
106      * Read the bag in using a custom routine.
107      */

108     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
109         in.defaultReadObject();
110         Comparator JavaDoc comp = (Comparator JavaDoc) in.readObject();
111         super.doReadObject(new TreeMap JavaDoc(comp), in);
112     }
113     
114 }
115
Popular Tags