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