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 2020/10/07 08:59:07 UTC
[isis] branch 2441-auditing updated: ISIS-2441: introduces _Result
monad (will replace most uses of _Either)
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch 2441-auditing
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/2441-auditing by this push:
new 6c8f4d1 ISIS-2441: introduces _Result monad (will replace most uses of _Either)
6c8f4d1 is described below
commit 6c8f4d138ea12b265063515545e7d92c81019dd7
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Oct 7 10:58:51 2020 +0200
ISIS-2441: introduces _Result monad (will replace most uses of _Either)
---
.../apache/isis/commons/internal/base/_Result.java | 212 +++++++++++++++++++++
.../isis/commons/internal/reflection/_Reflect.java | 51 ++---
.../metamodel/commons/CanonicalParameterUtil.java | 31 ++-
.../autocomplete/AutoCompleteFacetAbstract.java | 30 ++-
.../publishing/PublisherDispatchService.java | 8 +-
.../publish/PublisherDispatchServiceDefault.java | 5 +-
6 files changed, 271 insertions(+), 66 deletions(-)
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/base/_Result.java b/commons/src/main/java/org/apache/isis/commons/internal/base/_Result.java
new file mode 100644
index 0000000..c55805b
--- /dev/null
+++ b/commons/src/main/java/org/apache/isis/commons/internal/base/_Result.java
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.commons.internal.base;
+
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.concurrent.Callable;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.function.UnaryOperator;
+
+import javax.annotation.Nullable;
+
+import lombok.AccessLevel;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+import lombok.ToString;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * A specialization of the {@link _Either} monad.
+ * <p>
+ * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
+ * These may be changed or removed without notice!
+ *
+ * @since 2.0
+ */
+@RequiredArgsConstructor(access=AccessLevel.PRIVATE, staticName="of")
+@ToString @EqualsAndHashCode
+public final class _Result<L> {
+
+ private final L value;
+ private final Throwable exception;
+ private final boolean isSuccess;
+
+ // -- FACTORIES
+
+ public static <L> _Result<L> of(@NonNull Callable<L> callable) {
+ try {
+ return success(callable.call());
+ } catch (Throwable e) {
+ return failure(e);
+ }
+ }
+
+ public static <L> _Result<L> ofNullable(@NonNull Callable<L> callable) {
+ try {
+ return successNullable(callable.call());
+ } catch (Throwable e) {
+ return failure(e);
+ }
+ }
+
+ public static <L> _Result<L> success(@NonNull L value) {
+ return of(value, null, true);
+ }
+
+ public static <L> _Result<L> successNullable(@Nullable L value) {
+ return of(value, null, true);
+ }
+
+ public static <L> _Result<L> failure(@NonNull Throwable exception) {
+ return of(null, exception, false);
+ }
+
+ // -- PREDICATES
+
+ public boolean isSuccess() {
+ return isSuccess;
+ }
+
+ public boolean isFailure() {
+ return !isSuccess();
+ }
+
+ // -- ACCESSORS
+
+ public Optional<L> value() {
+ return Optional.ofNullable(value);
+ }
+
+ public Optional<Throwable> exception() {
+ return Optional.ofNullable(exception);
+ }
+
+// public L valueIfAny() {
+// return value;
+// }
+//
+// public Throwable exceptionIfAny() {
+// return exception;
+// }
+
+ // -- PEEKING
+
+ public _Result<L> onSuccess(final @NonNull Consumer<L> valueConsumer){
+ if(isSuccess()) {
+ valueConsumer.accept(value);
+ }
+ return this;
+ }
+
+ public _Result<L> onFailure(final @NonNull Consumer<Throwable> exceptionConsumer){
+ if(isFailure()) {
+ exceptionConsumer.accept(exception);
+ }
+ return this;
+ }
+
+ // -- MAPPING
+
+ public <T> _Result<T> mapValue(final @NonNull Function<L, T> valueMapper){
+ return isSuccess()
+ ? _Result.of(()->valueMapper.apply(value))
+ : _Result.failure(exception);
+ }
+
+ public _Result<L> mapException(final @NonNull UnaryOperator<Throwable> exceptionMapper){
+ if (isSuccess()) {
+ return this;
+ }
+ try {
+ return _Result.failure(exceptionMapper.apply(exception));
+ } catch (Throwable e) {
+ return failure(e);
+ }
+ }
+
+ // -- FOLDING
+
+ public <T> T fold(
+ final @NonNull Function<L, T> valueMapper,
+ final @NonNull Function<Throwable, T> exceptionMapper){
+ return isSuccess()
+ ? valueMapper.apply(value)
+ : exceptionMapper.apply(exception);
+ }
+
+ // -- REDUCTION
+
+ @SneakyThrows
+ public L getOrThrow() {
+ if (isSuccess()) {
+ if(value==null) {
+ throw new NoSuchElementException();
+ }
+ return value;
+ }
+ throw exception;
+ }
+
+ @SneakyThrows
+ public @Nullable L getNullableOrThrow() {
+ if (isSuccess()) {
+ return value;
+ }
+ throw exception;
+ }
+
+ public L getOrDefault(final @NonNull L defaultValue) {
+ if (isSuccess()) {
+ if(value!=null) {
+ return value;
+ }
+ }
+ return defaultValue;
+ }
+
+ public @Nullable L getNullableOrDefault(final @Nullable L defaultValue) {
+ if (isSuccess()) {
+ return value;
+ }
+ return defaultValue;
+ }
+
+ public L getOrElse(final @NonNull Supplier<L> defaultValueSupplier) {
+ if (isSuccess()) {
+ if(value!=null) {
+ return value;
+ }
+ }
+ return defaultValueSupplier.get();
+ }
+
+ public @Nullable L getNullableOrElse(final @NonNull Supplier<L> defaultValueSupplier) {
+ if (isSuccess()) {
+ return value;
+ }
+ return defaultValueSupplier.get();
+ }
+
+}
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Reflect.java b/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Reflect.java
index f3c5738..e1d0fd8 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Reflect.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Reflect.java
@@ -29,7 +29,6 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -48,6 +47,7 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.apache.isis.commons.collections.Can;
import org.apache.isis.commons.internal.base._NullSafe;
+import org.apache.isis.commons.internal.base._Result;
import org.apache.isis.commons.internal.base._Strings;
import org.apache.isis.commons.internal.base._With;
import org.apache.isis.commons.internal.collections._Arrays;
@@ -469,38 +469,43 @@ public final class _Reflect {
/*sonar-ignore-off*/
}
- public static Object invokeMethodOn(
+
+ public static _Result<Object> invokeMethodOn(
@NonNull final Method method,
@NonNull final Object target,
- final Object... args) throws IllegalAccessException, InvocationTargetException {
+ final Object... args) {
/*sonar-ignore-on*/
- if(method.isAccessible()) {
- return method.invoke(target, args);
- }
- try {
- method.setAccessible(true);
- return method.invoke(target, args);
- } finally {
- method.setAccessible(false);
- }
+ return _Result.ofNullable(()->{
+ if(method.isAccessible()) {
+ return method.invoke(target, args);
+ }
+ try {
+ method.setAccessible(true);
+ return method.invoke(target, args);
+ } finally {
+ method.setAccessible(false);
+ }
+ });
/*sonar-ignore-off*/
}
- public static <T> T invokeConstructor(
+ public static <T> _Result<T> invokeConstructor(
@NonNull final Constructor<T> constructor,
- final Object... args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ final Object... args) {
/*sonar-ignore-on*/
- if(constructor.isAccessible()) {
- return constructor.newInstance(args);
- }
- try {
- constructor.setAccessible(true);
- return constructor.newInstance(args);
- } finally {
- constructor.setAccessible(false);
- }
+ return _Result.of(()->{
+ if(constructor.isAccessible()) {
+ return constructor.newInstance(args);
+ }
+ try {
+ constructor.setAccessible(true);
+ return constructor.newInstance(args);
+ } finally {
+ constructor.setAccessible(false);
+ }
+ });
/*sonar-ignore-off*/
}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/commons/CanonicalParameterUtil.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/commons/CanonicalParameterUtil.java
index c5a33c6..1b2e6bc 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/commons/CanonicalParameterUtil.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/commons/CanonicalParameterUtil.java
@@ -49,18 +49,15 @@ import lombok.val;
* By now we do this for collection parameter types List, Set, SortedSet, Collection and Arrays.
* </p>
*/
-public class CanonicalParameterUtil {
-
- public static <T> T construct(Constructor<T> constructor, Object[] executionParameters)
- throws IllegalAccessException, InvocationTargetException, InstantiationException {
+public final class CanonicalParameterUtil {
+ public static <T> T construct(Constructor<T> constructor, Object[] executionParameters) {
val adaptedExecutionParameters = preprocess(constructor, executionParameters);
- try {
- // this utility supports effective private constructors as well
- return _Reflect.invokeConstructor(constructor, adaptedExecutionParameters);
- } catch (IllegalArgumentException e) {
- throw verboseArgumentException(constructor.getParameterTypes(), adaptedExecutionParameters, e);
- }
+
+ // supports effective private constructors as well
+ return _Reflect.invokeConstructor(constructor, adaptedExecutionParameters)
+ .mapException(ex->toVerboseException(constructor.getParameterTypes(), adaptedExecutionParameters, ex))
+ .getOrThrow();
}
public static Object invoke(Method method, Object targetPojo, Object[] executionParameters)
@@ -68,11 +65,10 @@ public class CanonicalParameterUtil {
val adaptedExecutionParameters = preprocess(method, executionParameters);
- try {
- return method.invoke(targetPojo, adaptedExecutionParameters);
- } catch (IllegalArgumentException e) {
- throw verboseArgumentException(method.getParameterTypes(), adaptedExecutionParameters, e);
- }
+ // supports effective private methods as well
+ return _Reflect.invokeMethodOn(method, targetPojo, adaptedExecutionParameters)
+ .mapException(ex->toVerboseException(method.getParameterTypes(), adaptedExecutionParameters, ex))
+ .getNullableOrThrow();
}
private static Object[] preprocess(Executable executable, Object[] executionParameters) {
@@ -99,7 +95,6 @@ public class CanonicalParameterUtil {
* @param parameterType
* @return
*/
-
private static Object adapt(Object obj, Class<?> parameterType) {
if(obj==null) {
@@ -142,10 +137,10 @@ public class CanonicalParameterUtil {
return obj;
}
- private static IllegalArgumentException verboseArgumentException(
+ private static IllegalArgumentException toVerboseException(
Class<?>[] parameterTypes,
Object[] adaptedExecutionParameters,
- IllegalArgumentException e) {
+ Throwable e) {
val sb = new StringBuilder();
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/autocomplete/AutoCompleteFacetAbstract.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/autocomplete/AutoCompleteFacetAbstract.java
index 6b212db..e1ee63d 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/autocomplete/AutoCompleteFacetAbstract.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/autocomplete/AutoCompleteFacetAbstract.java
@@ -19,12 +19,12 @@
package org.apache.isis.core.metamodel.facets.object.autocomplete;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import org.apache.isis.commons.collections.Can;
+import org.apache.isis.commons.internal.reflection._Reflect;
import org.apache.isis.core.metamodel.consent.InteractionInitiatedBy;
import org.apache.isis.core.metamodel.facetapi.Facet;
import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
@@ -34,6 +34,10 @@ import org.apache.isis.core.metamodel.services.publishing.PublisherDispatchServi
import org.apache.isis.core.metamodel.spec.ManagedObject;
import org.apache.isis.core.metamodel.spec.ManagedObjects;
+import lombok.val;
+import lombok.extern.log4j.Log4j2;
+
+@Log4j2
public abstract class AutoCompleteFacetAbstract
extends FacetAbstract
implements AutoCompleteFacet {
@@ -70,23 +74,13 @@ implements AutoCompleteFacet {
final String search,
final InteractionInitiatedBy interactionInitiatedBy) {
- final ManagedObject resultAdapter =
- getPublishingServiceInternal().withPublishingSuppressed(
- new PublisherDispatchService.Block<ManagedObject>() {
- @Override
- public ManagedObject exec() {
- final Object list = invoke();
- return getObjectManager().adapt(list);
- }
-
- private Object invoke() {
- try {
- return repositoryMethod.invoke(getRepository(), search);
- } catch (IllegalAccessException | InvocationTargetException e) {
- return Collections.emptyList();
- }
- }
- });
+ val resultAdapter = getPublishingServiceInternal()
+ .withPublishingSuppressed(()->{
+ final Object list = _Reflect.invokeMethodOn(repositoryMethod, getRepository(), search)
+ .onFailure(e->log.warn("failure while executing auto-complete", e))
+ .getOrElse(Collections::emptyList);
+ return getObjectManager().adapt(list);
+ });
return ManagedObjects.VisibilityUtil.streamVisibleAdapters(resultAdapter, interactionInitiatedBy)
.collect(Can.toCan());
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/publishing/PublisherDispatchService.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/publishing/PublisherDispatchService.java
index 8c0f868..5d50d19 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/publishing/PublisherDispatchService.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/publishing/PublisherDispatchService.java
@@ -18,6 +18,8 @@
*/
package org.apache.isis.core.metamodel.services.publishing;
+import java.util.function.Supplier;
+
import org.apache.isis.applib.services.iactn.Interaction;
public interface PublisherDispatchService {
@@ -28,13 +30,9 @@ public interface PublisherDispatchService {
void publishProperty(Interaction.Execution<?,?> execution);
- interface Block<T> {
- T exec();
- }
-
/**
* Slightly hokey wormhole (anti)pattern to disable publishing for mixin associations.
*/
- <T> T withPublishingSuppressed(final Block<T> block);
+ <T> T withPublishingSuppressed(Supplier<T> block);
}
diff --git a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/publish/PublisherDispatchServiceDefault.java b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/publish/PublisherDispatchServiceDefault.java
index eb3cd88..1c44da2 100644
--- a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/publish/PublisherDispatchServiceDefault.java
+++ b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/publish/PublisherDispatchServiceDefault.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.LongAdder;
+import java.util.function.Supplier;
import javax.inject.Inject;
import javax.inject.Named;
@@ -162,10 +163,10 @@ public class PublisherDispatchServiceDefault implements PublisherDispatchService
}
@Override
- public <T> T withPublishingSuppressed(final Block<T> block) {
+ public <T> T withPublishingSuppressed(final Supplier<T> block) {
try {
suppressionRequestCounter.increment();
- return block.exec();
+ return block.get();
} finally {
suppressionRequestCounter.decrement();
}