Skip to content

Commit

Permalink
Allow executing without a mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed May 1, 2017
1 parent 61ae617 commit 3721c02
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/main/java/com/zackehh/jackson/Jive.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.zackehh.jackson.scope.SafeExecution;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractMap;
Expand All @@ -30,6 +29,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
Expand Down Expand Up @@ -110,7 +110,7 @@ public static ArrayNode drop(ArrayNode node, int count) {
}

/**
* Executes a scoped function with the provided ObjectMapper.
* Executes a scoped function with caught Exceptions.
*
* Any thrown IOException instances will be caught and will
* return an empty Optional to the user. In the case of a
Expand All @@ -120,19 +120,38 @@ public static ArrayNode drop(ArrayNode node, int count) {
* This function can be used to remove the need to manually
* handle exceptions when calling ObjectMapper functions.
*
* @param mapper the ObjectMapper instance to execute with.
* @param execution the execution implementation to call.
* @param <T> the type of the block return value.
* @return an Optional containing a potential result.
*/
public static <T> Optional<T> execute(ObjectMapper mapper, SafeExecution<T> execution) {
public static <T> Optional<T> execute(Callable<T> execution) {
try {
return Optional.ofNullable(execution.apply(mapper));
return Optional.ofNullable(execution.call());
} catch(Exception e) {
return Optional.empty();
}
}

/**
* Executes a scoped function with the provided ObjectMapper.
*
* Any thrown Exception instances will be caught and will
* return an empty Optional to the user. In the case of a
* successful execution, the return value of the called block
* will be wrapped into an Optional and returned.
*
* This function can be used to remove the need to manually
* handle exceptions when calling ObjectMapper functions.
*
* @param mapper the ObjectMapper instance to execute with.
* @param execution the execution implementation to call.
* @param <T> the type of the block return value.
* @return an Optional containing a potential result.
*/
public static <T> Optional<T> execute(ObjectMapper mapper, SafeExecution<T> execution) {
return execute(() -> execution.apply(mapper));
}

/**
* Determines whether all JsonNodes within an ArrayNode fit
* a provided Predicate condition.
Expand Down

0 comments on commit 3721c02

Please sign in to comment.