All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.jalokim.utils.iteration.IterationUtils Maven / Gradle / Ivy

package pl.jalokim.utils.iteration;

/**
 * Another way of iterate code.
 */
public final class IterationUtils {

    private IterationUtils() {

    }

    public static void repeatNTimes(int nTimes, ExceptionableRunnable runnable) {
        repeatNTimes(nTimes, (index) -> runnable.invoke());
    }

    @SuppressWarnings({"IllegalCatch", "PMD.AvoidRethrowingException"})
    public static void repeatNTimes(int nTimes, ExceptionableConsumer consumer) {
        for (int i = 0; i < nTimes; i++) {
            try {
                consumer.consume(i);
            } catch (RuntimeException ex) {
                throw ex;
            } catch (Exception throwable) {
                throw new IterationException(throwable);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy