001: /*
002: * Copyright 2001-2004 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.Collection;
020: import java.util.Iterator;
021:
022: import org.apache.commons.collections.Transformer;
023:
024: /**
025: * Transformer implementation that chains the specified transformers together.
026: * <p>
027: * The input object is passed to the first transformer. The transformed result
028: * is passed to the second transformer and so on.
029: *
030: * @since Commons Collections 3.0
031: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
032: *
033: * @author Stephen Colebourne
034: */
035: public class ChainedTransformer implements Transformer, Serializable {
036:
037: /** Serial version UID */
038: private static final long serialVersionUID = 3514945074733160196L;
039:
040: /** The transformers to call in turn */
041: private final Transformer[] iTransformers;
042:
043: /**
044: * Factory method that performs validation and copies the parameter array.
045: *
046: * @param transformers the transformers to chain, copied, no nulls
047: * @return the <code>chained</code> transformer
048: * @throws IllegalArgumentException if the transformers array is null
049: * @throws IllegalArgumentException if any transformer in the array is null
050: */
051: public static Transformer getInstance(Transformer[] transformers) {
052: FunctorUtils.validate(transformers);
053: if (transformers.length == 0) {
054: return NOPTransformer.INSTANCE;
055: }
056: transformers = FunctorUtils.copy(transformers);
057: return new ChainedTransformer(transformers);
058: }
059:
060: /**
061: * Create a new Transformer that calls each transformer in turn, passing the
062: * result into the next transformer. The ordering is that of the iterator()
063: * method on the collection.
064: *
065: * @param transformers a collection of transformers to chain
066: * @return the <code>chained</code> transformer
067: * @throws IllegalArgumentException if the transformers collection is null
068: * @throws IllegalArgumentException if any transformer in the collection is null
069: */
070: public static Transformer getInstance(Collection transformers) {
071: if (transformers == null) {
072: throw new IllegalArgumentException(
073: "Transformer collection must not be null");
074: }
075: if (transformers.size() == 0) {
076: return NOPTransformer.INSTANCE;
077: }
078: // convert to array like this to guarantee iterator() ordering
079: Transformer[] cmds = new Transformer[transformers.size()];
080: int i = 0;
081: for (Iterator it = transformers.iterator(); it.hasNext();) {
082: cmds[i++] = (Transformer) it.next();
083: }
084: FunctorUtils.validate(cmds);
085: return new ChainedTransformer(cmds);
086: }
087:
088: /**
089: * Factory method that performs validation.
090: *
091: * @param transformer1 the first transformer, not null
092: * @param transformer2 the second transformer, not null
093: * @return the <code>chained</code> transformer
094: * @throws IllegalArgumentException if either transformer is null
095: */
096: public static Transformer getInstance(Transformer transformer1,
097: Transformer transformer2) {
098: if (transformer1 == null || transformer2 == null) {
099: throw new IllegalArgumentException(
100: "Transformers must not be null");
101: }
102: Transformer[] transformers = new Transformer[] { transformer1,
103: transformer2 };
104: return new ChainedTransformer(transformers);
105: }
106:
107: /**
108: * Constructor that performs no validation.
109: * Use <code>getInstance</code> if you want that.
110: *
111: * @param transformers the transformers to chain, not copied, no nulls
112: */
113: public ChainedTransformer(Transformer[] transformers) {
114: super ();
115: iTransformers = transformers;
116: }
117:
118: /**
119: * Transforms the input to result via each decorated transformer
120: *
121: * @param object the input object passed to the first transformer
122: * @return the transformed result
123: */
124: public Object transform(Object object) {
125: for (int i = 0; i < iTransformers.length; i++) {
126: object = iTransformers[i].transform(object);
127: }
128: return object;
129: }
130:
131: /**
132: * Gets the transformers, do not modify the array.
133: * @return the transformers
134: * @since Commons Collections 3.1
135: */
136: public Transformer[] getTransformers() {
137: return iTransformers;
138: }
139:
140: }
|