KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > TransformedSortedSet


1 /*
2  * Copyright 2003-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.set;
17
18 import java.util.Comparator JavaDoc;
19 import java.util.SortedSet JavaDoc;
20
21 import org.apache.commons.collections.Transformer;
22
23 /**
24  * Decorates another <code>SortedSet</code> to transform objects that are added.
25  * <p>
26  * The add methods are affected by this class.
27  * Thus objects must be removed or searched for using their transformed form.
28  * For example, if the transformation converts Strings to Integers, you must
29  * use the Integer form to remove objects.
30  * <p>
31  * This class is Serializable from Commons Collections 3.1.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TransformedSortedSet extends TransformedSet implements SortedSet JavaDoc {
39
40     /** Serialization version */
41     private static final long serialVersionUID = -1675486811351124386L;
42
43     /**
44      * Factory method to create a transforming sorted set.
45      * <p>
46      * If there are any elements already in the set being decorated, they
47      * are NOT transformed.
48      *
49      * @param set the set to decorate, must not be null
50      * @param transformer the transformer to use for conversion, must not be null
51      * @throws IllegalArgumentException if set or transformer is null
52      */

53     public static SortedSet JavaDoc decorate(SortedSet JavaDoc set, Transformer transformer) {
54         return new TransformedSortedSet(set, transformer);
55     }
56     
57     //-----------------------------------------------------------------------
58
/**
59      * Constructor that wraps (not copies).
60      * <p>
61      * If there are any elements already in the set being decorated, they
62      * are NOT transformed.
63      *
64      * @param set the set to decorate, must not be null
65      * @param transformer the transformer to use for conversion, must not be null
66      * @throws IllegalArgumentException if set or transformer is null
67      */

68     protected TransformedSortedSet(SortedSet JavaDoc set, Transformer transformer) {
69         super(set, transformer);
70     }
71
72     /**
73      * Gets the decorated set.
74      *
75      * @return the decorated set
76      */

77     protected SortedSet JavaDoc getSortedSet() {
78         return (SortedSet JavaDoc) collection;
79     }
80
81     //-----------------------------------------------------------------------
82
public Object JavaDoc first() {
83         return getSortedSet().first();
84     }
85
86     public Object JavaDoc last() {
87         return getSortedSet().last();
88     }
89
90     public Comparator JavaDoc comparator() {
91         return getSortedSet().comparator();
92     }
93
94     //-----------------------------------------------------------------------
95
public SortedSet JavaDoc subSet(Object JavaDoc fromElement, Object JavaDoc toElement) {
96         SortedSet JavaDoc set = getSortedSet().subSet(fromElement, toElement);
97         return new TransformedSortedSet(set, transformer);
98     }
99
100     public SortedSet JavaDoc headSet(Object JavaDoc toElement) {
101         SortedSet JavaDoc set = getSortedSet().headSet(toElement);
102         return new TransformedSortedSet(set, transformer);
103     }
104
105     public SortedSet JavaDoc tailSet(Object JavaDoc fromElement) {
106         SortedSet JavaDoc set = getSortedSet().tailSet(fromElement);
107         return new TransformedSortedSet(set, transformer);
108     }
109
110 }
111
Popular Tags