Module MaterialFX

Class ExecutionUtils

java.lang.Object
io.github.palexdev.materialfx.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 - If 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 BiConsumer action when the given ObservableValue changes.

      The consumer inputs are the oldValue and the newValue of the observable.

      If executeNow is true the consumer action is immediately executed with null as the oldValue and the current value as newValue, the listener is added anyway.

      The executeCondition BiFunction is used to specify on what conditions the action can be executed, the inputs of the function are the oldValue and newValue of the observable.

      The isOneShot flag is to specify if the added listener should be removed after the first time the observable changes.

      Type Parameters:
      T - the value type of the property
      Parameters:
      property - the observable to listen to
      consumer - the action to perform on change
      executeNow - to specify if the given action should be immediately executed
      executionCondition - to specify on what conditions the action should be executed
      isOneShot - to specify if the added listener should be removed after the first time the observable changes
    • executeWhen

      public static void executeWhen(Observable observable, Runnable action, boolean executeNow, Supplier<Boolean> executionCondition, boolean isOneShot)
      Executes the given action when the given Observable changes.

      If executeNow is true the action is immediately executed.

      Adds a listener to the observable and executes the given action every time the observable changes and the execution condition is met or just once if the isOneShot parameter is true.

      Parameters:
      observable - the observable to listen to
      action - the action to execute when the observable changes
      executeNow - to specify if the given action should be immediately executed
      executionCondition - to specify on what conditions the action should be executed
      isOneShot - to specify if the added listener should be removed after the first time the observable changes
    • 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