Package com.github.mike10004.xvfbmanager
Class Poller<T>
- java.lang.Object
-
- com.github.mike10004.xvfbmanager.Poller<T>
-
- Type Parameters:
T- type of content returned upon resolution
- Direct Known Subclasses:
Poller.SimplePoller
public abstract class Poller<T> extends Object
Class that facilitates polling for an arbitrary condition. Polling is the act of repeatedly querying the state at defined intervals. Polling stops when this poller'sevaluation functionanswers with a reason to stop, or the iterator of intervals to wait between polls is exhausted. Reasons to stop includeresolution, meaning the poller is satisfied with the result, orabortionmeaning polling must stop early without a resolution.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPoller.PollActionEnumeration of actions a poller's check function can return.static classPoller.PollAnswer<E>Class that represents an answer in response to a poll request.static classPoller.PollOutcome<E>Class that represents the outcome of a poll.protected static classPoller.RegularIntervalsprotected static classPoller.SimplePollerstatic classPoller.StopReasonEnmeration of reasons that polling stopped.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static <E> Poller.PollAnswer<E>abortPolling()protected static <E> Poller.PollAnswer<E>abortPolling(E value)protected abstract Poller.PollAnswer<T>check(int pollAttemptsSoFar)Checks whether the state being questioned has been resolved.static Poller<Void>checking(Supplier<Boolean> condition)Creates a simple poller that evaluates a condition on each poll.protected static <E> Poller.PollAnswer<E>continuePolling()Poller.PollOutcome<T>poll(long intervalMs, int maxNumPolls)Polls at regular intervals.Poller.PollOutcome<T>poll(Iterator<Long> intervalsMs)Starts polling and returns an outcome when polling stops.protected static <E> Poller.PollAnswer<E>resolve(E value)
-
-
-
Method Detail
-
poll
public Poller.PollOutcome<T> poll(long intervalMs, int maxNumPolls) throws InterruptedException
Polls at regular intervals.- Parameters:
intervalMs- the interval in millisecondsmaxNumPolls- the maximum number of polls to be executed- Returns:
- the poll outcome
- Throws:
InterruptedException- if waiting is interrupted
-
poll
public Poller.PollOutcome<T> poll(Iterator<Long> intervalsMs) throws InterruptedException
Starts polling and returns an outcome when polling stops.- Parameters:
intervalsMs- an iterator of interval lengths in milliseconds- Returns:
- the poll outcome
- Throws:
InterruptedException- if waiting is interrupted
-
resolve
protected static <E> Poller.PollAnswer<E> resolve(@Nullable E value)
-
continuePolling
protected static <E> Poller.PollAnswer<E> continuePolling()
-
abortPolling
protected static <E> Poller.PollAnswer<E> abortPolling(@Nullable E value)
-
abortPolling
protected static <E> Poller.PollAnswer<E> abortPolling()
-
check
protected abstract Poller.PollAnswer<T> check(int pollAttemptsSoFar)
Checks whether the state being questioned has been resolved. Subclasses must override this method to return ananswerthat may or may not contain a content object. In a conventional implementation, if the state has been resolved, this method would return an answer with content object representing a resolution along withPoller.PollAction.RESOLVE; if the state has not yet been resolved, this method would returnnullas the answer content along withPoller.PollAction.CONTINUEif we should continue polling orPoller.PollAction.ABORTif polling should stop immediately anyway.Implementations of this method should return an answer constructed with the
continuePolling(),abortPolling(), orresolve(Object)methods.Unconventional implementations may elect to return a non-null content object with
Poller.PollAction.ABORTto provide thepoll()caller a degenerate answer, perhaps indicating why state will never be resolved.- Parameters:
pollAttemptsSoFar- the number of poll attempts prior to this poll attempt- Returns:
- a poll answer
-
checking
public static Poller<Void> checking(Supplier<Boolean> condition)
Creates a simple poller that evaluates a condition on each poll.- Parameters:
condition- the condition to evaluate; poll will be resolved if it returns true, and if it returns false, the poller will keep polling- Returns:
- the poller
-
-