    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 org.apache.commons.collections4.Closure;
    020
    021/**
    022 * Closure implementation that calls another closure n times, like a for loop.
    023 * <p>
    024 * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
    025 * in order to prevent potential remote code execution exploits. Please refer to
    026 * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
    027 * for more details.
    028 * </p>
    029 *
    030 * @param <T> the type of the input to the operation.
    031 * @since 3.0
    032 */
    033public class ForClosure<T> implements Closure<T> {
    034
    035    /**
    036     * Factory method that performs validation.
    037     * <p>
    038     * A null closure or zero count returns the {@code NOPClosure}.
    039     * A count of one returns the specified closure.
    040     *
    041     * @param <E> the type that the closure acts on
    042     * @param count  the number of times to execute the closure
    043     * @param closure  the closure to execute, not null
    044     * @return the {@code for} closure
    045     */
    046    @SuppressWarnings("unchecked")
    047    public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) {
    048        if (count <= 0 || closure == null) {
    049            return NOPClosure.<E>nopClosure();
    050        }
    051        if (count == 1) {
    052            return (Closure<E>) closure;
    053        }
    054        return new ForClosure<>(count, closure);
    055    }
    056    /** The number of times to loop */
    057    private final int iCount;
    058
    059    /** The closure to call */
    060    private final Closure<? super T> iClosure;
    061
    062    /**
    063     * Constructor that performs no validation.
    064     * Use {@code forClosure} if you want that.
    065     *
    066     * @param count  the number of times to execute the closure
    067     * @param closure  the closure to execute, not null
    068     */
    069    public ForClosure(final int count, final Closure<? super T> closure) {
    070        iCount = count;
    071        iClosure = closure;
    072    }
    073
    074    /**
    075     * Executes the closure {@code count} times.
    076     *
    077     * @param input  the input object
    078     */
    079    @Override
    080    public void execute(final T input) {
    081        for (int i = 0; i < iCount; i++) {
    082            iClosure.accept(input);
    083        }
    084    }
    085
    086    /**
    087     * Gets the closure.
    088     *
    089     * @return the closure
    090     * @since 3.1
    091     */
    092    public Closure<? super T> getClosure() {
    093        return iClosure;
    094    }
    095
    096    /**
    097     * Gets the count.
    098     *
    099     * @return the count
    100     * @since 3.1
    101     */
    102    public int getCount() {
    103        return iCount;
    104    }
    105
    106}