View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.functors;
18  
19  import org.apache.commons.collections4.Closure;
20  
21  /**
22   * Closure implementation that calls another closure n times, like a for loop.
23   * <p>
24   * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> be serializable anymore
25   * in order to prevent potential remote code execution exploits. Please refer to
26   * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
27   * for more details.
28   * </p>
29   *
30   * @since 3.0
31   */
32  public class ForClosure<E> implements Closure<E> {
33  
34      /**
35       * Factory method that performs validation.
36       * <p>
37       * A null closure or zero count returns the {@code NOPClosure}.
38       * A count of one returns the specified closure.
39       *
40       * @param <E> the type that the closure acts on
41       * @param count  the number of times to execute the closure
42       * @param closure  the closure to execute, not null
43       * @return the {@code for} closure
44       */
45      @SuppressWarnings("unchecked")
46      public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) {
47          if (count <= 0 || closure == null) {
48              return NOPClosure.<E>nopClosure();
49          }
50          if (count == 1) {
51              return (Closure<E>) closure;
52          }
53          return new ForClosure<>(count, closure);
54      }
55      /** The number of times to loop */
56      private final int iCount;
57  
58      /** The closure to call */
59      private final Closure<? super E> iClosure;
60  
61      /**
62       * Constructor that performs no validation.
63       * Use {@code forClosure} if you want that.
64       *
65       * @param count  the number of times to execute the closure
66       * @param closure  the closure to execute, not null
67       */
68      public ForClosure(final int count, final Closure<? super E> closure) {
69          iCount = count;
70          iClosure = closure;
71      }
72  
73      /**
74       * Executes the closure {@code count} times.
75       *
76       * @param input  the input object
77       */
78      @Override
79      public void execute(final E input) {
80          for (int i = 0; i < iCount; i++) {
81              iClosure.execute(input);
82          }
83      }
84  
85      /**
86       * Gets the closure.
87       *
88       * @return the closure
89       * @since 3.1
90       */
91      public Closure<? super E> getClosure() {
92          return iClosure;
93      }
94  
95      /**
96       * Gets the count.
97       *
98       * @return the count
99       * @since 3.1
100      */
101     public int getCount() {
102         return iCount;
103     }
104 
105 }