Package de.arstwo.twotil
Class Guarded<T>
java.lang.Object
de.arstwo.twotil.Guarded<T>
Functional approach of thread-safe lock access to a given target.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAccesses the target and supplies it to the given consumer when ready.<R> RAccesses the target, supplies it to the given function when ready and returns the computed result.booleanTry immediate access to the target and forward it to the given consumer.<R> Optional<R> tryProcess(Function<T, R> action) Try to access the target immediately, supplies it to the given function when ready and possibly returns the computed result.
-
Constructor Details
-
Guarded
-
Guarded
-
-
Method Details
-
access
Accesses the target and supplies it to the given consumer when ready.- Parameters:
action- any consumer for the target
-
process
Accesses the target, supplies it to the given function when ready and returns the computed result.- Type Parameters:
R- any result type of the function given- Parameters:
action- the function to apply to the target once ready- Returns:
- the result of the function
-
tryAccess
Try immediate access to the target and forward it to the given consumer.- Parameters:
action- any consumer for the target- Returns:
- true if the target could be accessed immediately, false otherwise.
-
tryProcess
Try to access the target immediately, supplies it to the given function when ready and possibly returns the computed result.- Type Parameters:
R- any result type of the function given- Parameters:
action- the function to apply to the target if ready- Returns:
- an optional result of the function applied if the target was ready, an empty optional if it was not ready.
-