diff --git a/src/main/java/com/zackehh/jackson/Jive.java b/src/main/java/com/zackehh/jackson/Jive.java index e81130c..c22799c 100644 --- a/src/main/java/com/zackehh/jackson/Jive.java +++ b/src/main/java/com/zackehh/jackson/Jive.java @@ -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; @@ -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; @@ -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 @@ -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 the type of the block return value. * @return an Optional containing a potential result. */ - public static Optional execute(ObjectMapper mapper, SafeExecution execution) { + public static Optional execute(Callable 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 the type of the block return value. + * @return an Optional containing a potential result. + */ + public static Optional execute(ObjectMapper mapper, SafeExecution execution) { + return execute(() -> execution.apply(mapper)); + } + /** * Determines whether all JsonNodes within an ArrayNode fit * a provided Predicate condition.