Interface CheckedRunnable

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface CheckedRunnable
A Runnable that is allowed to throw checked exceptions.
  • Method Summary

    Modifier and Type
    Method
    Description
    of(@NonNull CheckedRunnable methodReference)
    Creates a CheckedRunnable from the given method reference or lambda.
    void
    run()
    Executes the action, potentially performing side-effects.
    default Runnable
    Returns an unchecked Runnable that sneakily throws any exception encountered during execution of this unit of work.
  • Method Details

    • of

      static CheckedRunnable of(@NonNull CheckedRunnable methodReference)
      Creates a CheckedRunnable from the given method reference or lambda.

      Example usage:

      // class Evil { static void sideEffect() { ... } }
      final CheckedRunnable checkedRunnable = CheckedRunnable.of(Evil::sideEffect);
      final Runnable runnable = checkedRunnable.unchecked();
      
      // may or may not perform the side-effect without throwing checked exceptions
      runnable.run();
      
      // may or may not perform the side-effect while potentially throwing
      runnable.run();
      
      Parameters:
      methodReference - typically a method reference, e.g. Type::method
      Returns:
      a new CheckedRunnable wrapping the given method reference
      See Also:
    • run

      void run() throws Throwable
      Executes the action, potentially performing side-effects.
      Throws:
      Throwable - if an error occurs during execution
    • unchecked

      default Runnable unchecked()
      Returns an unchecked Runnable that sneakily throws any exception encountered during execution of this unit of work.
      Returns:
      a Runnable that may throw any Throwable without declaring it