001: /*
002: * Copyright 2001-2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.functors;
017:
018: import java.io.Serializable;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.apache.commons.collections.Predicate;
023: import org.apache.commons.collections.Transformer;
024:
025: /**
026: * Transformer implementation calls the transformer whose predicate returns true,
027: * like a switch statement.
028: *
029: * @since Commons Collections 3.0
030: * @version $Revision: 393105 $ $Date: 2006-04-10 23:23:11 +0100 (Mon, 10 Apr 2006) $
031: *
032: * @author Stephen Colebourne
033: */
034: public class SwitchTransformer implements Transformer, Serializable {
035:
036: /** Serial version UID */
037: private static final long serialVersionUID = -6404460890903469332L;
038:
039: /** The tests to consider */
040: private final Predicate[] iPredicates;
041: /** The matching transformers to call */
042: private final Transformer[] iTransformers;
043: /** The default transformer to call if no tests match */
044: private final Transformer iDefault;
045:
046: /**
047: * Factory method that performs validation and copies the parameter arrays.
048: *
049: * @param predicates array of predicates, cloned, no nulls
050: * @param transformers matching array of transformers, cloned, no nulls
051: * @param defaultTransformer the transformer to use if no match, null means return null
052: * @return the <code>chained</code> transformer
053: * @throws IllegalArgumentException if array is null
054: * @throws IllegalArgumentException if any element in the array is null
055: */
056: public static Transformer getInstance(Predicate[] predicates,
057: Transformer[] transformers, Transformer defaultTransformer) {
058: FunctorUtils.validate(predicates);
059: FunctorUtils.validate(transformers);
060: if (predicates.length != transformers.length) {
061: throw new IllegalArgumentException(
062: "The predicate and transformer arrays must be the same size");
063: }
064: if (predicates.length == 0) {
065: return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE
066: : defaultTransformer);
067: }
068: predicates = FunctorUtils.copy(predicates);
069: transformers = FunctorUtils.copy(transformers);
070: return new SwitchTransformer(predicates, transformers,
071: defaultTransformer);
072: }
073:
074: /**
075: * Create a new Transformer that calls one of the transformers depending
076: * on the predicates.
077: * <p>
078: * The Map consists of Predicate keys and Transformer values. A transformer
079: * is called if its matching predicate returns true. Each predicate is evaluated
080: * until one returns true. If no predicates evaluate to true, the default
081: * transformer is called. The default transformer is set in the map with a
082: * null key. The ordering is that of the iterator() method on the entryset
083: * collection of the map.
084: *
085: * @param predicatesAndTransformers a map of predicates to transformers
086: * @return the <code>switch</code> transformer
087: * @throws IllegalArgumentException if the map is null
088: * @throws IllegalArgumentException if any transformer in the map is null
089: * @throws ClassCastException if the map elements are of the wrong type
090: */
091: public static Transformer getInstance(Map predicatesAndTransformers) {
092: Transformer[] transformers = null;
093: Predicate[] preds = null;
094: if (predicatesAndTransformers == null) {
095: throw new IllegalArgumentException(
096: "The predicate and transformer map must not be null");
097: }
098: if (predicatesAndTransformers.size() == 0) {
099: return ConstantTransformer.NULL_INSTANCE;
100: }
101: // convert to array like this to guarantee iterator() ordering
102: Transformer defaultTransformer = (Transformer) predicatesAndTransformers
103: .remove(null);
104: int size = predicatesAndTransformers.size();
105: if (size == 0) {
106: return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE
107: : defaultTransformer);
108: }
109: transformers = new Transformer[size];
110: preds = new Predicate[size];
111: int i = 0;
112: for (Iterator it = predicatesAndTransformers.entrySet()
113: .iterator(); it.hasNext();) {
114: Map.Entry entry = (Map.Entry) it.next();
115: preds[i] = (Predicate) entry.getKey();
116: transformers[i] = (Transformer) entry.getValue();
117: i++;
118: }
119: return new SwitchTransformer(preds, transformers,
120: defaultTransformer);
121: }
122:
123: /**
124: * Constructor that performs no validation.
125: * Use <code>getInstance</code> if you want that.
126: *
127: * @param predicates array of predicates, not cloned, no nulls
128: * @param transformers matching array of transformers, not cloned, no nulls
129: * @param defaultTransformer the transformer to use if no match, null means return null
130: */
131: public SwitchTransformer(Predicate[] predicates,
132: Transformer[] transformers, Transformer defaultTransformer) {
133: super ();
134: iPredicates = predicates;
135: iTransformers = transformers;
136: iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE
137: : defaultTransformer);
138: }
139:
140: /**
141: * Transforms the input to result by calling the transformer whose matching
142: * predicate returns true.
143: *
144: * @param input the input object to transform
145: * @return the transformed result
146: */
147: public Object transform(Object input) {
148: for (int i = 0; i < iPredicates.length; i++) {
149: if (iPredicates[i].evaluate(input) == true) {
150: return iTransformers[i].transform(input);
151: }
152: }
153: return iDefault.transform(input);
154: }
155:
156: /**
157: * Gets the predicates, do not modify the array.
158: *
159: * @return the predicates
160: * @since Commons Collections 3.1
161: */
162: public Predicate[] getPredicates() {
163: return iPredicates;
164: }
165:
166: /**
167: * Gets the transformers, do not modify the array.
168: *
169: * @return the transformers
170: * @since Commons Collections 3.1
171: */
172: public Transformer[] getTransformers() {
173: return iTransformers;
174: }
175:
176: /**
177: * Gets the default transformer.
178: *
179: * @return the default transformer
180: * @since Commons Collections 3.1
181: */
182: public Transformer getDefaultTransformer() {
183: return iDefault;
184: }
185:
186: }
|