Class ExecutionUtils

java.lang.Object
io.github.palexdev.virtualizedfx.utils.ExecutionUtils

public class ExecutionUtils extends Object
Utils class to help with concurrency and callables.
  • Constructor Details

    • ExecutionUtils

      public ExecutionUtils()
  • Method Details

    • runAndWaitEx

      public static void runAndWaitEx(Runnable run) throws InterruptedException, ExecutionException
      Invokes a Runnable on the JavaFX Application Thread and waits for it to finish.
      Parameters:
      run - The Runnable that has to be called on JFX thread.
      Throws:
      InterruptedException - f the execution is interrupted.
      ExecutionException - If an exception is occurred in the run method of the Runnable
    • runAndWait

      public static void runAndWait(Runnable runnable)
      Calls runAndWaitEx(Runnable) but consumes/ignores any thrown exception.
    • tryCallableAndPrint

      public static <V> V tryCallableAndPrint(Callable<V> callable)
      Tries to execute the given callable and prints the stacktrace in case of exception.
      Returns:
      the callable result or null in case of exception
    • tryCallableAndIgnore

      public static <V> V tryCallableAndIgnore(Callable<V> callable)
      Tries to execute the given callable but ignores the exception in case of fail.
      Returns:
      the callable result or null in case of exception
    • executeWhen

      public static <T> void executeWhen(ObservableValue<? extends T> property, BiConsumer<T,​T> consumer, boolean executeNow, BiFunction<T,​T,​Boolean> executionCondition, boolean isOneShot)
      Executes the given action (as a consumer) when the given ObservableValue changes.

      If executeNow is true the action will be immediately run, the first value will be null, the second will be the current observable's value.

      When the observable changes the executionCondition is evaluated and if it's true the action will be run with two arguments passed to the consumer, the first is the observable's oldValue, the second is the observable's newValue.

      If isOneShot is true the listener will be removed from the observable as soon as it changes and executionCondition is true.

      Parameters:
      property - the observable to add a listener to
      consumer - the action as a consumer to offer both the oldValue and newValue
      executeNow - to specify if the given action should be immediately executed if the expression is already true
      executionCondition - to specify when the action should be executed
      isOneShot - to specify if the added listener should be removed after the first time the expression becomes true
    • executeWhen

      public static void executeWhen(BooleanExpression booleanExpression, Runnable action, boolean executeNow, boolean addListenerIfTrue, boolean isOneShot)
      Executes the given action if the given expression and the executeNow parameter are true.

      If the given expression is false or the addListenerIfTrue parameter is true, adds a listener to the expression and executes the given action every time the property becomes true or just once if the isOneShot parameter is true.

      Parameters:
      booleanExpression - the expression to evaluate
      action - the action to execute when the expression is true
      executeNow - to specify if the given action should be immediately executed if the expression is already true
      addListenerIfTrue - to specify if the listener should be added anyway to the expression even if it is already true
      isOneShot - to specify if the added listener should be removed after the first time the expression becomes true
    • executeWhen

      public static void executeWhen(BooleanExpression booleanExpression, Runnable trueAction, Runnable falseAction, boolean executeTrueNow, boolean executeFalseNow, boolean addListenerIfTrue, boolean isOneShot)
      Executes the given truAction if the given expression and the executeTrueNow parameter are true.

      Executes the given falseAction if the given expression is false and the executeFalseNow parameter is true.

      If the given expression is false or the addListenerIfTrue parameter is true, adds a listener to the expression and executes the given trueAction every time the property becomes true, and the given falseAction every time the property becomes false, or just once if the isOneShot parameter is true.

      Parameters:
      booleanExpression - the expression to evaluate
      trueAction - the action to execute when the expression is true
      falseAction - the action to execute when the expression is false
      executeTrueNow - to specify if the given trueAction should be immediately executed if the expression is already true
      executeFalseNow - to specify if the given falseAction should be immediately executed if the expression is already false
      addListenerIfTrue - to specify if the listener should be added anyway to the expression even if it is already true
      isOneShot - to specify if the added listener should be removed after the first time the expression becomes true