java.lang.Object
org.praxislive.code.userapi.Async.Queue<T>
- Type Parameters:
T- async result type
A queue for handling Async instances. The queue can be polled for added
Async instances that have completed, or a handler can be attached to run
on completion.
A queue cannot be constructed directly. Use the Inject annotation
on a field. Queues will have handlers and limits automatically removed on
reset, and will be cleared on disposal.
-
Method Summary
Modifier and TypeMethodDescriptionAdd an Async to the queue.clear()Clear all Async from the queue.limit(int size) Limit the queue to the provided size.voidAttach a handler for completed Async.voidConvenience method to link separate result and error handlers toonDone(java.util.function.Consumer).poll()Retrieves and removes the next completed Async, if available.booleanRemove the provided Async from the queue.intsize()Query current number of Async in the queue.
-
Method Details
-
poll
Retrieves and removes the next completed Async, if available. Returnsnullif no completed Async is available. The caller should check whether any returned Async has failed before extracting the result or error.- Returns:
- next completed Async or null
-
add
Add an Async to the queue. If the Async is already completed, and an onDone handler has been attached, this will be executed before this method returns.If the queue size limit has been reached, the least recently added Async will be evicted and returned to make space.
- Parameters:
async- Async to add to queue- Returns:
- evicted Async or null
-
remove
Remove the provided Async from the queue. If the provided Async is not in this queue, this is a no-op.- Parameters:
async- Async to remove- Returns:
- true if an element was removed
-
limit
Limit the queue to the provided size. If the current queue size is above the limit, the least recently added Async will be evicted and returned to make space.- Parameters:
size- requested size limit - at least 1.- Returns:
- evicted Async or empty list
- Throws:
IllegalArgumentException- if size is less than 1.
-
size
public int size()Query current number of Async in the queue.- Returns:
- current number of Async
-
clear
Clear all Async from the queue. Removed Async will be returned.- Returns:
- list of removed Async or empty list
-
onDone
Attach a handler for completed Async. Only one handler may be attached at a time. The handler will be run when any Async in the queue completes. The handler should check for failure before extracting any result or error. Any already completed Async in the queue will be passed to the handler before this method returns. Anullvalue may be passed to remove the current handler.- Parameters:
handler- handler for completed Async, or null to remove
-
onDone
Convenience method to link separate result and error handlers toonDone(java.util.function.Consumer). This method does not acceptnullvalues.- Parameters:
resultHandler- handler for succesful Async resulterrorHandler- handler for failed Async errors
-