Class Waiter<C,​R>

  • Type Parameters:
    C - type of component
    R - type of result that may be returned when the desired condition arises
    Direct Known Subclasses:
    MediaPlayerWaiter, MediaWaiter

    public abstract class Waiter<C,​R>
    extends Object
    Base implementation for a component that waits for specific component state to occur.

    This implementation works by adding a temporary event listener to an associated component then waiting, via await(), for an internal synchronisation object to trigger, via ready() or ready(Object) when one or other of the event listener's implementation methods detects the desired component event. The temporary event listener is then removed and the await() method is unblocked and returns.

    Commonly needed triggers are implemented for error() and finished().

    This facilitates a semblance of synchronous or sequential programming.

    Sub-classes have access to the associated component if needed.

    Sub-classes may also override onBefore(Object) and onAfter(Object, Object) to implement behaviour that executes respectively before awaiting the condition and after the condition state is reached.

    Using onBefore(Object) guarantees that the component event listener has been registered with the media player before the condition implementation is executed (e.g. an implementation of this method could check if the condition they would otherwise wait for has already been achieved).

    Note that as with other media player and related component implementations the event callbacks are running in a native thread.

    See Also:
    MediaWaiter, MediaPlayerWaiter