1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.functors;
18
19 import java.io.Serializable;
20 import java.util.Collection;
21 import java.util.Objects;
22
23 import org.apache.commons.collections4.Transformer;
24
25 /**
26 * Transformer implementation that chains the specified transformers together.
27 * <p>
28 * The input object is passed to the first transformer. The transformed result
29 * is passed to the second transformer and so on.
30 * </p>
31 *
32 * @param <T> the type of the input and result to the function.
33 * @since 3.0
34 */
35 public class ChainedTransformer<T> implements Transformer<T, T>, Serializable {
36
37 @SuppressWarnings("rawtypes")
38 private static final Transformer[] EMPTY_TRANSFORMER_ARRAY = {};
39
40 /** Serial version UID */
41 private static final long serialVersionUID = 3514945074733160196L;
42
43 /**
44 * Create a new Transformer that calls each transformer in turn, passing the
45 * result into the next transformer. The ordering is that of the iterator()
46 * method on the collection.
47 *
48 * @param <T> the object type
49 * @param transformers a collection of transformers to chain
50 * @return the {@code chained} transformer
51 * @throws NullPointerException if the transformers collection is null
52 * @throws NullPointerException if any transformer in the collection is null
53 */
54 public static <T> Transformer<T, T> chainedTransformer(
55 final Collection<? extends Transformer<? super T, ? extends T>> transformers) {
56 Objects.requireNonNull(transformers, "transformers");
57 if (transformers.isEmpty()) {
58 return NOPTransformer.<T>nopTransformer();
59 }
60 // convert to array like this to guarantee iterator() ordering
61 final Transformer<T, T>[] cmds = transformers.toArray(EMPTY_TRANSFORMER_ARRAY);
62 FunctorUtils.validate(cmds);
63 return new ChainedTransformer<>(false, cmds);
64 }
65
66 /**
67 * Factory method that performs validation and copies the parameter array.
68 *
69 * @param <T> the object type
70 * @param transformers the transformers to chain, copied, no nulls
71 * @return the {@code chained} transformer
72 * @throws NullPointerException if the transformers array is null
73 * @throws NullPointerException if any transformer in the array is null
74 */
75 public static <T> Transformer<T, T> chainedTransformer(final Transformer<? super T, ? extends T>... transformers) {
76 FunctorUtils.validate(transformers);
77 if (transformers.length == 0) {
78 return NOPTransformer.<T>nopTransformer();
79 }
80 return new ChainedTransformer<>(transformers);
81 }
82
83 /** The transformers to call in turn */
84 private final Transformer<? super T, ? extends T>[] iTransformers;
85
86 /**
87 * Hidden constructor for the use by the static factory methods.
88 *
89 * @param clone if {@code true} the input argument will be cloned
90 * @param transformers the transformers to chain, no nulls
91 */
92 private ChainedTransformer(final boolean clone, final Transformer<? super T, ? extends T>[] transformers) {
93 iTransformers = clone ? FunctorUtils.copy(transformers) : transformers;
94 }
95
96 /**
97 * Constructor that performs no validation.
98 * Use {@code chainedTransformer} if you want that.
99 *
100 * @param transformers the transformers to chain, copied, no nulls
101 */
102 public ChainedTransformer(final Transformer<? super T, ? extends T>... transformers) {
103 this(true, transformers);
104 }
105
106 /**
107 * Gets the transformers.
108 *
109 * @return a copy of the transformers
110 * @since 3.1
111 */
112 public Transformer<? super T, ? extends T>[] getTransformers() {
113 return FunctorUtils.copy(iTransformers);
114 }
115
116 /**
117 * Transforms the input to result via each decorated transformer
118 *
119 * @param object the input object passed to the first transformer
120 * @return the transformed result
121 */
122 @Override
123 public T transform(T object) {
124 for (final Transformer<? super T, ? extends T> iTransformer : iTransformers) {
125 object = iTransformer.apply(object);
126 }
127 return object;
128 }
129
130 }