001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.functors;
018
019import java.io.Serializable;
020import java.util.Collection;
021import java.util.Objects;
022
023import org.apache.commons.collections4.Transformer;
024
025/**
026 * Transformer implementation that chains the specified transformers together.
027 * <p>
028 * The input object is passed to the first transformer. The transformed result
029 * is passed to the second transformer and so on.
030 * </p>
031 *
032 * @param <T> the type of the input and result to the function.
033 * @since 3.0
034 */
035public class ChainedTransformer<T> implements Transformer<T, T>, Serializable {
036
037    @SuppressWarnings("rawtypes")
038    private static final Transformer[] EMPTY_TRANSFORMER_ARRAY = {};
039
040    /** Serial version UID */
041    private static final long serialVersionUID = 3514945074733160196L;
042
043    /**
044     * Create a new Transformer that calls each transformer in turn, passing the
045     * result into the next transformer. The ordering is that of the iterator()
046     * method on the collection.
047     *
048     * @param <T>  the object type
049     * @param transformers  a collection of transformers to chain
050     * @return the {@code chained} transformer
051     * @throws NullPointerException if the transformers collection is null
052     * @throws NullPointerException if any transformer in the collection is null
053     */
054    public static <T> Transformer<T, T> chainedTransformer(
055            final Collection<? extends Transformer<? super T, ? extends T>> transformers) {
056        Objects.requireNonNull(transformers, "transformers");
057        if (transformers.isEmpty()) {
058            return NOPTransformer.<T>nopTransformer();
059        }
060        // convert to array like this to guarantee iterator() ordering
061        final Transformer<T, T>[] cmds = transformers.toArray(EMPTY_TRANSFORMER_ARRAY);
062        FunctorUtils.validate(cmds);
063        return new ChainedTransformer<>(false, cmds);
064    }
065
066    /**
067     * Factory method that performs validation and copies the parameter array.
068     *
069     * @param <T>  the object type
070     * @param transformers  the transformers to chain, copied, no nulls
071     * @return the {@code chained} transformer
072     * @throws NullPointerException if the transformers array is null
073     * @throws NullPointerException if any transformer in the array is null
074     */
075    public static <T> Transformer<T, T> chainedTransformer(final Transformer<? super T, ? extends T>... transformers) {
076        FunctorUtils.validate(transformers);
077        if (transformers.length == 0) {
078            return NOPTransformer.<T>nopTransformer();
079        }
080        return new ChainedTransformer<>(transformers);
081    }
082
083    /** The transformers to call in turn */
084    private final Transformer<? super T, ? extends T>[] iTransformers;
085
086    /**
087     * Hidden constructor for the use by the static factory methods.
088     *
089     * @param clone  if {@code true} the input argument will be cloned
090     * @param transformers  the transformers to chain, no nulls
091     */
092    private ChainedTransformer(final boolean clone, final Transformer<? super T, ? extends T>[] transformers) {
093        iTransformers = clone ? FunctorUtils.copy(transformers) : transformers;
094    }
095
096    /**
097     * Constructor that performs no validation.
098     * Use {@code chainedTransformer} if you want that.
099     *
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}