Class FunctionalIterate

Object
FunctionalIterate

public class FunctionalIterate extends Object
Utilities for repeating operations a certain number of times.
Author:
Owen Feehan
  • Method Details

    • repeat

      public static <E extends Exception> void repeat(int numberTimes, CheckedRunnable<E> operation) throws E
      Repeats an operation a number of times.
      Type Parameters:
      E - an exception that may be thrown by operation.
      Parameters:
      numberTimes - how many times to repeat the operation
      operation - the operation
      Throws:
      E - if operation throws it.
    • repeatCountSuccessful

      public static <E extends Exception> int repeatCountSuccessful(int numberTimes, CheckedBooleanSupplier<E> operation) throws E
      Repeats an operation a number of times, counting the number of times the operation returns true.
      Type Parameters:
      E - an exception that may be thrown by operation.
      Parameters:
      numberTimes - how many times to repeat the operation
      operation - the operation, which returns true for success, or false for failure.
      Returns:
      the number of successful operations (maximally numberTimes).
      Throws:
      E - if operation throws it.
    • repeatWithIndex

      public static <E extends Exception> void repeatWithIndex(int numberTimes, CheckedIntConsumer<E> operation) throws E
      Repeats an operation a number of times, passing an increment index to operation each time.

      The index begins at 0 and will increment to numberTimes -1 (inclusive).

      Type Parameters:
      E - an exception that may be thrown by operation.
      Parameters:
      numberTimes - how many times to repeat the operation
      operation - the operation to execute given an index
      Throws:
      E - if operation throws it.
    • repeatUntil

      public static <E extends Exception> boolean repeatUntil(int maxNumberTimes, CheckedBooleanSupplier<E> supplier) throws E
      Repeats an operation until a true value is returned.
      Type Parameters:
      E - an exception that may be thrown by the supplier
      Parameters:
      maxNumberTimes - the maximum number of times to repeat if no true value is returned.
      supplier - the operation to repeat that returns true or false
      Returns:
      true if at least one operation returned true, or false if no operation did.
      Throws:
      E - if supplier throws an exception.
    • repeatAsStream

      public static <T, E extends Exception> Stream<T> repeatAsStream(int numberTimes, Class<? extends E> throwableClass, CheckedSupplier<T,E> supplier) throws E
      Repeats an operation a number of times, to form a Stream.
      Type Parameters:
      T - type of element to created in the Stream.
      E - an exception that may be thrown by operation.
      Parameters:
      numberTimes - how many times to repeat the operation
      throwableClass - the class of E.
      supplier - the operation
      Returns:
      a newly created Stream of numberTimes * the objects created by supplier.
      Throws:
      E - if operation throws it.
    • iterateMap

      public static <K, V, E extends Exception> void iterateMap(Map<K,V> map, CheckedBiConsumer<K,V,E> consumer) throws E
      Iterate through all entries in a Map.
      Type Parameters:
      K - map key type, see corresponding parameter in @{link Map}.
      V - map value type, see corresponding parameter in @{link Map}.
      E - an exception that may be thrown by consumer.
      Parameters:
      map - the map whose entries will be iterated.
      consumer - called for each entry.
      Throws:
      E - if throw by consumer.
    • reverseIterateArray

      public static <T, E extends Exception> void reverseIterateArray(T[] array, CheckedConsumer<T,E> consumer) throws E
      Iterates through every element in an array in reverse order
      Type Parameters:
      T - array element type
      E - an exception that may be thrown by consumer.
      Parameters:
      array - the array
      consumer - called for each element in the array.
      Throws:
      E - if throw by consumer.