KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TransformerUtils


1 /*
2  * Copyright 2002-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;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.commons.collections.functors.ChainedTransformer;
23 import org.apache.commons.collections.functors.CloneTransformer;
24 import org.apache.commons.collections.functors.ClosureTransformer;
25 import org.apache.commons.collections.functors.ConstantTransformer;
26 import org.apache.commons.collections.functors.EqualPredicate;
27 import org.apache.commons.collections.functors.ExceptionTransformer;
28 import org.apache.commons.collections.functors.FactoryTransformer;
29 import org.apache.commons.collections.functors.InstantiateTransformer;
30 import org.apache.commons.collections.functors.InvokerTransformer;
31 import org.apache.commons.collections.functors.MapTransformer;
32 import org.apache.commons.collections.functors.NOPTransformer;
33 import org.apache.commons.collections.functors.PredicateTransformer;
34 import org.apache.commons.collections.functors.StringValueTransformer;
35 import org.apache.commons.collections.functors.SwitchTransformer;
36
37 /**
38  * <code>TransformerUtils</code> provides reference implementations and
39  * utilities for the Transformer functor interface. The supplied transformers are:
40  * <ul>
41  * <li>Invoker - returns the result of a method call on the input object
42  * <li>Clone - returns a clone of the input object
43  * <li>Constant - always returns the same object
44  * <li>Closure - performs a Closure and returns the input object
45  * <li>Predicate - returns the result of the predicate as a Boolean
46  * <li>Factory - returns a new object from a factory
47  * <li>Chained - chains two or more transformers together
48  * <li>Switch - calls one transformer based on one or more predicates
49  * <li>SwitchMap - calls one transformer looked up from a Map
50  * <li>Instantiate - the Class input object is instantiated
51  * <li>Map - returns an object from a supplied Map
52  * <li>Null - always returns null
53  * <li>NOP - returns the input object, which should be immutable
54  * <li>Exception - always throws an exception
55  * <li>StringValue - returns a <code>java.lang.String</code> representation of the input object
56  * </ul>
57  * All the supplied transformers are Serializable.
58  *
59  * @since Commons Collections 3.0
60  * @version $Revision: 1.13 $ $Date: 2004/05/26 21:50:52 $
61  *
62  * @author Stephen Colebourne
63  * @author James Carman
64  */

