Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Completion to continue mapping on a void method that throws Exception #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>no.finn.lambda</groupId>
<artifactId>lambda-companion</artifactId>
<version>0.24</version>
<version>0.26</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/no/finn/lambdacompanion/Completion.java
Original file line number Diff line number Diff line change
@@ -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 {
}
8 changes: 8 additions & 0 deletions src/main/java/no/finn/lambdacompanion/ThrowingBiConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.finn.lambdacompanion;

@FunctionalInterface
public interface ThrowingBiConsumer<T, R, E extends Exception> {

void accept(T t, R r) throws E;

}
39 changes: 39 additions & 0 deletions src/main/java/no/finn/lambdacompanion/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,49 @@ public static <U> Try<U> of(ThrowingSupplier<U, ? extends Exception> supplier) {
}
}

/**
* Create a Failure from an Exception
* @param Exception base exception
* @param <U> Type of returned Try
* @return a Failure
*/
public static <U> Try<U> 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 <U> type of void function param
* @return Success of Completion or a Failure
*/
public static <U> Try<Completion> completion(ThrowingConsumer<U, ? extends Exception> 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 <U> type of void function param
* @return Success of Completion or a Failure
*/
public static <U, R> Try<Completion> completion(ThrowingBiConsumer<U, R, ? extends Exception> 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
Expand Down