Top Description Fields Constructors Methods
io.github.resilience4j.core.functions

public final Class OnceConsumer<T>

extends Object
Class Inheritance
Type Parameters
<T>
the type of the input which is passed to the consumer and executed once.
Imports
java.util.concurrent.atomic.AtomicBoolean, java.util.function.Consumer

Apply a computation only once.

Field Summary

Modifier and TypeField and Description
private final AtomicBoolean
pack-priv final T
t

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
applyOnce(Consumer<T>
computation run once with input t
consumer
)

Apply a computation on subject only once.

public static <
type of input on which operation is applied.
T
>
OnceConsumer<T>
of(T
input which is passed to operation
t
)

Create a do once consumer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hasRunback to summary
private final AtomicBoolean hasRun
tback to summary
pack-priv final T t

Constructor Detail

OnceConsumerback to summary
private OnceConsumer(T t)

Method Detail

applyOnceback to summary
public void applyOnce(Consumer<T> consumer)

Apply a computation on subject only once.


List<String> lst = new ArrayList<>();

OnceConsumer<List<String>> once = OnceConsumer.of(lst);
once.applyOnce((l) -> l.add("Hello World"));
once.applyOnce((l) -> l.add("Hello World"));

assertThat(lst).hasSize(1).contains("Hello World");

Parameters
consumer:Consumer<T>

computation run once with input t

ofback to summary
public static <T> OnceConsumer<T> of(T t)

Create a do once consumer.

Parameters
<T>
type of input on which operation is applied.
t:T

input which is passed to operation