65 public class TransformerUtils {
66
67     /**
68      * This class is not normally instantiated.
69      */

70     public TransformerUtils() {
71         super();
72     }
73
74     /**
75      * Gets a transformer that always throws an exception.
76      * This could be useful during testing as a placeholder.
77      *
78      * @see org.apache.commons.collections.functors.ExceptionTransformer
79      *
80      * @return the transformer
81      */

82     public static Transformer exceptionTransformer() {
83         return ExceptionTransformer.INSTANCE;
84     }
85
86     /**
87      * Gets a transformer that always returns null.
88      *
89      * @see org.apache.commons.collections.functors.ConstantTransformer
90      *
91      * @return the transformer
92      */

93     public static Transformer nullTransformer() {
94         return ConstantTransformer.NULL_INSTANCE;
95     }
96
97     /**
98      * Gets a transformer that returns the input object.
99      * The input object should be immutable to maintain the
100      * contract of Transformer (although this is not checked).
101      *
102      * @see org.apache.commons.collections.functors.NOPTransformer
103      *
104      * @return the transformer
105      */

106     public static Transformer nopTransformer() {
107         return NOPTransformer.INSTANCE;
108     }
109
110     /**
111      * Gets a transformer that returns a clone of the input
112      * object. The input object will be cloned using one of these
113      * techniques (in order):
114      * <ul>
115      * <li>public clone method
116      * <li>public copy constructor
117      * <li>serialization clone
118      * <ul>
119      *
120      * @see org.apache.commons.collections.functors.CloneTransformer
121      *
122      * @return the transformer
123      */

124     public static Transformer cloneTransformer() {
125         return CloneTransformer.INSTANCE;
126     }
127
128     /**
129      * Creates a Transformer that will return the same object each time the
130      * transformer is used.
131      *
132      * @see org.apache.commons.collections.functors.ConstantTransformer
133      *
134      * @param constantToReturn the constant object to return each time in the transformer
135      * @return the transformer.
136      */

137     public static Transformer constantTransformer(Object JavaDoc constantToReturn) {
138         return ConstantTransformer.getInstance(constantToReturn);
139     }
140
141     /**
142      * Creates a Transformer that calls a Closure each time the transformer is used.
143      * The transformer returns the input object.
144      *
145      * @see org.apache.commons.collections.functors.ClosureTransformer
146      *
147      * @param closure the closure to run each time in the transformer, not null
148      * @return the transformer
149      * @throws IllegalArgumentException if the closure is null
150      */

151     public static Transformer asTransformer(Closure closure) {
152         return ClosureTransformer.getInstance(closure);
153     }
154
155     /**
156      * Creates a Transformer that calls a Predicate each time the transformer is used.
157      * The transformer will return either Boolean.TRUE or Boolean.FALSE.
158      *
159      * @see org.apache.commons.collections.functors.PredicateTransformer
160      *
161      * @param predicate the predicate to run each time in the transformer, not null
162      * @return the transformer
163      * @throws IllegalArgumentException if the predicate is null
164      */

165     public static Transformer asTransformer(Predicate predicate) {
166         return PredicateTransformer.getInstance(predicate);
167     }
168
169     /**
170      * Creates a Transformer that calls a Factory each time the transformer is used.
171      * The transformer will return the value returned by the factory.
172      *
173      * @see org.apache.commons.collections.functors.FactoryTransformer
174      *
175      * @param factory the factory to run each time in the transformer, not null
176      * @return the transformer
177      * @throws IllegalArgumentException if the factory is null
178      */

179     public static Transformer asTransformer(Factory factory) {
180         return FactoryTransformer.getInstance(factory);
181     }
182
183     /**
184      * Create a new Transformer that calls two transformers, passing the result of
185      * the first into the second.
186      *
187      * @see org.apache.commons.collections.functors.ChainedTransformer
188      *
189      * @param transformer1 the first transformer
190      * @param transformer2 the second transformer
191      * @return the transformer
192      * @throws IllegalArgumentException if either transformer is null
193      */

194     public static Transformer chainedTransformer(Transformer transformer1, Transformer transformer2) {
195         return ChainedTransformer.getInstance(transformer1, transformer2);
196     }
197
198     /**
199      * Create a new Transformer that calls each transformer in turn, passing the
200      * result into the next transformer.
201      *
202      * @see org.apache.commons.collections.functors.ChainedTransformer
203      *
204      * @param transformers an array of transformers to chain
205      * @return the transformer
206      * @throws IllegalArgumentException if the transformers array is null
207      * @throws IllegalArgumentException if any transformer in the array is null
208      */

209     public static Transformer chainedTransformer(Transformer[] transformers) {
210         return ChainedTransformer.getInstance(transformers);
211     }
212
213     /**
214      * Create a new Transformer that calls each transformer in turn, passing the
215      * result into the next transformer. The ordering is that of the iterator()
216      * method on the collection.
217      *
218      * @see org.apache.commons.collections.functors.ChainedTransformer
219      *
220      * @param transformers a collection of transformers to chain
221      * @return the transformer
222      * @throws IllegalArgumentException if the transformers collection is null
223      * @throws IllegalArgumentException if any transformer in the collection is null
224      */

225     public static Transformer chainedTransformer(Collection JavaDoc transformers) {
226         return ChainedTransformer.getInstance(transformers);
227     }
228
229     /**
230      * Create a new Transformer that calls one of two transformers depending
231      * on the specified predicate.
232      *
233      * @see org.apache.commons.collections.functors.SwitchTransformer
234      *
235      * @param predicate the predicate to switch on
236      * @param trueTransformer the transformer called if the predicate is true
237      * @param falseTransformer the transformer called if the predicate is false
238      * @return the transformer
239      * @throws IllegalArgumentException if the predicate is null
240      * @throws IllegalArgumentException if either transformer is null
241      */

242     public static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer) {
243         return SwitchTransformer.getInstance(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer);
244     }
245
246     /**
247      * Create a new Transformer that calls one of the transformers depending
248      * on the predicates. The transformer at array location 0 is called if the
249      * predicate at array location 0 returned true. Each predicate is evaluated
250      * until one returns true. If no predicates evaluate to true, null is returned.
251      *
252      * @see org.apache.commons.collections.functors.SwitchTransformer
253      *
254      * @param predicates an array of predicates to check
255      * @param transformers an array of transformers to call
256      * @return the transformer
257      * @throws IllegalArgumentException if the either array is null
258      * @throws IllegalArgumentException if the either array has 0 elements
259      * @throws IllegalArgumentException if any element in the arrays is null
260      * @throws IllegalArgumentException if the arrays are different sizes
261      */

