Interface Lifecycled<T>
- Type Parameters:
T
- the value type
- All Known Implementing Classes:
ConstantLifecycled
,SimpleLifecycled
public interface Lifecycled<T>
Represents an object with a simple valid/invalid lifecycle.
A lifecycled object will start with no value, then trigger
Lifecycled.Events.onNewValue(Object, BiConsumer)
callbacks when it gets one, and
Lifecycled.Events.onInvalidated(Object, BiConsumer)
callbacks when it loses it. A full
invalidated->new value cycle is called a "reload".
Downstream lifecycled objects can be derived using functional methods, and share some
common rules. They will apply the operation sometime before the result is needed, either
eagerly or lazily. They will re-do the operation after the upstream Lifecycled
is
reloaded.
Unless specified, Lifecycled
objects are not thread-safe. However, the
Lifecycled.Events
objects are, and callbacks may be added from any thread.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionevents()
Get the event manager for this lifecycled object.default Lifecycled
<T> Filter the value.default <U> Lifecycled
<U> flatMap
(Function<T, Lifecycled<U>> mapper) default boolean
isValid()
Check for validity, usually without triggering computation.default <U> Lifecycled
<U> Map the value.value()
Get the value orOptional.empty()
.default T
Get the value or throw.
-
Method Details
-
value
Get the value orOptional.empty()
.- Returns:
- the value
-
valueOrThrow
Get the value or throw.- Returns:
- the value
- Throws:
IllegalStateException
- if there is no value
-
isValid
default boolean isValid()Check for validity, usually without triggering computation.- Returns:
- if this lifecycled's
value()
is valid
-
events
Lifecycled.Events<T> events()Get the event manager for this lifecycled object.- Returns:
- the event manager
-
map
Map the value.- Type Parameters:
U
- the new type- Parameters:
mapper
- the mapper function- Returns:
- the downstream lifecycled
-
filter
Filter the value. In other words, create a new lifecycled object where validity is ANDed with the result of calling the filter function.- Parameters:
filterer
- the filter function- Returns:
- the downstream lifecycled
-
flatMap
-