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 * @since 3.0
033 */
034public class ChainedTransformer<T> implements Transformer<T, T>, Serializable {
035
036    private static final Transformer[] EMPTY_TRANSFORMER_ARRAY = {};
037
038    /** Serial version UID */
039    private static final long serialVersionUID = 3514945074733160196L;
040
041    /**
042     * Create a new Transformer that calls each transformer in turn, passing the
043     * result into the next transformer. The ordering is that of the iterator()
044     * method on the collection.
045     *
046     * @param <T>  the object type
047     * @param transformers  a collection of transformers to chain
048     * @return the {@code chained} transformer
049     * @throws NullPointerException if the transformers collection is null
050     * @throws NullPointerException if any transformer in the collection is null
051     */
052    public static <T> Transformer<T, T> chainedTransformer(
053            final Collection<? extends Transformer<? super T, ? extends T>> transformers) {
054        Objects.requireNonNull(transformers, "transformers");
055        if (transformers.isEmpty()) {
056            return NOPTransformer.<T>nopTransformer();
057        }
058        // convert to array like this to guarantee iterator() ordering
059        final Transformer<T, T>[] cmds = transformers.toArray(EMPTY_TRANSFORMER_ARRAY);
060        FunctorUtils.validate(cmds);
061        return new ChainedTransformer<>(false, cmds);
062    }
063
064    /**
065     * Factory method that performs validation and copies the parameter array.
066     *
067     * @param <T>  the object type
068     * @param transformers  the transformers to chain, copied, no nulls
069     * @return the {@code chained} transformer
070     * @throws NullPointerException if the transformers array is null
071     * @throws NullPointerException if any transformer in the array is null
072     */
073    public static <T> Transformer<T, T> chainedTransformer(final Transformer<? super T, ? extends T>... transformers) {
074        FunctorUtils.validate(transformers);
075        if (transformers.length == 0) {
076            return NOPTransformer.<T>nopTransformer();
077        }
078        return new ChainedTransformer<>(transformers);
079    }
080
081    /** The transformers to call in turn */
082    private final Transformer<? super T, ? extends T>[] iTransformers;
083
084    /**
085     * Hidden constructor for the use by the static factory methods.
086     *
087     * @param clone  if {@code true} the input argument will be cloned
088     * @param transformers  the transformers to chain, no nulls
089     */
090    private ChainedTransformer(final boolean clone, final Transformer<? super T, ? extends T>[] transformers) {
091        iTransformers = clone ? FunctorUtils.copy(transformers) : transformers;
092    }
093
094    /**
095     * Constructor that performs no validation.
096     * Use {@code chainedTransformer} if you want that.
097     *
098     * @param transformers  the transformers to chain, copied, no nulls
099     */
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}