KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TransformedSortedMap


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.map;
17
18 import java.util.Comparator JavaDoc;
19 import java.util.SortedMap JavaDoc;
20
21 import org.apache.commons.collections.Transformer;
22
23 /**
24  * Decorates another <code>SortedMap </code> to transform objects that are added.
25  * <p>
26  * The Map put methods and Map.Entry setValue method 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.6 $ $Date: 2004/04/09 10:36:01 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TransformedSortedMap
39         extends TransformedMap
40         implements SortedMap JavaDoc {
41
42     /** Serialization version */
43     private static final long serialVersionUID = -8751771676410385778L;
44     
45     /**
46      * Factory method to create a transforming sorted map.
47      * <p>
48      * If there are any elements already in the map being decorated, they
49      * are NOT transformed.
50      *
51      * @param map the map to decorate, must not be null
52      * @param keyTransformer the predicate to validate the keys, null means no transformation
53      * @param valueTransformer the predicate to validate to values, null means no transformation
54      * @throws IllegalArgumentException if the map is null
55      */

56     public static SortedMap JavaDoc decorate(SortedMap JavaDoc map, Transformer keyTransformer, Transformer valueTransformer) {
57         return new TransformedSortedMap(map, keyTransformer, valueTransformer);
58     }
59
60     //-----------------------------------------------------------------------
61
/**
62      * Constructor that wraps (not copies).
63      * <p>
64      * If there are any elements already in the collection being decorated, they
65      * are NOT transformed.</p>
66      *
67      * @param map the map to decorate, must not be null
68      * @param keyTransformer the predicate to validate the keys, null means no transformation
69      * @param valueTransformer the predicate to validate to values, null means no transformation
70      * @throws IllegalArgumentException if the map is null
71      */

72     protected TransformedSortedMap(SortedMap JavaDoc map, Transformer keyTransformer, Transformer valueTransformer) {
73         super(map, keyTransformer, valueTransformer);
74     }
75
76     //-----------------------------------------------------------------------
77
/**
78      * Gets the map being decorated.
79      *
80      * @return the decorated map
81      */

82     protected SortedMap JavaDoc getSortedMap() {
83         return (SortedMap JavaDoc) map;
84     }
85
86     //-----------------------------------------------------------------------
87
public Object JavaDoc firstKey() {
88         return getSortedMap().firstKey();
89     }
90
91     public Object JavaDoc lastKey() {
92         return getSortedMap().lastKey();
93     }
94
95     public Comparator JavaDoc comparator() {
96         return getSortedMap().comparator();
97     }
98
99     public SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
100         SortedMap JavaDoc map = getSortedMap().subMap(fromKey, toKey);
101         return new TransformedSortedMap(map, keyTransformer, valueTransformer);
102     }
103
104     public SortedMap JavaDoc headMap(Object JavaDoc toKey) {
105         SortedMap JavaDoc map = getSortedMap().headMap(toKey);
106         return new TransformedSortedMap(map, keyTransformer, valueTransformer);
107     }
108
109     public SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
110         SortedMap JavaDoc map = getSortedMap().tailMap(fromKey);
111         return new TransformedSortedMap(map, keyTransformer, valueTransformer);
112     }
113
114 }
115
Popular Tags