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.Closure;
024
025/**
026 * Closure implementation that chains the specified closures together.
027 *
028 * @param <T> the type of the input to the operation.
029 * @since 3.0
030 */
031public class ChainedClosure<T> implements Closure<T>, Serializable {
032
033    /** Serial version UID */
034    private static final long serialVersionUID = -3520677225766901240L;
035
036    /**
037     * Factory method that performs validation and copies the parameter array.
038     *
039     * @param <E> the type that the closure acts on
040     * @param closures  the closures to chain, copied, no nulls
041     * @return the {@code chained} closure
042     * @throws NullPointerException if the closures array is null
043     * @throws NullPointerException if any closure in the array is null
044     */
045    public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures) {
046        FunctorUtils.validate(closures);
047        if (closures.length == 0) {
048            return NOPClosure.<E>nopClosure();
049        }
050        return new ChainedClosure<>(closures);
051    }
052
053    /**
054     * Create a new Closure that calls each closure in turn, passing the
055     * result into the next closure. The ordering is that of the iterator()
056     * method on the collection.
057     *
058     * @param <E> the type that the closure acts on
059     * @param closures  a collection of closures to chain
060     * @return the {@code chained} closure
061     * @throws NullPointerException if the closures collection is null
062     * @throws NullPointerException if any closure in the collection is null
063     */
064    @SuppressWarnings("unchecked")
065    public static <E> Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures) {
066        Objects.requireNonNull(closures, "closures");
067        if (closures.isEmpty()) {
068            return NOPClosure.<E>nopClosure();
069        }
070        // convert to array like this to guarantee iterator() ordering
071        final Closure<? super E>[] cmds = new Closure[closures.size()];
072        int i = 0;
073        for (final Closure<? super E> closure : closures) {
074            cmds[i++] = closure;
075        }
076        FunctorUtils.validate(cmds);
077        return new ChainedClosure<>(false, cmds);
078    }
079
080    /** The closures to call in turn */
081    private final Closure<? super T>[] iClosures;
082
083    /**
084     * Hidden constructor for the use by the static factory methods.
085     *
086     * @param clone  if {@code true} the input argument will be cloned
087     * @param closures  the closures to chain, no nulls
088     */
089    private ChainedClosure(final boolean clone, final Closure<? super T>... closures) {
090        iClosures = clone ? FunctorUtils.copy(closures) : closures;
091    }
092
093    /**
094     * Constructor that performs no validation.
095     * Use {@code chainedClosure} if you want that.
096     *
097     * @param closures  the closures to chain, copied, no nulls
098     */
099    public ChainedClosure(final Closure<? super T>... closures) {
100        this(true, closures);
101    }
102
103    /**
104     * Execute a list of closures.
105     *
106     * @param input  the input object passed to each closure
107     */
108    @Override
109    public void execute(final T input) {
110        for (final Closure<? super T> iClosure : iClosures) {
111            iClosure.accept(input);
112        }
113    }
114
115    /**
116     * Gets the closures.
117     *
118     * @return a copy of the closures
119     * @since 3.1
120     */
121    public Closure<? super T>[] getClosures() {
122        return FunctorUtils.copy(iClosures);
123    }
124
125}