KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.collections.Transformer;
26
27 /**
28  * Decorates another <code>Map</code> to transform objects that are added.
29  * <p>
30  * The Map put methods and Map.Entry setValue method are affected by this class.
31  * Thus objects must be removed or searched for using their transformed form.
32  * For example, if the transformation converts Strings to Integers, you must
33  * use the Integer form to remove objects.
34  * <p>
35  * This class is Serializable from Commons Collections 3.1.
36  *
37  * @since Commons Collections 3.0
38  * @version $Revision: 1.11 $ $Date: 2004/06/07 22:14:42 $
39  *
40  * @author Stephen Colebourne
41  */

42 public class TransformedMap
43         extends AbstractInputCheckedMapDecorator
44         implements Serializable JavaDoc {
45
46     /** Serialization version */
47     private static final long serialVersionUID = 7023152376788900464L;
48
49     /** The transformer to use for the key */
50     protected final Transformer keyTransformer;
51     /** The transformer to use for the value */
52     protected final Transformer valueTransformer;
53
54     /**
55      * Factory method to create a transforming map.
56      * <p>
57      * If there are any elements already in the map being decorated, they
58      * are NOT transformed.
59      *
60      * @param map the map to decorate, must not be null
61      * @param keyTransformer the transformer to use for key conversion, null means no conversion
62      * @param valueTransformer the transformer to use for value conversion, null means no conversion
63      * @throws IllegalArgumentException if map is null
64      */

65     public static Map JavaDoc decorate(Map JavaDoc map, Transformer keyTransformer, Transformer valueTransformer) {
66         return new TransformedMap(map, keyTransformer, valueTransformer);
67     }
68
69     //-----------------------------------------------------------------------
70
/**
71      * Constructor that wraps (not copies).
72      * <p>
73      * If there are any elements already in the collection being decorated, they
74      * are NOT transformed.
75      *
76      * @param map the map to decorate, must not be null
77      * @param keyTransformer the transformer to use for key conversion, null means no conversion
78      * @param valueTransformer the transformer to use for value conversion, null means no conversion
79      * @throws IllegalArgumentException if map is null
80      */

81     protected TransformedMap(Map JavaDoc map, Transformer keyTransformer, Transformer valueTransformer) {
82         super(map);
83         this.keyTransformer = keyTransformer;
84         this.valueTransformer = valueTransformer;
85     }
86
87     //-----------------------------------------------------------------------
88
/**
89      * Write the map out using a custom routine.
90      *
91      * @param out the output stream
92      * @throws IOException
93      * @since Commons Collections 3.1
94      */

95     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
96         out.defaultWriteObject();
97         out.writeObject(map);
98     }
99
100     /**
101      * Read the map in using a custom routine.
102      *
103      * @param in the input stream
104      * @throws IOException
105      * @throws ClassNotFoundException
106      * @since Commons Collections 3.1
107      */

108     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
109         in.defaultReadObject();
110         map = (Map JavaDoc) in.readObject();
111     }
112
113     //-----------------------------------------------------------------------
114
/**
115      * Transforms a key.
116      * <p>
117      * The transformer itself may throw an exception if necessary.
118      *
119      * @param object the object to transform
120      * @throws the transformed object
121      */

122     protected Object JavaDoc transformKey(Object JavaDoc object) {
123         if (keyTransformer == null) {
124             return object;
125         }
126         return keyTransformer.transform(object);
127     }
128
129     /**
130      * Transforms a value.
131      * <p>
132      * The transformer itself may throw an exception if necessary.
133      *
134      * @param object the object to transform
135      * @throws the transformed object
136      */

137     protected Object JavaDoc transformValue(Object JavaDoc object) {
138         if (valueTransformer == null) {
139             return object;
140         }
141         return valueTransformer.transform(object);
142     }
143
144     /**
145      * Transforms a map.
146      * <p>
147      * The transformer itself may throw an exception if necessary.
148      *
149      * @param map the map to transform
150      * @throws the transformed object
151      */

152     protected Map JavaDoc transformMap(Map JavaDoc map) {
153         Map JavaDoc result = new LinkedMap(map.size());
154         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext(); ) {
155             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
156             result.put(transformKey(entry.getKey()), transformValue(entry.getValue()));
157         }
158         return result;
159     }
160
161     /**
162      * Override to transform the value when using <code>setValue</code>.
163      *
164      * @param value the value to transform
165      * @return the transformed value
166      * @since Commons Collections 3.1
167      */

168     protected Object JavaDoc checkSetValue(Object JavaDoc value) {
169         return valueTransformer.transform(value);
170     }
171
172     /**
173      * Override to only return true when there is a value transformer.
174      *
175      * @return true if a value transformer is in use
176      * @since Commons Collections 3.1
177      */

178     protected boolean isSetValueChecking() {
179         return (valueTransformer != null);
180     }
181
182     //-----------------------------------------------------------------------
183
public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
184         key = transformKey(key);
185         value = transformValue(value);
186         return getMap().put(key, value);
187     }
188
189     public void putAll(Map JavaDoc mapToCopy) {
190         mapToCopy = transformMap(mapToCopy);
191         getMap().putAll(mapToCopy);
192     }
193
194 }
195
Popular Tags