Interface CheckedConsumer<T>

Type Parameters:
T - the type of the input to the consumer
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 CheckedConsumer<T>
A Consumer that is allowed to throw checked exceptions.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(T t)
    Performs an action on the given value, potentially causing side-effects.
    andThen(@NonNull CheckedConsumer<? super T> after)
    Returns a composed CheckedConsumer that performs, in sequence, this.accept(t) followed by after.accept(t) for the same input t.
    static <T> CheckedConsumer<T>
    of(@NonNull CheckedConsumer<T> methodReference)
    Creates a CheckedConsumer from the given method reference or lambda.
    default Consumer<T>
    Returns an unchecked Consumer that sneakily throws any exception encountered while accepting a value.
  • Method Details

    • of

      static <T> CheckedConsumer<T> of(@NonNull CheckedConsumer<T> methodReference)
      Creates a CheckedConsumer from the given method reference or lambda.

      Example usage:

      final CheckedConsumer<Value> checkedConsumer = CheckedConsumer.of(Value::stdout);
      final Consumer<Value> consumer = checkedConsumer.unchecked();
      
      // prints "Hi" to the console
      consumer.accept(CharSeq.of("Hi!"));
      
      // may throw an exception
      consumer.accept(null);
      
      Type Parameters:
      T - the type of values accepted by the consumer
      Parameters:
      methodReference - typically a method reference, e.g. Type::method
      Returns:
      a new CheckedConsumer wrapping the given method reference
      See Also:
    • accept

      void accept(T t) throws Throwable
      Performs an action on the given value, potentially causing side-effects.
      Parameters:
      t - the input value of type T
      Throws:
      Throwable - if an error occurs during execution
    • andThen

      default CheckedConsumer<T> andThen(@NonNull CheckedConsumer<? super T> after)
      Returns a composed CheckedConsumer that performs, in sequence, this.accept(t) followed by after.accept(t) for the same input t.
      Parameters:
      after - the action to execute after this action
      Returns:
      a new CheckedConsumer that chains this and after
      Throws:
      NullPointerException - if after is null
    • unchecked

      default Consumer<T> unchecked()
      Returns an unchecked Consumer that sneakily throws any exception encountered while accepting a value.
      Returns:
      a Consumer that may throw any Throwable without declaring it