Top Description Methods
org.springframework.util.function

public Interface ThrowingConsumer<T>

extends Consumer<T>
Annotations
@FunctionalInterface
Type Parameters
<T>
the type of the input to the operation
Imports
java.util.function.BiFunction, .Consumer

A Consumer that allows invocation of code that throws a checked exception.
Authors
Stephane Nicoll, Phillip Webb
Since
6.0

Method Summary

Modifier and TypeMethod and Description
public default void
accept(T
the input argument
t
)

Implements java.util.function.Consumer.accept.

Default Consumer#accept(Object) that wraps any thrown checked exceptions (by default in a RuntimeException).
public default void
accept(T t, BiFunction<String, Exception, RuntimeException>
BiFunction that wraps the given message and checked exception into a runtime exception
exceptionWrapper
)

Performs this operation on the given argument, wrapping any thrown checked exceptions using the given exceptionWrapper.

public void
acceptWithException(T
the input argument
t
)

Performs this operation on the given argument, possibly throwing a checked exception.

public static <
the type of the input to the operation
T
>
ThrowingConsumer<T>

Returns:

a new ThrowingConsumer instance
of
(ThrowingConsumer<T>
the source consumer
consumer
)

Lambda friendly convenience method that can be used to create a ThrowingConsumer where the accept(Object) method wraps any checked exception thrown by the supplied lambda expression or method reference.

public static <
the type of the input to the operation
T
>
ThrowingConsumer<T>

Returns:

a new ThrowingConsumer instance
of
(ThrowingConsumer<T>
the source consumer
consumer
,
BiFunction<String, Exception, RuntimeException>
the exception wrapper to use
exceptionWrapper
)

Lambda friendly convenience method that can be used to create a ThrowingConsumer where the accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper.

public default ThrowingConsumer<T>

Returns:

the replacement ThrowingConsumer instance
throwing
(BiFunction<String, Exception, RuntimeException>
BiFunction that wraps the given message and checked exception into a runtime exception
exceptionWrapper
)

Return a new ThrowingConsumer where the accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper.

Inherited from java.util.function.Consumer:
andThen

Method Detail

acceptback to summary
public default void accept(T t)

Implements java.util.function.Consumer.accept.

Default Consumer#accept(Object) that wraps any thrown checked exceptions (by default in a RuntimeException).

Parameters
t:T

Doc from java.util.function.Consumer.accept.

the input argument

Annotations
@Override
See Also
java.util.function.Consumer#accept(Object)
acceptback to summary
public default void accept(T t, BiFunction<String, Exception, RuntimeException> exceptionWrapper)

Performs this operation on the given argument, wrapping any thrown checked exceptions using the given exceptionWrapper.

Parameters
exceptionWrapper:BiFunction<String, Exception, RuntimeException>

BiFunction that wraps the given message and checked exception into a runtime exception

acceptWithExceptionback to summary
public void acceptWithException(T t) throws Exception

Performs this operation on the given argument, possibly throwing a checked exception.

Parameters
t:T

the input argument

Exceptions
Exception:
on error
ofback to summary
public static <T> ThrowingConsumer<T> of(ThrowingConsumer<T> consumer)

Lambda friendly convenience method that can be used to create a ThrowingConsumer where the accept(Object) method wraps any checked exception thrown by the supplied lambda expression or method reference.

This method can be especially useful when working with method references. It allows you to easily convert a method that throws a checked exception into an instance compatible with a regular Consumer.

For example:

list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException));
Parameters
<T>
the type of the input to the operation
consumer:ThrowingConsumer<T>

the source consumer

Returns:ThrowingConsumer<T>

a new ThrowingConsumer instance

ofback to summary
public static <T> ThrowingConsumer<T> of(ThrowingConsumer<T> consumer, BiFunction<String, Exception, RuntimeException> exceptionWrapper)

Lambda friendly convenience method that can be used to create a ThrowingConsumer where the accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper.

This method can be especially useful when working with method references. It allows you to easily convert a method that throws a checked exception into an instance compatible with a regular Consumer.

For example:

list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
Parameters
<T>
the type of the input to the operation
consumer:ThrowingConsumer<T>

the source consumer

exceptionWrapper:BiFunction<String, Exception, RuntimeException>

the exception wrapper to use

Returns:ThrowingConsumer<T>

a new ThrowingConsumer instance

throwingback to summary
public default ThrowingConsumer<T> throwing(BiFunction<String, Exception, RuntimeException> exceptionWrapper)

Return a new ThrowingConsumer where the accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper.

Parameters
exceptionWrapper:BiFunction<String, Exception, RuntimeException>

BiFunction that wraps the given message and checked exception into a runtime exception

Returns:ThrowingConsumer<T>

the replacement ThrowingConsumer instance