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   * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> 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   * @param <T> the type of the input to the operation.
31   * @since 3.0
32   */
33  public class ForClosure<T> implements Closure<T> {
34  
35      /**
36       * Factory method that performs validation.
37       * <p>
38       * A null closure or zero count returns the {@code NOPClosure}.
39       * A count of one returns the specified closure.
40       *
41       * @param <E> the type that the closure acts on
42       * @param count  the number of times to execute the closure
43       * @param closure  the closure to execute, not null
44       * @return the {@code for} closure
45       */
46      @SuppressWarnings("unchecked")
47      public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) {
48          if (count <= 0 || closure == null) {
49              return NOPClosure.<E>nopClosure();
50          }
51          if (count == 1) {
52              return (Closure<E>) closure;
53          }
54          return new ForClosure<>(count, closure);
55      }
56      /** The number of times to loop */
57      private final int iCount;
58  
59      /** The closure to call */
60      private final Closure<? super T> iClosure;
61  
62      /**
63       * Constructor that performs no validation.
64       * Use {@code forClosure} if you want that.
65       *
66       * @param count  the number of times to execute the closure
67       * @param closure  the closure to execute, not null
68       */
69      public ForClosure(final int count, final Closure<? super T> closure) {
70          iCount = count;
71          iClosure = closure;
72      }
73  
74      /**
75       * Executes the closure {@code count} times.
76       *
77       * @param input  the input object
78       */
79      @Override
80      public void execute(final T input) {
81          for (int i = 0; i < iCount; i++) {
82              iClosure.accept(input);
83          }
84      }
85  
86      /**
87       * Gets the closure.
88       *
89       * @return the closure
90       * @since 3.1
91       */
92      public Closure<? super T> getClosure() {
93          return iClosure;
94      }
95  
96      /**
97       * Gets the count.
98       *
99       * @return the count
100      * @since 3.1
101      */
102     public int getCount() {
103         return iCount;
104     }
105 
106 }