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;
018
019 import java.util.Collection;
020 import java.util.Comparator;
021 import java.util.SortedMap;
022 import java.util.TreeMap;
023
024 /**
025 * A {@link Bag} that is backed by a {@link TreeMap}.
026 * Order will be maintained among the unique representative
027 * members.
028 *
029 * @deprecated Moved to bag subpackage and rewritten internally. Due to be removed in v4.0.
030 * @since Commons Collections 2.0
031 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
032 *
033 * @author Chuck Burdick
034 */
035 public class TreeBag extends DefaultMapBag implements SortedBag {
036
037 /**
038 * Constructs an empty <code>TreeBag</code>.
039 */
040 public TreeBag() {
041 super(new TreeMap());
042 }
043
044 /**
045 * Constructs an empty {@link Bag} that maintains order on its unique
046 * representative members according to the given {@link Comparator}.
047 *
048 * @param comparator the comparator to use
049 */
050 public TreeBag(Comparator comparator) {
051 super(new TreeMap(comparator));
052 }
053
054 /**
055 * Constructs a {@link Bag} containing all the members of the given
056 * collection.
057 *
058 * @param coll the collection to copy into the bag
059 */
060 public TreeBag(Collection coll) {
061 this();
062 addAll(coll);
063 }
064
065 public Object first() {
066 return ((SortedMap) getMap()).firstKey();
067 }
068
069 public Object last() {
070 return ((SortedMap) getMap()).lastKey();
071 }
072
073 public Comparator comparator() {
074 return ((SortedMap) getMap()).comparator();
075 }
076
077 }