Class ReactiveLatch

java.lang.Object
dev.sympho.modular_commands.utils.ReactiveLatch
All Implemented Interfaces:
Function<Object,org.reactivestreams.Publisher<Void>>

public class ReactiveLatch extends Object implements Function<Object,org.reactivestreams.Publisher<Void>>
An equivalent to Java's CountDownLatch that uses Reactive Streams for non-blocking waiting. It can be used by either directly subscribing to await() or by using an instance of this class with Mono.delayUntil(Function) (or the Flux equivalent).

The latch can also be failed, propagating an error to those waiting on it instead of a completion.

Since:
1.0
Version:
1.0
See Also:
  • Constructor Details

    • ReactiveLatch

      @SideEffectFree public ReactiveLatch(long needed) throws IllegalArgumentException
      Creates a new latch that opens after counting down a given number of times.
      Parameters:
      needed - How many times countDown() needs to be called before the latch opens.
      Throws:
      IllegalArgumentException - if the given value is not positive (>0).
    • ReactiveLatch

      @SideEffectFree public ReactiveLatch()
      Creates a new latch that opens after countDown() is called once. This is effectively a binary latch or a switch.
  • Method Details

    • apply

      @Pure public org.reactivestreams.Publisher<Void> apply(Object ignored)
      Creates a publisher that completes once this latch opens.
      Specified by:
      apply in interface Function<Object,org.reactivestreams.Publisher<Void>>
    • await

      @Pure public Mono<Void> await()
      Creates a Mono that emits a completion signal when this latch enters the open state.

      Once the latch is open, subscribing to the returned Mono will complete instantly.

      Returns:
      The Mono to wait on.
    • countDown

      public void countDown() throws IllegalStateException
      Counts down the latch. If the internal counter reaches zero, the latch opens.

      Has no effect if the latch is already opened or failed.

      Throws:
      IllegalStateException - if emitting the completion signal failed.
    • fail

      public void fail(Throwable error) throws IllegalStateException
      Fails the latch, issuing an error signal to those waiting on it.

      Has no effect if the latch is already opened or failed.

      Parameters:
      error - The error to issue.
      Throws:
      IllegalStateException - if emitting the error signal failed.