View Javadoc
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   * @since 3.0
33   */
34  public class ChainedTransformer<T> implements Transformer<T, T>, Serializable {
35  
36      private static final Transformer[] EMPTY_TRANSFORMER_ARRAY = {};
37  
38      /** Serial version UID */
39      private static final long serialVersionUID = 3514945074733160196L;
40  
41      /**
42       * Create a new Transformer that calls each transformer in turn, passing the
43       * result into the next transformer. The ordering is that of the iterator()
44       * method on the collection.
45       *
46       * @param <T>  the object type
47       * @param transformers  a collection of transformers to chain
48       * @return the {@code chained} transformer
49       * @throws NullPointerException if the transformers collection is null
50       * @throws NullPointerException if any transformer in the collection is null
51       */
52      public static <T> Transformer<T, T> chainedTransformer(
53              final Collection<? extends Transformer<? super T, ? extends T>> transformers) {
54          Objects.requireNonNull(transformers, "transformers");
55          if (transformers.isEmpty()) {
56              return NOPTransformer.<T>nopTransformer();
57          }
58          // convert to array like this to guarantee iterator() ordering
59          final Transformer<T, T>[] cmds = transformers.toArray(EMPTY_TRANSFORMER_ARRAY);
60          FunctorUtils.validate(cmds);
61          return new ChainedTransformer<>(false, cmds);
62      }
63  
64      /**
65       * Factory method that performs validation and copies the parameter array.
66       *
67       * @param <T>  the object type
68       * @param transformers  the transformers to chain, copied, no nulls
69       * @return the {@code chained} transformer
70       * @throws NullPointerException if the transformers array is null
71       * @throws NullPointerException if any transformer in the array is null
72       */
73      public static <T> Transformer<T, T> chainedTransformer(final Transformer<? super T, ? extends T>... transformers) {
74          FunctorUtils.validate(transformers);
75          if (transformers.length == 0) {
76              return NOPTransformer.<T>nopTransformer();
77          }
78          return new ChainedTransformer<>(transformers);
79      }
80  
81      /** The transformers to call in turn */
82      private final Transformer<? super T, ? extends T>[] iTransformers;
83  
84      /**
85       * Hidden constructor for the use by the static factory methods.
86       *
87       * @param clone  if {@code true} the input argument will be cloned
88       * @param transformers  the transformers to chain, no nulls
89       */
90      private ChainedTransformer(final boolean clone, final Transformer<? super T, ? extends T>[] transformers) {
91          iTransformers = clone ? FunctorUtils.copy(transformers) : transformers;
92      }
93  
94      /**
95       * Constructor that performs no validation.
96       * Use {@code chainedTransformer} if you want that.
97       *
98       * @param transformers  the transformers to chain, copied, no nulls
99       */
100     public ChainedTransformer(final Transformer<? super T, ? extends T>... transformers) {
101         this(true, transformers);
102     }
103 
104     /**
105      * Gets the transformers.
106      *
107      * @return a copy of the transformers
108      * @since 3.1
109      */
110     public Transformer<? super T, ? extends T>[] getTransformers() {
111         return FunctorUtils.<T, T>copy(iTransformers);
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     @Override
121     public T transform(T object) {
122         for (final Transformer<? super T, ? extends T> iTransformer : iTransformers) {
123             object = iTransformer.transform(object);
124         }
125         return object;
126     }
127 
128 }