001: /*
002: * Copyright 2003-2004 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.set;
017:
018: import java.util.Comparator;
019: import java.util.SortedSet;
020:
021: import org.apache.commons.collections.Transformer;
022:
023: /**
024: * Decorates another <code>SortedSet</code> to transform objects that are added.
025: * <p>
026: * The add methods are affected by this class.
027: * Thus objects must be removed or searched for using their transformed form.
028: * For example, if the transformation converts Strings to Integers, you must
029: * use the Integer form to remove objects.
030: * <p>
031: * This class is Serializable from Commons Collections 3.1.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035: *
036: * @author Stephen Colebourne
037: */
038: public class TransformedSortedSet extends TransformedSet implements
039: SortedSet {
040:
041: /** Serialization version */
042: private static final long serialVersionUID = -1675486811351124386L;
043:
044: /**
045: * Factory method to create a transforming sorted set.
046: * <p>
047: * If there are any elements already in the set being decorated, they
048: * are NOT transformed.
049: *
050: * @param set the set to decorate, must not be null
051: * @param transformer the transformer to use for conversion, must not be null
052: * @throws IllegalArgumentException if set or transformer is null
053: */
054: public static SortedSet decorate(SortedSet set,
055: Transformer transformer) {
056: return new TransformedSortedSet(set, transformer);
057: }
058:
059: //-----------------------------------------------------------------------
060: /**
061: * Constructor that wraps (not copies).
062: * <p>
063: * If there are any elements already in the set being decorated, they
064: * are NOT transformed.
065: *
066: * @param set the set to decorate, must not be null
067: * @param transformer the transformer to use for conversion, must not be null
068: * @throws IllegalArgumentException if set or transformer is null
069: */
070: protected TransformedSortedSet(SortedSet set,
071: Transformer transformer) {
072: super (set, transformer);
073: }
074:
075: /**
076: * Gets the decorated set.
077: *
078: * @return the decorated set
079: */
080: protected SortedSet getSortedSet() {
081: return (SortedSet) collection;
082: }
083:
084: //-----------------------------------------------------------------------
085: public Object first() {
086: return getSortedSet().first();
087: }
088:
089: public Object last() {
090: return getSortedSet().last();
091: }
092:
093: public Comparator comparator() {
094: return getSortedSet().comparator();
095: }
096:
097: //-----------------------------------------------------------------------
098: public SortedSet subSet(Object fromElement, Object toElement) {
099: SortedSet set = getSortedSet().subSet(fromElement, toElement);
100: return new TransformedSortedSet(set, transformer);
101: }
102:
103: public SortedSet headSet(Object toElement) {
104: SortedSet set = getSortedSet().headSet(toElement);
105: return new TransformedSortedSet(set, transformer);
106: }
107:
108: public SortedSet tailSet(Object fromElement) {
109: SortedSet set = getSortedSet().tailSet(fromElement);
110: return new TransformedSortedSet(set, transformer);
111: }
112:
113: }
|