diff --git a/java/code/src/com/suse/utils/Exceptions.java b/java/code/src/com/suse/utils/Exceptions.java index 33f187436deb..4a9e61546af4 100644 --- a/java/code/src/com/suse/utils/Exceptions.java +++ b/java/code/src/com/suse/utils/Exceptions.java @@ -20,28 +20,45 @@ public final class Exceptions { /** * Represents an operation that does not accept inputs and returns no result, but may throw an exception. + * @param type of exception */ @FunctionalInterface - public interface ThrowingOperation { + public interface ThrowingRunnable { /** * Performs this operation. - * @throws Exception when an error occurs during the execution + * @throws E when an error occurs during the execution */ - void execute() throws Exception; + void run() throws E; } /** * Represents an operation that does not accept inputs and returns a value, but may throw an exception. * @param the type of the return value + * @param type of exception */ @FunctionalInterface - public interface ThrowingSupplier { + public interface ThrowingSupplier { /** * Performs this operation. * @return the result of the operation - * @throws Exception when an error occurs during the execution + * @throws E when an error occurs during the execution */ - T execute() throws Exception; + T get() throws E; + } + + /** + * Represents an operation that accepts a single input argument and returns no result, but may throw an exception. + * @param the type of the return value + * @param type of exception + */ + @FunctionalInterface + public interface ThrowingConsumer { + /** + * Performs this operation. + * @param value the value to consume + * @throws E when an error occurs during the execution + */ + void accept(T value) throws E; } private Exceptions() { @@ -51,11 +68,12 @@ private Exceptions() { /** * Executes an operation and returns the exception if occurred during the execution. * @param operation the operation to perform + * @param type of exception * @return an optional wrapping the exception, or empty if the operation completes successfully. */ - public static Optional handleByReturning(ThrowingOperation operation) { + public static Optional handleByReturning(ThrowingRunnable operation) { try { - operation.execute(); + operation.run(); return Optional.empty(); } catch (Exception ex) { @@ -66,11 +84,12 @@ public static Optional handleByReturning(ThrowingOperation /** * Executes an operation and wraps any exception into a runtime exception. * @param operation the operation to perform + * @param type of exception * @throws RuntimeException if an exception occurs during the operation. */ - public static void handleByWrapping(ThrowingOperation operation) { + public static void handleByWrapping(ThrowingRunnable operation) { handleByWrapping(() -> { - operation.execute(); + operation.run(); return null; }); } @@ -79,12 +98,13 @@ public static void handleByWrapping(ThrowingOperation operation) { * Executes an operation and wraps any exception into a runtime exception. * @param operation the operation to perform * @param the type of the return value of the operation + * @param type of exception * @return the result value of the operation * @throws RuntimeException if an exception occurs during the operation. */ - public static T handleByWrapping(ThrowingSupplier operation) { + public static T handleByWrapping(ThrowingSupplier operation) { try { - return operation.execute(); + return operation.get(); } catch (Exception ex) { throw new RuntimeException("Unable to execute operation", ex);