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 * 029 * @since 3.0 030 */ 031public class ForClosure<E> implements Closure<E> { 032 033 /** The number of times to loop */ 034 private final int iCount; 035 /** The closure to call */ 036 private final Closure<? super E> iClosure; 037 038 /** 039 * Factory method that performs validation. 040 * <p> 041 * A null closure or zero count returns the <code>NOPClosure</code>. 042 * A count of one returns the specified closure. 043 * 044 * @param <E> the type that the closure acts on 045 * @param count the number of times to execute the closure 046 * @param closure the closure to execute, not null 047 * @return the <code>for</code> closure 048 */ 049 @SuppressWarnings("unchecked") 050 public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) { 051 if (count <= 0 || closure == null) { 052 return NOPClosure.<E>nopClosure(); 053 } 054 if (count == 1) { 055 return (Closure<E>) closure; 056 } 057 return new ForClosure<>(count, closure); 058 } 059 060 /** 061 * Constructor that performs no validation. 062 * Use <code>forClosure</code> if you want that. 063 * 064 * @param count the number of times to execute the closure 065 * @param closure the closure to execute, not null 066 */ 067 public ForClosure(final int count, final Closure<? super E> closure) { 068 super(); 069 iCount = count; 070 iClosure = closure; 071 } 072 073 /** 074 * Executes the closure <code>count</code> 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}