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 2022/04/06 09:14:30 UTC

[isis] branch master updated: ISIS-2993: purge Result

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 cd135e5f90 ISIS-2993: purge Result<T>
cd135e5f90 is described below

commit cd135e5f90cfddc4e1307ad9e3696f13b1017d73
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Apr 6 11:14:23 2022 +0200

    ISIS-2993: purge Result<T>
---
 .../org/apache/isis/commons/functional/Result.java | 230 ---------------------
 .../org/apache/isis/commons/functional/Try.java    |   2 +-
 .../isis/core/metamodel/facets/Evaluators.java     |  29 ++-
 .../all/i8n/imperative/HasImperativeText.java      |   6 +-
 .../imperative/HasImperativeTextFacetAbstract.java |   4 +-
 .../isis/core/metamodel/spec/ManagedObjects.java   |   8 +-
 6 files changed, 30 insertions(+), 249 deletions(-)

diff --git a/commons/src/main/java/org/apache/isis/commons/functional/Result.java b/commons/src/main/java/org/apache/isis/commons/functional/Result.java
deleted file mode 100644
index 5177f2fe58..0000000000
--- a/commons/src/main/java/org/apache/isis/commons/functional/Result.java
+++ /dev/null
@@ -1,230 +0,0 @@
-/*
- *  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.functional;
-
-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 org.springframework.lang.Nullable;
-
-import lombok.AccessLevel;
-import lombok.EqualsAndHashCode;
-import lombok.NonNull;
-import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
-import lombok.ToString;
-import lombok.val;
-
-/**
- * The {@link Result} type represents a value of one of two possible types (a disjoint union).
- * The data constructors {@link Result#success(Object)} and {@link Result#failure(Throwable)}
- * represent the two possible values.
- *
- * @since 2.0 {@index}
- */
-@Deprecated(forRemoval = true, since = "2.0.0-M7")
-@RequiredArgsConstructor(access=AccessLevel.PRIVATE, staticName="of")
-@ToString @EqualsAndHashCode
-public final class Result<L> {
-
-    private final L value;
-    private final Throwable throwable;
-    private final boolean isSuccess;
-
-    // -- FACTORIES
-
-    public static <L> Result<L> of(final @NonNull Callable<L> callable) {
-        try {
-            return success(callable.call());
-        } catch (Throwable e) {
-            return failure(e);
-        }
-    }
-
-    public static <L> Result<L> success(final @Nullable L value) {
-        return of(value, null, true);
-    }
-
-    public static <L> Result<L> failure(final @NonNull Throwable throwable) {
-        return of(null, throwable, false);
-    }
-
-    // -- FACTORY SHORTCUTS
-
-    public static <L> Result<L> failure(final @NonNull String message) {
-        return failure(new RuntimeException(message));
-    }
-
-    public static <L> Result<L> failure(final @NonNull String message, final @NonNull Throwable cause) {
-        return failure(new RuntimeException(message, cause));
-    }
-
-    // -- PREDICATES
-
-    public boolean isSuccess() {
-        return isSuccess;
-    }
-
-    public boolean isFailure() {
-        return !isSuccess();
-    }
-
-    // -- ACCESSORS
-
-    public Optional<L> getValue() {
-        return Optional.ofNullable(value);
-    }
-
-    public Optional<Throwable> getFailure() {
-        return Optional.ofNullable(throwable);
-    }
-
-    // -- PEEKING
-
-    public Result<L> ifSuccess(final @NonNull Consumer<L> valueConsumer){
-        if(isSuccess()) {
-            valueConsumer.accept(value);
-        }
-        return this;
-    }
-
-    public Result<L> ifSuccessAndValuePresent(final @NonNull Consumer<L> valueConsumer){
-        getValue().ifPresent(valueConsumer::accept);
-        return this;
-    }
-
-    public Result<L> ifFailure(final @NonNull Consumer<Throwable> exceptionConsumer){
-        if(isFailure()) {
-            exceptionConsumer.accept(throwable);
-        }
-        return this;
-    }
-
-    // -- MAP NULL TO FAILURE
-
-    public <E extends Throwable> Result<L> mapSuccessWithEmptyValueToFailure(
-            final @NonNull Supplier<E> onNullValue){
-        return isSuccess()
-                && value==null
-                ? Result.failure(onNullValue.get())
-                : this;
-    }
-
-    public <E extends Throwable> Result<L> mapSuccessWithEmptyValueToNoSuchElement(){
-        return mapSuccessWithEmptyValueToFailure(NoSuchElementException::new);
-    }
-
-    // -- MAPPING
-
-    public <T> Result<T> mapSuccess(final @NonNull Function<L, T> successMapper){
-        return isSuccess()
-                ? Result.of(()->successMapper.apply(value))
-                : Result.failure(throwable);
-    }
-
-    public Result<L> mapFailure(final @NonNull UnaryOperator<Throwable> failureMapper){
-        if (isSuccess()) {
-            return this;
-        }
-        try {
-            return Result.failure(failureMapper.apply(throwable));
-        } catch (Throwable e) {
-            return failure(e);
-        }
-    }
-
-    // -- FOLDING
-
-    public <T> T fold(
-            final @NonNull Function<L, T> successMapper,
-            final @NonNull Function<Throwable, T> failureMapper){
-        return isSuccess()
-                ? successMapper.apply(value)
-                : failureMapper.apply(throwable);
-    }
-
-    // -- EXTRACTION
-
-    @SneakyThrows
-    public L presentElseFail() {
-        if (isSuccess()) {
-            if(value==null) {
-                throw new NoSuchElementException();
-            }
-            return value;
-        }
-        throw throwable;
-    }
-
-    @SneakyThrows
-    public Optional<L> optionalElseFail() {
-        if (isSuccess()) {
-            return getValue();
-        }
-        throw throwable;
-    }
-
-    @SneakyThrows
-    public L presentElseThrow(final @NonNull UnaryOperator<Throwable> toThrowable) {
-        if (isSuccess()) {
-            if(value==null) {
-                throw toThrowable.apply(new NoSuchElementException());
-            }
-            return value;
-        }
-        throw toThrowable.apply(throwable);
-    }
-
-    @SneakyThrows
-    public Optional<L> optionalElseThrow(final @NonNull UnaryOperator<Throwable> toThrowable) {
-        if (isSuccess()) {
-            return getValue();
-        }
-        throw toThrowable.apply(throwable);
-    }
-
-    public L presentElse(final @NonNull L defaultValue) {
-        if (isSuccess()) {
-            if(value!=null) {
-                return value;
-            }
-        }
-        return defaultValue;
-    }
-
-    public L presentElseGet(final @NonNull Supplier<L> defaultValueSupplier) {
-        if (isSuccess()) {
-            if(value!=null) {
-                return value;
-            }
-        }
-        val defaultValue = defaultValueSupplier.get();
-        if(defaultValue!=null) {
-            return defaultValue;
-        }
-        throw new NoSuchElementException();
-    }
-
-
-}
diff --git a/commons/src/main/java/org/apache/isis/commons/functional/Try.java b/commons/src/main/java/org/apache/isis/commons/functional/Try.java
index 1d5bbfc50b..df78305561 100644
--- a/commons/src/main/java/org/apache/isis/commons/functional/Try.java
+++ b/commons/src/main/java/org/apache/isis/commons/functional/Try.java
@@ -128,7 +128,7 @@ public interface Try<T> {
     // -- FOLDING
 
     /**
-     * Folds the contained {@code value} or {@code failure} to a new value of type {@code R}
+     * Maps the contained {@code value} or {@code failure} to a new value of type {@code R}
      * using according mapping function {@code successMapper} or {@code failureMapper}.
      */
     <R> R fold(
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/Evaluators.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/Evaluators.java
index 420f198f37..2d1f6498fc 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/Evaluators.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/Evaluators.java
@@ -29,7 +29,7 @@ import java.util.stream.Stream;
 
 import org.apache.isis.applib.exceptions.unrecoverable.MetaModelException;
 import org.apache.isis.commons.collections.Can;
-import org.apache.isis.commons.functional.Result;
+import org.apache.isis.commons.functional.Try;
 import org.apache.isis.commons.internal.base._NullSafe;
 import org.apache.isis.commons.internal.reflection._ClassCache;
 import org.apache.isis.commons.internal.reflection._Reflect;
@@ -126,7 +126,7 @@ public final class Evaluators  {
     implements Evaluator {
 
         @Getter(lazy = true, value = AccessLevel.PRIVATE)
-        private final Result<MethodHandle> methodHandleRef = Result.of(this::createMethodHandle);
+        private final Try<MethodHandle> methodHandleRef = Try.call(this::createMethodHandle);
 
         protected abstract MethodHandle createMethodHandle() throws IllegalAccessException;
 
@@ -135,13 +135,10 @@ public final class Evaluators  {
         public Object value(final Object obj) {
 
             val mh = getMethodHandleRef()
-            .optionalElseThrow(ex->
-                new MetaModelException("failed to create a method handle for " + name(), ex))
-            .orElse(null);
-
-            if(mh==null) {
-                return null;
-            }
+            .mapFailure(this::failure)
+            .ifFailureFail()
+            .getValue()
+            .orElseThrow(this::failure);
 
             try {
                 return mh.invoke(obj);
@@ -151,6 +148,20 @@ public final class Evaluators  {
 
         }
 
+        // -- HELPER
+
+        private String failureMessage() {
+            return "failed to create a method handle for " + name();
+        }
+
+        private MetaModelException failure(final Throwable cause) {
+            return new MetaModelException(failureMessage(), cause);
+        }
+
+        private MetaModelException failure() {
+            return new MetaModelException(failureMessage());
+        }
+
     }
 
     @RequiredArgsConstructor
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeText.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeText.java
index fcf4a55d81..aca0120359 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeText.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeText.java
@@ -21,7 +21,7 @@ package org.apache.isis.core.metamodel.facets.all.i8n.imperative;
 import org.springframework.lang.Nullable;
 
 import org.apache.isis.applib.services.i18n.TranslatableString;
-import org.apache.isis.commons.functional.Result;
+import org.apache.isis.commons.functional.Try;
 import org.apache.isis.core.metamodel.spec.ManagedObject;
 
 public interface HasImperativeText {
@@ -34,11 +34,11 @@ public interface HasImperativeText {
      * <p>
      * eg. title, name, description
      */
-    Result<String> text(ManagedObject object);
+    Try<String> text(ManagedObject object);
 
     @Nullable
     default String textElseNull(final ManagedObject object) {
-        return text(object).optionalElseFail().orElse(null);
+        return text(object).ifFailureFail().getValue().orElse(null);
     }
 
 }
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeTextFacetAbstract.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeTextFacetAbstract.java
index 710a381634..846d36ada9 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeTextFacetAbstract.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/all/i8n/imperative/HasImperativeTextFacetAbstract.java
@@ -24,7 +24,7 @@ import java.util.function.BiConsumer;
 
 import org.apache.isis.applib.services.i18n.TranslationContext;
 import org.apache.isis.commons.collections.Can;
-import org.apache.isis.commons.functional.Result;
+import org.apache.isis.commons.functional.Try;
 import org.apache.isis.core.metamodel.facetapi.Facet;
 import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
@@ -56,7 +56,7 @@ implements
     }
 
     @Override
-    public final Result<String> text(final ManagedObject object) {
+    public final Try<String> text(final ManagedObject object) {
         return ManagedObjects.imperativeText(object, method, translationContext);
     }
 
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java
index 227ab8547a..0780e9a443 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/spec/ManagedObjects.java
@@ -42,7 +42,7 @@ import org.apache.isis.applib.services.i18n.TranslatableString;
 import org.apache.isis.applib.services.i18n.TranslationContext;
 import org.apache.isis.applib.services.repository.EntityState;
 import org.apache.isis.commons.collections.Can;
-import org.apache.isis.commons.functional.Result;
+import org.apache.isis.commons.functional.Try;
 import org.apache.isis.commons.internal.assertions._Assert;
 import org.apache.isis.commons.internal.base._NullSafe;
 import org.apache.isis.commons.internal.base._Objects;
@@ -433,18 +433,18 @@ public final class ManagedObjects {
 
     // -- IMPERATIVE TEXT UTILITY
 
-    public static Result<String> imperativeText(
+    public static Try<String> imperativeText(
             final @Nullable ManagedObject object,
             final @NonNull Method method,
             final @Nullable TranslationContext translationContext) {
 
         if(ManagedObjects.isNullOrUnspecifiedOrEmpty(object)) {
-            return Result.success(null);
+            return Try.success(null);
         }
 
         val mmc = object.getSpecification().getMetaModelContext();
 
-        val result =  Result.of(()->{
+        val result =  Try.call(()->{
             final Object returnValue = ManagedObjects.InvokeUtil.invoke(method, object);
             if(returnValue instanceof String) {
                 return (String) returnValue;