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 */ 017 package org.apache.commons.collections.functors; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.collections.Closure; 022 023 /** 024 * Closure implementation that calls another closure n times, like a for loop. 025 * 026 * @since Commons Collections 3.0 027 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 028 * 029 * @author Stephen Colebourne 030 */ 031 public class ForClosure implements Closure, Serializable { 032 033 /** Serial version UID */ 034 private static final long serialVersionUID = -1190120533393621674L; 035 036 /** The number of times to loop */ 037 private final int iCount; 038 /** The closure to call */ 039 private final Closure iClosure; 040 041 /** 042 * Factory method that performs validation. 043 * <p> 044 * A null closure or zero count returns the <code>NOPClosure</code>. 045 * A count of one returns the specified closure. 046 * 047 * @param count the number of times to execute the closure 048 * @param closure the closure to execute, not null 049 * @return the <code>for</code> closure 050 */ 051 public static Closure getInstance(int count, Closure closure) { 052 if (count <= 0 || closure == null) { 053 return NOPClosure.INSTANCE; 054 } 055 if (count == 1) { 056 return closure; 057 } 058 return new ForClosure(count, closure); 059 } 060 061 /** 062 * Constructor that performs no validation. 063 * Use <code>getInstance</code> 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(int count, Closure closure) { 069 super(); 070 iCount = count; 071 iClosure = closure; 072 } 073 074 /** 075 * Executes the closure <code>count</code> times. 076 * 077 * @param input the input object 078 */ 079 public void execute(Object 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 Commons Collections 3.1 090 */ 091 public Closure getClosure() { 092 return iClosure; 093 } 094 095 /** 096 * Gets the count. 097 * 098 * @return the count 099 * @since Commons Collections 3.1 100 */ 101 public int getCount() { 102 return iCount; 103 } 104 105 }