ForClosure.java

  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. import org.apache.commons.collections4.Closure;

  19. /**
  20.  * Closure implementation that calls another closure n times, like a for loop.
  21.  * <p>
  22.  * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
  23.  * in order to prevent potential remote code execution exploits. Please refer to
  24.  * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
  25.  * for more details.
  26.  * </p>
  27.  *
  28.  * @param <T> the type of the input to the operation.
  29.  * @since 3.0
  30.  */
  31. public class ForClosure<T> implements Closure<T> {

  32.     /**
  33.      * Factory method that performs validation.
  34.      * <p>
  35.      * A null closure or zero count returns the {@code NOPClosure}.
  36.      * A count of one returns the specified closure.
  37.      *
  38.      * @param <E> the type that the closure acts on
  39.      * @param count  the number of times to execute the closure
  40.      * @param closure  the closure to execute, not null
  41.      * @return the {@code for} closure
  42.      */
  43.     @SuppressWarnings("unchecked")
  44.     public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) {
  45.         if (count <= 0 || closure == null) {
  46.             return NOPClosure.<E>nopClosure();
  47.         }
  48.         if (count == 1) {
  49.             return (Closure<E>) closure;
  50.         }
  51.         return new ForClosure<>(count, closure);
  52.     }
  53.     /** The number of times to loop */
  54.     private final int iCount;

  55.     /** The closure to call */
  56.     private final Closure<? super T> iClosure;

  57.     /**
  58.      * Constructor that performs no validation.
  59.      * Use {@code forClosure} if you want that.
  60.      *
  61.      * @param count  the number of times to execute the closure
  62.      * @param closure  the closure to execute, not null
  63.      */
  64.     public ForClosure(final int count, final Closure<? super T> closure) {
  65.         iCount = count;
  66.         iClosure = closure;
  67.     }

  68.     /**
  69.      * Executes the closure {@code count} times.
  70.      *
  71.      * @param input  the input object
  72.      */
  73.     @Override
  74.     public void execute(final T input) {
  75.         for (int i = 0; i < iCount; i++) {
  76.             iClosure.accept(input);
  77.         }
  78.     }

  79.     /**
  80.      * Gets the closure.
  81.      *
  82.      * @return the closure
  83.      * @since 3.1
  84.      */
  85.     public Closure<? super T> getClosure() {
  86.         return iClosure;
  87.     }

  88.     /**
  89.      * Gets the count.
  90.      *
  91.      * @return the count
  92.      * @since 3.1
  93.      */
  94.     public int getCount() {
  95.         return iCount;
  96.     }

  97. }