T
- public abstract class Ref<T> extends Object
Use with Inject
eg. @Inject Ref<List<String>> strings;
Then in
init() / setup() use strings.init(ArrayList::new);
Most methods will throw an Exception if init() has not been called with a Supplier function.
Constructor and Description |
---|
Ref() |
Modifier and Type | Method and Description |
---|---|
Ref<T> |
apply(Consumer<? super T> consumer)
Pass the value to the provided Consumer function.
|
<K> Ref<T> |
asyncCompute(K key,
Function<K,? extends T> function)
Run an intensive or time consuming function as a background task to update
the value.
|
<V> Ref<T> |
bind(BiConsumer<? super T,V> binder,
BiConsumer<? super T,V> unbinder,
V bindee)
Bind something (usually a callback / listener) to the reference, providing for automatic
removal on reset or disposal.
|
Ref<T> |
clear()
Disposes the value and clears initialization.
|
Ref<T> |
compute(Function<? super T,? extends T> function)
Transform the value using the supplied function.
|
T |
get()
Return the value.
|
Ref<T> |
ifPresent(Consumer<? super T> consumer)
Pass the value to the provided Consumer function if one exists.
Unlike
apply this may be safely called prior to initialization. |
Ref<T> |
init(Supplier<? extends T> supplier)
Initialize the reference, calling the supplier function if a value is needed.
|
Ref<T> |
onDispose(Consumer<? super T> onDisposeHandler)
Provide a function to run on the value whenever the value is being disposed of,
either because the Ref has been removed from the code, the root is being stopped,
or
clear has been explicitly called. |
Ref<T> |
onReset(Consumer<? super T> onResetHandler)
Provide a function to run on the value whenever the Ref is reset - eg.
|
public Ref<T> init(Supplier<? extends T> supplier)
supplier
- public T get()
init
first.public Ref<T> clear()
public Ref<T> apply(Consumer<? super T> consumer)
consumer
- public Ref<T> compute(Function<? super T,? extends T> function)
onReset
and
onDispose
handlers called.function
- public <K> Ref<T> asyncCompute(K key, Function<K,? extends T> function)
K
- type of key valuekey
- a key value used by the function to calculate the new valuefunction
- an intensive or time-consuming functionpublic <V> Ref<T> bind(BiConsumer<? super T,V> binder, BiConsumer<? super T,V> unbinder, V bindee)
V
- the type of the value to bind to the reference, usually a callback / listenerbinder
- the function to bind the value, usually a method reference on T that accepts a value Vunbinder
- the function to unbind the value, usually a method reference on T that accepts a value Vbindee
- the value, usually a lambda or method referencepublic Ref<T> ifPresent(Consumer<? super T> consumer)
apply
this may be safely called prior to initialization.consumer
- public Ref<T> onReset(Consumer<? super T> onResetHandler)
onResetHandler
- public Ref<T> onDispose(Consumer<? super T> onDisposeHandler)
clear
has been explicitly called.onDisposeHandler
-