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.Comparator; 025 import java.util.SortedMap; 026 import java.util.TreeMap; 027 028 import org.apache.commons.collections.SortedBag; 029 030 /** 031 * Implements <code>SortedBag</code>, using a <code>TreeMap</code> to provide 032 * the data storage. This is the standard implementation of a sorted bag. 033 * <p> 034 * Order will be maintained among the bag members and can be viewed through the 035 * iterator. 036 * <p> 037 * A <code>Bag</code> stores each object in the collection together with a 038 * count of occurrences. Extra methods on the interface allow multiple copies 039 * of an object to be added or removed at once. It is important to read the 040 * interface javadoc carefully as several methods violate the 041 * <code>Collection</code> interface specification. 042 * 043 * @since Commons Collections 3.0 (previously in main package v2.0) 044 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 045 * 046 * @author Chuck Burdick 047 * @author Stephen Colebourne 048 */ 049 public class TreeBag 050 extends AbstractMapBag implements SortedBag, Serializable { 051 052 /** Serial version lock */ 053 private static final long serialVersionUID = -7740146511091606676L; 054 055 /** 056 * Constructs an empty <code>TreeBag</code>. 057 */ 058 public TreeBag() { 059 super(new TreeMap()); 060 } 061 062 /** 063 * Constructs an empty bag that maintains order on its unique 064 * representative members according to the given {@link Comparator}. 065 * 066 * @param comparator the comparator to use 067 */ 068 public TreeBag(Comparator comparator) { 069 super(new TreeMap(comparator)); 070 } 071 072 /** 073 * Constructs a <code>TreeBag</code> containing all the members of the 074 * specified collection. 075 * 076 * @param coll the collection to copy into the bag 077 */ 078 public TreeBag(Collection coll) { 079 this(); 080 addAll(coll); 081 } 082 083 //----------------------------------------------------------------------- 084 public Object first() { 085 return ((SortedMap) getMap()).firstKey(); 086 } 087 088 public Object last() { 089 return ((SortedMap) getMap()).lastKey(); 090 } 091 092 public Comparator comparator() { 093 return ((SortedMap) getMap()).comparator(); 094 } 095 096 //----------------------------------------------------------------------- 097 /** 098 * Write the bag out using a custom routine. 099 */ 100 private void writeObject(ObjectOutputStream out) throws IOException { 101 out.defaultWriteObject(); 102 out.writeObject(comparator()); 103 super.doWriteObject(out); 104 } 105 106 /** 107 * Read the bag in using a custom routine. 108 */ 109 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 110 in.defaultReadObject(); 111 Comparator comp = (Comparator) in.readObject(); 112 super.doReadObject(new TreeMap(comp), in); 113 } 114 115 }