Class Canceller


  • public class Canceller
    extends java.lang.Object
    Represents a way to check for a cancellation request. Users of this class (possibly cancelable, long-running operations) should periodically check whether a cancellation request has been received.
    • Method Detail

      • newCanceller

        public static Canceller newCanceller()
        Create a new Canceller which is trivially empty. The returned Canceller will never relay a positive cancellation request.
        Returns:
        an instance of Canceller.
      • isCancelable

        public boolean isCancelable()
        Check if this instance can ever return anything other than NOPE. This will be only be false for ROOT and short circuits thereof.
      • check

        public Cancellation check()
        Check if cancellation has been requested. This method should be invoked periodically, and the returned Cancellation should be inspected and reacted upon.
        Returns:
        an instance of Cancellation.
      • withCondition

        public Canceller withCondition​(java.lang.String reason,
                                       java.util.function.BooleanSupplier condition)
        Return a new Canceller based on a boolean predicate. The returned instance will relay a positive cancellation request when either the supplied boolean predicate is true or this Canceller is cancelled.
        Parameters:
        reason - The reason associated to the boolean condition.
        condition - A boolean predicate.
        Returns:
        a new instance of Canceller.
      • withTimeout

        public Canceller withTimeout​(java.lang.String reason,
                                     long duration,
                                     java.util.concurrent.TimeUnit unit)
        Return a new Canceller based on time duration. The returned instance will relay a positive cancellation request when either the duration expires or this Canceller is cancelled.
        Parameters:
        reason - The reason associated to the boolean condition.
        duration - The duration for the timeout.
        unit - The time unit for the duration.
        Returns:
        a new instance of Canceller.
      • withShortCircuit

        public Canceller withShortCircuit()
        Create a new Canceller based on this Canceller. The returned instance will be canceled when this instance is canceled, but will never transition back to an "uncanceled" state.
        Returns:
        a new instance of Canceller.