KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > ChainedTransformer


1 /*
2  * Copyright 2001-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.functors;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.commons.collections.Transformer;
23
24 /**
25  * Transformer implementation that chains the specified transformers together.
26  * <p>
27  * The input object is passed to the first transformer. The transformed result
28  * is passed to the second transformer and so on.
29  *
30  * @since Commons Collections 3.0
31  * @version $Revision: 1.7 $ $Date: 2004/05/16 11:36:31 $
32  *
33  * @author Stephen Colebourne
34  */

35 public class ChainedTransformer implements Transformer, Serializable JavaDoc {
36
37     /** Serial version UID */
38     static final long serialVersionUID = 3514945074733160196L;
39
40     /** The transformers to call in turn */
41     private final Transformer[] iTransformers;
42
43     /**
44      * Factory method that performs validation and copies the parameter array.
45      *
46      * @param transformers the transformers to chain, copied, no nulls
47      * @return the <code>chained</code> transformer
48      * @throws IllegalArgumentException if the transformers array is null
49      * @throws IllegalArgumentException if any transformer in the array is null
50      */

51     public static Transformer getInstance(Transformer[] transformers) {
52         FunctorUtils.validate(transformers);
53         if (transformers.length == 0) {
54             return NOPTransformer.INSTANCE;
55         }
56         transformers = FunctorUtils.copy(transformers);
57         return new ChainedTransformer(transformers);
58     }
59     
60     /**
61      * Create a new Transformer that calls each transformer in turn, passing the
62      * result into the next transformer. The ordering is that of the iterator()
63      * method on the collection.
64      *
65      * @param transformers a collection of transformers to chain
66      * @return the <code>chained</code> transformer
67      * @throws IllegalArgumentException if the transformers collection is null
68      * @throws IllegalArgumentException if any transformer in the collection is null
69      */

70     public static Transformer getInstance(Collection JavaDoc transformers) {
71         if (transformers == null) {
72             throw new IllegalArgumentException JavaDoc("Transformer collection must not be null");
73         }
74         if (transformers.size() == 0) {
75             return NOPTransformer.INSTANCE;
76         }
77         // convert to array like this to guarantee iterator() ordering
78
Transformer[] cmds = new Transformer[transformers.size()];
79         int i = 0;
80         for (Iterator JavaDoc it = transformers.iterator(); it.hasNext();) {
81             cmds[i++] = (Transformer) it.next();
82         }
83         FunctorUtils.validate(cmds);
84         return new ChainedTransformer(cmds);
85     }
86
87     /**
88      * Factory method that performs validation.
89      *
90      * @param transformer1 the first transformer, not null
91      * @param transformer2 the second transformer, not null
92      * @return the <code>chained</code> transformer
93      * @throws IllegalArgumentException if either transformer is null
94      */

95     public static Transformer getInstance(Transformer transformer1, Transformer transformer2) {
96         if (transformer1 == null || transformer2 == null) {
97             throw new IllegalArgumentException JavaDoc("Transformers must not be null");
98         }
99         Transformer[] transformers = new Transformer[] { transformer1, transformer2 };
100         return new ChainedTransformer(transformers);
101     }
102
103     /**
104      * Constructor that performs no validation.
105      * Use <code>getInstance</code> if you want that.
106      *
107      * @param transformers the transformers to chain, not copied, no nulls
108      */

109     public ChainedTransformer(Transformer[] transformers) {
110         super();
111         iTransformers = transformers;
112     }
113
114     /**
115      * Transforms the input to result via each decorated transformer
116      *
117      * @param object the input object passed to the first transformer
118      * @return the transformed result
119      */

120     public Object JavaDoc transform(Object JavaDoc object) {
121         for (int i = 0; i < iTransformers.length; i++) {
122             object = iTransformers[i].transform(object);
123         }
124         return object;
125     }
126
127     /**
128      * Gets the transformers, do not modify the array.
129      * @return the transformers
130      * @since Commons Collections 3.1
131      */

132     public Transformer[] getTransformers() {
133         return iTransformers;
134     }
135
136 }
137
Popular Tags