You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/05/24 11:25:48 UTC
[isis] branch master updated: ISIS-1841 Adds FunctionalInterface
variants allowing checked Exceptions
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new d378d05 ISIS-1841 Adds FunctionalInterface variants allowing checked Exceptions
d378d05 is described below
commit d378d0501705512cb18dae158c3550969c513812
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu May 24 13:25:42 2018 +0200
ISIS-1841 Adds FunctionalInterface variants allowing checked Exceptions
- CheckedFunction
- CheckedConsumer
- CheckedRunnable
- CheckedSupplier
---
.../isis/applib/NonRecoverableException.java | 33 +++++
.../commons/internal/exceptions/_Exceptions.java | 46 ++++++-
.../commons/internal/functions/_Functions.java | 133 ++++++++++++++++++++-
.../components/tree/IsisToWicketTreeAdapter.java | 2 +-
4 files changed, 211 insertions(+), 3 deletions(-)
diff --git a/core/applib/src/main/java/org/apache/isis/applib/NonRecoverableException.java b/core/applib/src/main/java/org/apache/isis/applib/NonRecoverableException.java
index 8620703..ee8af1f 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/NonRecoverableException.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/NonRecoverableException.java
@@ -22,6 +22,7 @@ package org.apache.isis.applib;
import org.apache.isis.applib.services.exceprecog.TranslatableException;
import org.apache.isis.applib.services.i18n.TranslatableString;
import org.apache.isis.commons.internal.base._Strings;
+import org.apache.isis.commons.internal.functions._Functions;
/**
* Indicates that an unexpected, non-recoverable (fatal) exception has occurred within
@@ -106,4 +107,36 @@ public class NonRecoverableException extends RuntimeException implements Transla
public String getTranslationContext() {
return translationContext;
}
+
+ // -- SHORTCUTS
+
+ /**
+ * <p><pre>
+ * Path path = ...
+ *
+ * ## OLD
+ *
+ * try {
+ * Files.createDirectories(path);
+ * } catch (IOException e) {
+ * throw new NonRecoverableException(e);
+ * }
+ *
+ * ## NEW
+ *
+ * NonRecoverableException.tryRun(()->Files.createDirectories(path));
+ *
+ * </pre></p>
+ *
+ * @param checkedRunnable
+ */
+ public static void tryRun(_Functions.CheckedRunnable checkedRunnable) {
+ try {
+ checkedRunnable.run();
+ } catch (Exception cause) {
+ throw new NonRecoverableException(cause);
+ }
+ }
+
+
}
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java b/core/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java
index 4d1c557..ceea19e 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java
@@ -22,12 +22,15 @@ package org.apache.isis.commons.internal.exceptions;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
+import java.util.function.Consumer;
+import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.isis.commons.internal.base._NullSafe;
import org.apache.isis.commons.internal.collections._Lists;
+import org.apache.isis.commons.internal.functions._Functions;
/**
* <h1>- internal use only -</h1>
@@ -76,7 +79,7 @@ public final class _Exceptions {
}
/**
- * Used, to hide from the compiler the fact, that this call always throws.
+ * Used to hide from the compiler the fact, that this call always throws.
*
* <pre>{
* throw notImplemented();
@@ -135,6 +138,47 @@ public final class _Exceptions {
}
// --
+
+ /**
+ * [ahuber] Experimental, remove if it adds no value.
+ */
+ public static class TryContext {
+
+ private final Function<Exception, ? extends RuntimeException> toUnchecked;
+
+ public TryContext(Function<Exception, ? extends RuntimeException> toUnchecked) {
+ this.toUnchecked = toUnchecked;
+ }
+
+ // -- SHORTCUTS (RUNNABLE)
+
+ public Runnable uncheckedRunnable(_Functions.CheckedRunnable checkedRunnable) {
+ return checkedRunnable.toUnchecked(toUnchecked);
+ }
+
+ public void tryRun(_Functions.CheckedRunnable checkedRunnable) {
+ uncheckedRunnable(checkedRunnable).run();
+ }
+
+ // -- SHORTCUTS (FUNCTION)
+
+ public <T, R> Function<T, R> uncheckedFunction(_Functions.CheckedFunction<T, R> checkedFunction) {
+ return checkedFunction.toUnchecked(toUnchecked);
+ }
+
+ public <T, R> R tryApply(T obj, _Functions.CheckedFunction<T, R> checkedFunction) {
+ return uncheckedFunction(checkedFunction).apply(obj);
+ }
+
+ // -- SHORTCUTS (CONSUMER)
+ public <T> Consumer<T> uncheckedConsumer(_Functions.CheckedConsumer<T> checkedConsumer) {
+ return checkedConsumer.toUnchecked(toUnchecked);
+ }
+
+ public <T> void tryAccept(T obj, _Functions.CheckedConsumer<T> checkedConsumer) {
+ uncheckedConsumer(checkedConsumer).accept(obj);
+ }
+ }
}
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Functions.java b/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Functions.java
index d5bfb51..d595f85 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Functions.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Functions.java
@@ -18,7 +18,9 @@
*/
package org.apache.isis.commons.internal.functions;
+import java.util.function.Consumer;
import java.util.function.Function;
+import java.util.function.Supplier;
/**
* <h1>- internal use only -</h1>
@@ -34,6 +36,8 @@ import java.util.function.Function;
*/
public class _Functions {
+ // -- INDEX AWARE
+
@FunctionalInterface
public interface IndexAwareFunction<T, R> {
public R apply(int index, T t);
@@ -45,9 +49,136 @@ public class _Functions {
* @param indexAwareFunction
* @return
*/
- public static <T, R> Function<T, R> indexAwareFunctionToFunction(IndexAwareFunction<T, R> indexAwareFunction){
+ public static <T, R> Function<T, R> indexAwareToFunction(IndexAwareFunction<T, R> indexAwareFunction){
return new _Functions_IndexAwareFunctionAdapter<T, R>(indexAwareFunction);
}
+ // -- CHECKED EXCEPTION ADAPTERS (FUNCTION)
+
+ /**
+ *
+ * Similar to {@link Function}, but allows checked exceptions to be thrown.
+ *
+ * @param <T>
+ * @param <R>
+ */
+ @FunctionalInterface
+ public interface CheckedFunction<T, R> {
+
+ R apply(T t) throws Exception;
+
+ default <U extends RuntimeException> Function<T, R> toUnchecked(Function<Exception, U> toUncheckedException) {
+ return uncheckedFunction(this, toUncheckedException);
+ }
+
+ }
+
+ public static <T, R, U extends RuntimeException> Function<T, R> uncheckedFunction(
+ CheckedFunction<T, R> checkedFunction,
+ Function<Exception, U> toUncheckedException) {
+ return t->{
+ try {
+ return checkedFunction.apply(t);
+ } catch (Exception e) {
+ throw toUncheckedException.apply(e);
+ }
+ };
+ }
+
+ // -- CHECKED EXCEPTION ADAPTERS (RUNNABLE)
+
+ /**
+ *
+ * Similar to {@link Runnable}, but allows checked exceptions to be thrown.
+ *
+ */
+ @FunctionalInterface
+ public interface CheckedRunnable {
+
+ void run() throws Exception;
+
+ default <U extends RuntimeException> Runnable toUnchecked(Function<Exception, U> toUncheckedException) {
+ return uncheckedRunnable(this, toUncheckedException);
+ }
+
+ }
+
+ public static <U extends RuntimeException> Runnable uncheckedRunnable(
+ CheckedRunnable checkedRunnable,
+ Function<Exception, U> toUncheckedException) {
+ return ()->{
+ try {
+ checkedRunnable.run();
+ } catch (Exception e) {
+ throw toUncheckedException.apply(e);
+ }
+ };
+ }
+
+ // -- CHECKED EXCEPTION ADAPTERS (CONSUMER)
+
+ /**
+ *
+ * Similar to {@link Consumer}, but allows checked exceptions to be thrown.
+ *
+ * @param <T>
+ */
+ @FunctionalInterface
+ public interface CheckedConsumer<T> {
+
+ void accept(T t) throws Exception;
+
+ default <U extends RuntimeException> Consumer<T> toUnchecked(Function<Exception, U> toUncheckedException) {
+ return uncheckedConsumer(this, toUncheckedException);
+ }
+
+ }
+
+ public static <T, U extends RuntimeException> Consumer<T> uncheckedConsumer(
+ CheckedConsumer<T> checkedConsumer,
+ Function<Exception, U> toUncheckedException) {
+ return t->{
+ try {
+ checkedConsumer.accept(t);
+ } catch (Exception e) {
+ throw toUncheckedException.apply(e);
+ }
+ };
+ }
+
+ // -- CHECKED EXCEPTION ADAPTERS (SUPPLIER)
+
+ /**
+ *
+ * Similar to {@link Function}, but allows checked exceptions to be thrown.
+ *
+ * @param <T>
+ * @param <R>
+ */
+ @FunctionalInterface
+ public interface CheckedSupplier<T> {
+
+ T get() throws Exception;
+
+ default <U extends RuntimeException> Supplier<T> toUnchecked(Function<Exception, U> toUncheckedException) {
+ return uncheckedSupplier(this, toUncheckedException);
+ }
+
+ }
+
+ public static <T, U extends RuntimeException> Supplier<T> uncheckedSupplier(
+ CheckedSupplier<T> checkedSupplier,
+ Function<Exception, U> toUncheckedException) {
+ return ()->{
+ try {
+ return checkedSupplier.get();
+ } catch (Exception e) {
+ throw toUncheckedException.apply(e);
+ }
+ };
+ }
+
+ // --
+
}
diff --git a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java
index cdbb86b..46cf5d2 100644
--- a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java
+++ b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java
@@ -261,7 +261,7 @@ class IsisToWicketTreeAdapter {
}
private Function<Object, TreeModel> newPojoToTreeModelMapper(TreeModel parent) {
- return _Functions.indexAwareFunctionToFunction((indexWithinSiblings, pojo)->
+ return _Functions.indexAwareToFunction((indexWithinSiblings, pojo)->
wrap(pojo, parent.getTreePath().append(indexWithinSiblings)));
}
--
To stop receiving notification emails like this one, please contact
ahuber@apache.org.