Package io.vavr

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 Detail

      • 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:
        CheckedFunction1.of(CheckedFunction1)
      • accept

        void accept​(T t)
             throws java.lang.Throwable
        Performs an action on the given value, potentially causing side-effects.
        Parameters:
        t - the input value of type T
        Throws:
        java.lang.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:
        java.lang.NullPointerException - if after is null
      • unchecked

        default java.util.function.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