diff --git a/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/function/ExExecutor.java b/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/function/ExExecutor.java index 229648db1..7bf629929 100644 --- a/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/function/ExExecutor.java +++ b/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/function/ExExecutor.java @@ -37,8 +37,8 @@ private static RuntimeException wrap(@NonNull Throwable e, @NonNull String messa /** * Execute the runnable and * * @@ -63,7 +63,8 @@ public static void executeRunnable(@NonNull ExRunnable r, @NonNull String messag * @param exceptionConverter * function to convert checked exceptions into runtime exceptions */ - public static void executeRunnable(@NonNull ExRunnable r, @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { + public static void executeRunnable(@NonNull ExRunnable r, + @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { try { r.wrappedRun(); } catch (Throwable e) { @@ -76,8 +77,8 @@ public static void executeRunnable(@NonNull ExRunnable r, @NonNull Function<@Non } /** - * Execute the runnable and log the exception with the given logger but - * don't rethrow it + * Execute the runnable and log the exception with the given logger but don't + * rethrow it * * @param r * the runnable @@ -111,8 +112,8 @@ public static void executeRunnable(@NonNull ExRunnable r) { /** * Execute the supplier and * * @@ -139,7 +140,8 @@ public static void executeRunnable(@NonNull ExRunnable r) { * function to convert checked exceptions into runtime exceptions * @return the value provided by the supplier */ - public static <@Nullable V> Optional executeSupplier(@NonNull ExSupplier r, @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { + public static <@Nullable V> Optional executeSupplier(@NonNull ExSupplier r, + @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { try { return Optional.ofNullable(r.wrappedGet()); } catch (Throwable e) { @@ -161,19 +163,38 @@ public static void executeRunnable(@NonNull ExRunnable r) { * the exception handler * @return the value */ - public static <@Nullable V> Optional executeSupplierOrDefault(@NonNull ExSupplier r, Function exceptionHandler) { + public static <@Nullable V> Optional executeSupplierOrDefault(@NonNull ExSupplier r, + Function exceptionHandler) { try { return Optional.of(r.wrappedGet()); } catch (Throwable t) { + LoggerCreator.createLogger(ExExecutor.class).error("Failed to execute supplier", t); //$NON-NLS-1$ return Optional.ofNullable(exceptionHandler.apply(t)); } } + /** + * Execute the supplier and if an exception occurs return an empty + * {@link Optional} + * + * @param r + * the supplier + * @return the value + * @since 3.1.0 + */ + public static <@Nullable V> Optional accept(@NonNull ExSupplier r) { + try { + return Optional.of(r.wrappedGet()); + } catch (Throwable t) { + return Optional.empty(); + } + } + /** * Execute the consumer and *
    - *
  • wrap checked exceptions into a {@link RuntimeException} using the - * given message
  • + *
  • wrap checked exceptions into a {@link RuntimeException} using the given + * message
  • *
  • re-throw runtime exceptions as is
  • *
* @@ -202,7 +223,8 @@ public static void executeRunnable(@NonNull ExRunnable r) { * @param exceptionConverter * function to convert checked exceptions into runtime exceptions */ - public static <@Nullable T> void executeConsumer(T value, @NonNull ExConsumer r, @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { + public static <@Nullable T> void executeConsumer(T value, @NonNull ExConsumer r, + @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { try { r.wrappedAccept(value); } catch (Throwable e) { @@ -214,11 +236,28 @@ public static void executeRunnable(@NonNull ExRunnable r) { } } + /** + * Execute the consumer and log the exception if one occurs + * + * @param value + * the value passed to the consumer + * @param r + * the consumer + * @since 3.1.0 + */ + public static <@Nullable T> void get(T value, @NonNull ExConsumer r) { + try { + r.wrappedAccept(value); + } catch (Throwable e) { + LoggerCreator.createLogger(ExExecutor.class).error("Failed to execute consumer", e); //$NON-NLS-1$ + } + } + /** * Execute the function and *
    - *
  • wrap checked exceptions into a {@link RuntimeException} using the - * given message
  • + *
  • wrap checked exceptions into a {@link RuntimeException} using the given + * message
  • *
  • re-throw runtime exceptions as is
  • *
* @@ -230,7 +269,8 @@ public static void executeRunnable(@NonNull ExRunnable r) { * the message to use * @return the return value of the function */ - public static <@Nullable V, @Nullable R> Optional executeFunction(V value, @NonNull ExFunction r, @NonNull String message) { + public static <@Nullable V, @Nullable R> Optional executeFunction(V value, @NonNull ExFunction r, + @NonNull String message) { return executeFunction(value, r, (e) -> wrap(e, message)); } @@ -245,7 +285,8 @@ public static void executeRunnable(@NonNull ExRunnable r) { * handle an exception and return a value * @return the value returned by the function or the exception handler */ - public static <@Nullable V, @Nullable R> Optional executeFunctionOrDefault(V value, @NonNull ExFunction r, BiFunction exceptionHandler) { + public static <@Nullable V, @Nullable R> Optional executeFunctionOrDefault(V value, @NonNull ExFunction r, + BiFunction exceptionHandler) { try { return Optional.of(r.wrappedApply(value)); } catch (Throwable t) { @@ -253,6 +294,26 @@ public static void executeRunnable(@NonNull ExRunnable r) { } } + /** + * Execute the function and in case of an exception return an empty + * {@link Optional} + * + * @param value + * the value to pass to the function + * @param f + * the function + * @return the value returned by the function wrapped in an optional + * @since 3.1.0 + */ + public static <@Nullable V, @Nullable R> Optional apply(V value, @NonNull ExFunction f) { + try { + return Optional.of(f.wrappedApply(value)); + } catch (Throwable t) { + LoggerCreator.createLogger(ExExecutor.class).error("Failed to execute function", t); //$NON-NLS-1$ + return Optional.empty(); + } + } + /** * Execute the function and *
    @@ -268,7 +329,8 @@ public static void executeRunnable(@NonNull ExRunnable r) { * function to convert checked exceptions into runtime exceptions * @return the return value of the function */ - public static <@Nullable V, @Nullable R> Optional executeFunction(V value, @NonNull ExFunction r, @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { + public static <@Nullable V, @Nullable R> Optional executeFunction(V value, @NonNull ExFunction r, + @NonNull Function<@NonNull Throwable, @NonNull RuntimeException> exceptionConverter) { try { return Optional.ofNullable(r.wrappedApply(value)); } catch (Throwable e) {