- Type Parameters:
T- type of reference
Use with Inject eg. @Inject Ref<List<String>> strings; Then
in init() / setup() use strings.init(ArrayList::new);.
Can also be used with Out and AuxOut annotations to provide
output ports to share the Ref value with Ref.Input input ports on
other components.
A Ref field on a container can additionally be annotated with
Ref.Publish to share the value with direct child Ref fields annotated
with Ref.Subscribe.
Many methods will throw an Exception if init() has not been called with a Supplier function, even if the value has been set
The default dispose handler checks if the referenced value is
AutoCloseable and automatically closes it.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classEvent passed toonChangeHandlerwhen the Ref value changes.static interfaceA functional type for initializing a Ref, used by Providers.static classA field type for Ref input ports.static classProviders initialize Ref instances so that the underlying value can be used directly as the injected field type.static @interfaceAnnotation to be used on aReffield on a container, to allow Ref fields of direct child components to subscribe and bind to the values of the published Ref.static @interfaceAnnotation to be used on aReffield to bind its values to the values of the published Ref in the direct parent container. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionPass the value to the provided Consumer function.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 attachment and removal on reference change, reset or disposal.clear()Disposes the value and clears initialization.Transform the value using the supplied function.protected voiddispose()get()Return the value.Pass the value to the provided Consumer function if one exists.Initialize the reference, calling the supplier function if a value is needed.protected abstract voidonChange(Consumer<Ref.ChangeEvent<T>> onChangeHandler) Provide a function to handle changes in the Ref value.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, orclearhas been explicitly called.Provide a function to run on the value whenever the Ref is reset - eg.Returns the ref value if present, orother.protected voidreset()Set the value.Set the value from completion of the providedAsync.unbind()Clear all bindings added viabind(java.util.function.BiConsumer, java.util.function.BiConsumer, java.lang.Object)from this Ref.protected voidvalueChanged(T currentValue, T previousValue)
-
Constructor Details
-
Ref
public Ref()
-
-
Method Details
-
init
Initialize the reference, calling the supplier function if a value is needed.The supplier may return null, although this is not recommended.
- Parameters:
supplier-- Returns:
- this
-
get
Return the value. The Ref must be initialized by callinginitfirst.- Returns:
- value
-
clear
Disposes the value and clears initialization. Before using the Ref again it must be re-initialized.- Returns:
- this
-
apply
Pass the value to the provided Consumer function. The value must be initialized first.- Parameters:
consumer-- Returns:
- this
-
compute
Transform the value using the supplied function. Either an existing or new value may be returned. If a new value is returned, the value will be replaced and anyonResetandonDisposehandlers called.- Parameters:
function-- Returns:
- this
-
set
Set the value. This is a shortcut equivalent to callingref.init(() -> value).compute(old -> value).- Parameters:
value- ref value- Returns:
- this
-
setAsync
Set the value from completion of the providedAsync. If the Async is already completed, the value will be set before return. Calls to other methods that set the Ref value will cancel the pending set.- Parameters:
async- async value to set- Returns:
- this
-
bind
Bind something (usually a callback / listener) to the reference, providing for automatic attachment and removal on reference change, reset or disposal. This also allows for the easy management of listeners that are lambdas or method references, without the need to keep a reference to them.The binder and unbinder arguments will usually be method references for the add and remove listener methods. The bindee will usually be the listener, often as a lambda or method reference.
This method does not require the reference to have been initialized. If the reference is available, the bindee will be attached during this method call. If the reference is not available, the bindee will be queued for attachment when the reference is set.
- Type Parameters:
V- the type of the value to bind to the reference, usually a callback / listener- Parameters:
binder- 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 reference- Returns:
- this
-
unbind
Clear all bindings added viabind(java.util.function.BiConsumer, java.util.function.BiConsumer, java.lang.Object)from this Ref.- Returns:
- this
-
ifPresent
Pass the value to the provided Consumer function if one exists.Unlike
applythis may be safely called prior to initialization.- Parameters:
consumer-- Returns:
- this
-
orElse
Returns the ref value if present, orother.This method may be safely called prior when the ref has not been initialized. If the ref has been initialized to
nullthen the other value will be returned.- Parameters:
other- value to return if not initialized or null- Returns:
- value or other
-
onChange
Provide a function to handle changes in the Ref value. The providedRef.ChangeEventgives access to the current and previous values, if available.- Parameters:
onChangeHandler- handler of change event- Returns:
- this
-
onReset
Provide a function to run on the value whenever the Ref is reset - eg. when the Ref is passed from one iteration of code to the next.- Parameters:
onResetHandler- handler to reset ref value- Returns:
- this
-
onDispose
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, orclearhas been explicitly called.- Parameters:
onDisposeHandler- handler to dispose ref value- Returns:
- this
-
dispose
protected void dispose() -
reset
protected void reset() -
valueChanged
-
log
-