diff --git a/pom.xml b/pom.xml index 089d84f..ed12693 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ no.finn.lambda lambda-companion - 0.24 + 0.26 jar ${project.groupId}:${project.artifactId} diff --git a/src/main/java/no/finn/lambdacompanion/Completion.java b/src/main/java/no/finn/lambdacompanion/Completion.java new file mode 100644 index 0000000..1bf5ac4 --- /dev/null +++ b/src/main/java/no/finn/lambdacompanion/Completion.java @@ -0,0 +1,7 @@ +package no.finn.lambdacompanion; + +/** + * A class representing a successful completion of a method that would otherwise be void + */ +public final class Completion { +} diff --git a/src/main/java/no/finn/lambdacompanion/ThrowingBiConsumer.java b/src/main/java/no/finn/lambdacompanion/ThrowingBiConsumer.java new file mode 100644 index 0000000..eeb33d8 --- /dev/null +++ b/src/main/java/no/finn/lambdacompanion/ThrowingBiConsumer.java @@ -0,0 +1,8 @@ +package no.finn.lambdacompanion; + +@FunctionalInterface +public interface ThrowingBiConsumer { + + void accept(T t, R r) throws E; + +} diff --git a/src/main/java/no/finn/lambdacompanion/Try.java b/src/main/java/no/finn/lambdacompanion/Try.java index a24bbd0..6c1b92b 100644 --- a/src/main/java/no/finn/lambdacompanion/Try.java +++ b/src/main/java/no/finn/lambdacompanion/Try.java @@ -178,10 +178,49 @@ public static Try of(ThrowingSupplier supplier) { } } + /** + * Create a Failure from an Exception + * @param Exception base exception + * @param Type of returned Try + * @return a Failure + */ public static Try failure(Exception Exception) { return new Failure<>(Exception); } + /** + * Enhance a void method to return a Try of type Completion + * @param throwingConsumer void function throwing Exception + * @param param parameter to void function + * @param type of void function param + * @return Success of Completion or a Failure + */ + public static Try completion(ThrowingConsumer throwingConsumer, U param) { + try { + throwingConsumer.accept(param); + return new Success<>(new Completion()); + } catch (Exception e) { + return Try.failure(e); + } + } + + /** + * Enhance a void method to return a Try of type Completion + * @param throwingBiConsumer void function throwing Exception + * @param firstParam first parameter to void function + * @param secondParam second parameter to void function + * @param type of void function param + * @return Success of Completion or a Failure + */ + public static Try completion(ThrowingBiConsumer throwingBiConsumer, U firstParam, R secondParam) { + try { + throwingBiConsumer.accept(firstParam, secondParam); + return new Success<>(new Completion()); + } catch (Exception e) { + return Try.failure(e); + } + } + /** * Creates one Try from a list of tries containing the same type, or the _first_ failure in the given list * @param tries List of tries