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