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 * https://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 * </p> 041 * 042 * @param <E> The type that the closure acts on 043 * @param count The number of times to execute the closure 044 * @param closure The closure to execute, not null 045 * @return The {@code for} closure 046 */ 047 @SuppressWarnings("unchecked") 048 public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) { 049 if (count <= 0 || closure == null) { 050 return NOPClosure.<E>nopClosure(); 051 } 052 if (count == 1) { 053 return (Closure<E>) closure; 054 } 055 return new ForClosure<>(count, closure); 056 } 057 058 /** The number of times to loop */ 059 private final int iCount; 060 061 /** The closure to call */ 062 private final Closure<? super T> iClosure; 063 064 /** 065 * Constructor that performs no validation. 066 * Use {@code forClosure} if you want that. 067 * 068 * @param count The number of times to execute the closure 069 * @param closure The closure to execute, not null 070 */ 071 public ForClosure(final int count, final Closure<? super T> closure) { 072 iCount = count; 073 iClosure = closure; 074 } 075 076 /** 077 * Executes the closure {@code count} times. 078 * 079 * @param input The input object 080 */ 081 @Override 082 public void execute(final T input) { 083 for (int i = 0; i < iCount; i++) { 084 iClosure.accept(input); 085 } 086 } 087 088 /** 089 * Gets the closure. 090 * 091 * @return The closure 092 * @since 3.1 093 */ 094 public Closure<? super T> getClosure() { 095 return iClosure; 096 } 097 098 /** 099 * Gets the count. 100 * 101 * @return The count 102 * @since 3.1 103 */ 104 public int getCount() { 105 return iCount; 106 } 107 108}