Class Condition<T>

java.lang.Object
com.onenetwork.platform.tools.test.Condition<T>

public class Condition<T>
extends java.lang.Object
Utility allowing one to "await" a certain "condition" to occur.

For example, the following code will wait up to 120 seconds (2 minutes) for bizLoggerSearch to return a result containing for 100 or more rows. To achieve this, it will execute the first lambda every 1000 seconds until it returns a result with at least 100 rows.

 JSONObject response = new Condition(120000, 1000).await(() -> {
   return bizLoggerSevice.search(criteria, ctx);
 }, (resp) -> resp.getJSONArray("result").length() >= 100);
 
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static interface  Condition.Supplier<T>
    Function used to compute a value in a Condition.
    static interface  Condition.Test<T>
    Function used to validate a value in a Condition.
  • Constructor Summary

    Constructors 
    Constructor Description
    Condition​(int maxWaitMillis, int... retryIntervalMillis)  
  • Method Summary

    Modifier and Type Method Description
    T await​(Condition.Supplier<T> supplier)
    Like $await(Supplier, Test), with a default test which validates the returned value is not null.
    T await​(Condition.Supplier<T> supplier, Condition.Test<T> test)
    Executes the given supplier function repeatedly until it returns a value which passes the given test function.
    boolean isSilent()  
    void setSilent​(boolean silent)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Condition

      public Condition​(int maxWaitMillis, int... retryIntervalMillis)
      Parameters:
      maxWaitMillis - max time (in millis) to wait for the given condition in await()
      retryIntervalMillis - optionally provide the interval to sleep between failed attempts
  • Method Details

    • await

      public T await​(Condition.Supplier<T> supplier)
      Like $await(Supplier, Test), with a default test which validates the returned value is not null. Throws a RuntimeException if the condition isn't met within the maxWaitMillis.
      Parameters:
      supplier - lambda which computes the value
      Returns:
      non-null value returned by lambda
    • await

      public T await​(Condition.Supplier<T> supplier, Condition.Test<T> test)
      Executes the given supplier function repeatedly until it returns a value which passes the given test function. Throws a RuntimeException if the condition isn't met within the maxWaitMillis.
      Parameters:
      supplier - lambda which computes the value
      test - lambda which validates whether the value is acceptable, or if we should sleep and try again
      Returns:
      value returned by lambda which passed the test
    • isSilent

      public boolean isSilent()
      Returns:
      the silent
    • setSilent

      public void setSilent​(boolean silent)
      Parameters:
      silent - true to avoid generation of log statements