Class MultiReady

java.lang.Object
com.tccc.kos.commons.util.ready.MultiReady

public class MultiReady extends Object
Class used to coordinate multiple asynchronous states to trigger a ready callback. The constructor takes a list of state objects and a target (either a Ready or ReadyCallback object). The target is triggered when all states have been marked ready.

Examples

Using the Ready constructor

 
  public class MyService implements Ready {

      private static final String STATE_INIT = "A";
      private static final String STATE_CONNECT = "B";

      private final MultiReady multiReady;
      private boolean everythingIsReady;

      public MyService() {
          multiReady = new MultiReady(this, STATE_INIT, STATE_CONNECT);
      }

      public void init() {
          // Execute code, then indicate state 1 is ready:
          multiReady.setReady(STATE_INIT);
      }

      public void connect() {
          // Execute code, then indicate state 2 is ready:
          multiReady.setReady(STATE_CONNECT);
      }

      // Implement the "Ready" interface
      @Override
      public void setReady() {
          // Called when both the init() and connect() methods have finished:
          everythingIsReady = true;
      }
  }
 
 

Using the ReadyCallback constructor

 
  public class MyService implements ReadyCallback {

      private static final int STATE_INIT = 1;
      private static final int STATE_CONNECT = 2;

      private final MultiReady multiReady;
      private boolean everythingIsReady;

      public MyService() {
          multiReady = new MultiReady(this, STATE_INIT, STATE_CONNECT);
      }

      public void init() {
          // Execute code, then indicate state 1 is ready:
          multiReady.setReady(STATE_INIT);
      }

      public void connect() {
          // Execute code, then indicate state 2 is ready:
          multiReady.setReady(STATE_CONNECT);
      }

      // Implement the "ReadyCallback" interface.
      @Override
      public void onReady() {
          // Called when both the init() and connect() methods have finished:
          everythingIsReady = true;
      }
  }
 
 
Since:
1.0
Version:
2022-09-01
See Also:
  • Constructor Details

    • MultiReady

      public MultiReady(Ready target, Object... states)
      Creates a MultiReady that will call setReady() on the target once all the states are marked ready.
      Parameters:
      target - the target setReady() to call when all states are ready
      states - the states to mark ready
    • MultiReady

      public MultiReady(ReadyCallback target, Object... states)
      Creates a MultiReady that will call the target callback once all the states are marked ready.
      Parameters:
      target - the target onReady() to call when all states are ready
      states - the states to mark ready
  • Method Details

    • setReady

      public void setReady(Object state)
      Marks a given state as ready. When the last state is marked ready, then the target is triggered.
      Parameters:
      state - the state to mark ready