262     public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers) {
263         return SwitchTransformer.getInstance(predicates, transformers, null);
264     }
265
266     /**
267      * Create a new Transformer that calls one of the transformers depending
268      * on the predicates. The transformer at array location 0 is called if the
269      * predicate at array location 0 returned true. Each predicate is evaluated
270      * until one returns true. If no predicates evaluate to true, the default
271      * transformer is called. If the default transformer is null, null is returned.
272      *
273      * @see org.apache.commons.collections.functors.SwitchTransformer
274      *
275      * @param predicates an array of predicates to check
276      * @param transformers an array of transformers to call
277      * @param defaultTransformer the default to call if no predicate matches, null means return null
278      * @return the transformer
279      * @throws IllegalArgumentException if the either array is null
280      * @throws IllegalArgumentException if the either array has 0 elements
281      * @throws IllegalArgumentException if any element in the arrays is null
282      * @throws IllegalArgumentException if the arrays are different sizes
283      */

284     public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
285         return SwitchTransformer.getInstance(predicates, transformers, defaultTransformer);
286     }
287
288     /**
289      * Create a new Transformer that calls one of the transformers depending
290      * on the predicates.
291      * <p>
292      * The Map consists of Predicate keys and Transformer values. A transformer
293      * is called if its matching predicate returns true. Each predicate is evaluated
294      * until one returns true. If no predicates evaluate to true, the default
295      * transformer is called. The default transformer is set in the map with a
296      * null key. If no default transformer is set, null will be returned in a default
297      * case. The ordering is that of the iterator() method on the entryset collection
298      * of the map.
299      *
300      * @see org.apache.commons.collections.functors.SwitchTransformer
301      *
302      * @param predicatesAndTransformers a map of predicates to transformers
303      * @return the transformer
304      * @throws IllegalArgumentException if the map is null
305      * @throws IllegalArgumentException if the map is empty
306      * @throws IllegalArgumentException if any transformer in the map is null
307      * @throws ClassCastException if the map elements are of the wrong type
308      */

309     public static Transformer switchTransformer(Map JavaDoc predicatesAndTransformers) {
310         return SwitchTransformer.getInstance(predicatesAndTransformers);
311     }
312
313     /**
314      * Create a new Transformer that uses the input object as a key to find the
315      * transformer to call.
316      * <p>
317      * The Map consists of object keys and Transformer values. A transformer
318      * is called if the input object equals the key. If there is no match, the
319      * default transformer is called. The default transformer is set in the map
320      * using a null key. If no default is set, null will be returned in a default case.
321      *
322      * @see org.apache.commons.collections.functors.SwitchTransformer
323      *
324      * @param objectsAndTransformers a map of objects to transformers
325      * @return the transformer
326      * @throws IllegalArgumentException if the map is null
327      * @throws IllegalArgumentException if the map is empty
328      * @throws IllegalArgumentException if any transformer in the map is null
329      */

