001: /*
002: * Copyright 2002-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bag;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.Comparator;
024: import java.util.SortedMap;
025: import java.util.TreeMap;
026:
027: import org.apache.commons.collections.SortedBag;
028:
029: /**
030: * Implements <code>SortedBag</code>, using a <code>TreeMap</code> to provide
031: * the data storage. This is the standard implementation of a sorted bag.
032: * <p>
033: * Order will be maintained among the bag members and can be viewed through the
034: * iterator.
035: * <p>
036: * A <code>Bag</code> stores each object in the collection together with a
037: * count of occurrences. Extra methods on the interface allow multiple copies
038: * of an object to be added or removed at once. It is important to read the
039: * interface javadoc carefully as several methods violate the
040: * <code>Collection</code> interface specification.
041: *
042: * @since Commons Collections 3.0 (previously in main package v2.0)
043: * @version $Revision: 348299 $ $Date: 2005-11-22 23:51:45 +0000 (Tue, 22 Nov 2005) $
044: *
045: * @author Chuck Burdick
046: * @author Stephen Colebourne
047: */
048: public class TreeBag extends AbstractMapBag implements SortedBag,
049: Serializable {
050:
051: /** Serial version lock */
052: private static final long serialVersionUID = -7740146511091606676L;
053:
054: /**
055: * Constructs an empty <code>TreeBag</code>.
056: */
057: public TreeBag() {
058: super (new TreeMap());
059: }
060:
061: /**
062: * Constructs an empty bag that maintains order on its unique
063: * representative members according to the given {@link Comparator}.
064: *
065: * @param comparator the comparator to use
066: */
067: public TreeBag(Comparator comparator) {
068: super (new TreeMap(comparator));
069: }
070:
071: /**
072: * Constructs a <code>TreeBag</code> containing all the members of the
073: * specified collection.
074: *
075: * @param coll the collection to copy into the bag
076: */
077: public TreeBag(Collection coll) {
078: this ();
079: addAll(coll);
080: }
081:
082: //-----------------------------------------------------------------------
083: public Object first() {
084: return ((SortedMap) getMap()).firstKey();
085: }
086:
087: public Object last() {
088: return ((SortedMap) getMap()).lastKey();
089: }
090:
091: public Comparator comparator() {
092: return ((SortedMap) getMap()).comparator();
093: }
094:
095: //-----------------------------------------------------------------------
096: /**
097: * Write the bag out using a custom routine.
098: */
099: private void writeObject(ObjectOutputStream out) throws IOException {
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 in) throws IOException,
109: ClassNotFoundException {
110: in.defaultReadObject();
111: Comparator comp = (Comparator) in.readObject();
112: super .doReadObject(new TreeMap(comp), in);
113: }
114:
115: }
|