KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.commons.collections.Predicate;
23 import org.apache.commons.collections.Transformer;
24
25 /**
26  * Transformer implementation calls the transformer whose predicate returns true,
27  * like a switch statement.
28  *
29  * @since Commons Collections 3.0
30  * @version $Revision: 1.5 $ $Date: 2004/05/16 11:36:31 $
31  *
32  * @author Stephen Colebourne
33  */

34 public class SwitchTransformer implements Transformer, Serializable JavaDoc {
35
36     /** Serial version UID */
37     static final long serialVersionUID = -6404460890903469332L;
38
39     /** The tests to consider */
40     private final Predicate[] iPredicates;
41     /** The matching transformers to call */
42     private final Transformer[] iTransformers;
43     /** The default transformer to call if no tests match */
44     private final Transformer iDefault;
45
46     /**
47      * Factory method that performs validation and copies the parameter arrays.
48      *
49      * @param predicates array of predicates, cloned, no nulls
50      * @param transformers matching array of transformers, cloned, no nulls
51      * @param defaultTransformer the transformer to use if no match, null means nop
52      * @return the <code>chained</code> transformer
53      * @throws IllegalArgumentException if array is null
54      * @throws IllegalArgumentException if any element in the array is null
55      */

56     public static Transformer getInstance(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
57         FunctorUtils.validate(predicates);
58         FunctorUtils.validate(transformers);
59         if (predicates.length != transformers.length) {
60             throw new IllegalArgumentException JavaDoc("The predicate and transformer arrays must be the same size");
61         }
62         if (predicates.length == 0) {
63             return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
64         }
65         predicates = FunctorUtils.copy(predicates);
66         transformers = FunctorUtils.copy(transformers);
67         return new SwitchTransformer(predicates, transformers, defaultTransformer);
68     }
69
70     /**
71      * Create a new Transformer that calls one of the transformers depending
72      * on the predicates.
73      * <p>
74      * The Map consists of Predicate keys and Transformer values. A transformer
75      * is called if its matching predicate returns true. Each predicate is evaluated
76      * until one returns true. If no predicates evaluate to true, the default
77      * transformer is called. The default transformer is set in the map with a
78      * null key. The ordering is that of the iterator() method on the entryset
79      * collection of the map.
80      *
81      * @param predicatesAndTransformers a map of predicates to transformers
82      * @return the <code>switch</code> transformer
83      * @throws IllegalArgumentException if the map is null
84      * @throws IllegalArgumentException if any transformer in the map is null
85      * @throws ClassCastException if the map elements are of the wrong type
86      */

87     public static Transformer getInstance(Map JavaDoc predicatesAndTransformers) {
88         Transformer[] transformers = null;
89         Predicate[] preds = null;
90         if (predicatesAndTransformers == null) {
91             throw new IllegalArgumentException JavaDoc("The predicate and transformer map must not be null");
92         }
93         if (predicatesAndTransformers.size() == 0) {
94             return ConstantTransformer.NULL_INSTANCE;
95         }
96         // convert to array like this to guarantee iterator() ordering
97
Transformer defaultTransformer = (Transformer) predicatesAndTransformers.remove(null);
98         int size = predicatesAndTransformers.size();
99         if (size == 0) {
100             return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
101         }
102         transformers = new Transformer[size];
103         preds = new Predicate[size];
104         int i = 0;
105         for (Iterator JavaDoc it = predicatesAndTransformers.entrySet().iterator(); it.hasNext();) {
106             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
107             preds[i] = (Predicate) entry.getKey();
108             transformers[i] = (Transformer) entry.getValue();
109             i++;
110         }
111         return new SwitchTransformer(preds, transformers, defaultTransformer);
112     }
113     
114     /**
115      * Constructor that performs no validation.
116      * Use <code>getInstance</code> if you want that.
117      *
118      * @param predicates array of predicates, not cloned, no nulls
119      * @param transformers matching array of transformers, not cloned, no nulls
120      * @param defaultTransformer the transformer to use if no match, null means nop
121      */

122     public SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
123         super();
124         iPredicates = predicates;
125         iTransformers = transformers;
126         iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
127     }
128
129     /**
130      * Transforms the input to result by calling the transformer whose matching
131      * predicate returns true.
132      *
133      * @param input the input object to transform
134      * @return the transformed result
135      */

136     public Object JavaDoc transform(Object JavaDoc input) {
137         for (int i = 0; i < iPredicates.length; i++) {
138             if (iPredicates[i].evaluate(input) == true) {
139                 return iTransformers[i].transform(input);
140             }
141         }
142         return iDefault.transform(input);
143     }
144
145     /**
146      * Gets the predicates, do not modify the array.
147      *
148      * @return the predicates
149      * @since Commons Collections 3.1
150      */

151     public Predicate[] getPredicates() {
152         return iPredicates;
153     }
154
155     /**
156      * Gets the transformers, do not modify the array.
157      *
158      * @return the transformers
159      * @since Commons Collections 3.1
160      */

161     public Transformer[] getTransformers() {
162         return iTransformers;
163     }
164
165     /**
166      * Gets the default transformer.
167      *
168      * @return the default transformer
169      * @since Commons Collections 3.1
170      */

171     public Transformer getDefaultTransformer() {
172         return iDefault;
173     }
174
175 }
176
Popular Tags