330     public static Transformer switchMapTransformer(Map JavaDoc objectsAndTransformers) {
331         Transformer[] trs = null;
332         Predicate[] preds = null;
333         if (objectsAndTransformers == null) {
334             throw new IllegalArgumentException JavaDoc("The object and transformer map must not be null");
335         }
336         Transformer def = (Transformer) objectsAndTransformers.remove(null);
337         int size = objectsAndTransformers.size();
338         trs = new Transformer[size];
339         preds = new Predicate[size];
340         int i = 0;
341         for (Iterator JavaDoc it = objectsAndTransformers.entrySet().iterator(); it.hasNext();) {
342             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
343             preds[i] = EqualPredicate.getInstance(entry.getKey());
344             trs[i] = (Transformer) entry.getValue();
345             i++;
346         }
347         return switchTransformer(preds, trs, def);
348     }
349
350     /**
351      * Gets a Transformer that expects an input Class object that it will instantiate.
352      *
353      * @see org.apache.commons.collections.functors.InstantiateTransformer
354      *
355      * @return the transformer
356      */

357     public static Transformer instantiateTransformer() {
358         return InstantiateTransformer.NO_ARG_INSTANCE;
359     }
360
361     /**
362      * Creates a Transformer that expects an input Class object that it will
363      * instantiate. The constructor used is determined by the arguments specified
364      * to this method.
365      *
366      * @see org.apache.commons.collections.functors.InstantiateTransformer
367      *
368      * @param paramTypes parameter types for the constructor, can be null
369      * @param args the arguments to pass to the constructor, can be null
370      * @return the transformer
371      * @throws IllegalArgumentException if the paramTypes and args don't match
372      */

373     public static Transformer instantiateTransformer(Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
374         return InstantiateTransformer.getInstance(paramTypes, args);
375     }
376
377     /**
378      * Creates a Transformer that uses the passed in Map to transform the input
379      * object (as a simple lookup).
380      *
381      * @see org.apache.commons.collections.functors.MapTransformer
382      *
383      * @param map the map to use to transform the objects
384      * @return the transformer
385      * @throws IllegalArgumentException if the map is null
386      */

387     public static Transformer mapTransformer(Map JavaDoc map) {
388         return MapTransformer.getInstance(map);
389     }
390
391     /**
392      * Gets a Transformer that invokes a method on the input object.
393      * The method must have no parameters. If the input object is null,
394      * null is returned.
395      * <p>
396      * For example, <code>TransformerUtils.invokerTransformer("getName");</code>
397      * will call the <code>getName/code> method on the input object to
398      * determine the transformer result.
399      *
400      * @see org.apache.commons.collections.functors.InvokerTransformer
401      *
402      * @param methodName the method name to call on the input object, may not be null
403      * @return the transformer
404      * @throws IllegalArgumentException if the methodName is null.
405      */

406     public static Transformer invokerTransformer(String JavaDoc methodName){
407         return InvokerTransformer.getInstance(methodName, null, null);
408     }
409
410     /**
411      * Gets a Transformer that invokes a method on the input object.
412      * The method parameters are specified. If the input object is null,
413      * null is returned.
414      *
415      * @see org.apache.commons.collections.functors.InvokerTransformer
416      *
417      * @param methodName the name of the method
418      * @param paramTypes the parameter types
419      * @param args the arguments
420      * @return the transformer
421      * @throws IllegalArgumentException if the method name is null
422      * @throws IllegalArgumentException if the paramTypes and args don't match
423      */

424     public static Transformer invokerTransformer(String JavaDoc methodName, Class JavaDoc[] paramTypes, Object JavaDoc[] args){
425         return InvokerTransformer.getInstance(methodName, paramTypes, args);
426     }
427
428     /**
429      * Gets a transformer that returns a <code>java.lang.String</code>
430      * representation of the input object. This is achieved via the
431      * <code>toString</code> method, <code>null</code> returns 'null'.
432      *
433      * @see org.apache.commons.collections.functors.StringValueTransformer
434      *
435      * @return the transformer
436      */

437     public static Transformer stringValueTransformer() {
438         return StringValueTransformer.INSTANCE;
439     }
440     
441 }
442
Popular Tags