All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.internal.Try Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed 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.gradle.internal;

import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * An object to represent the result of an operation that can potentially fail.
 * The object either holds the result of a successful execution, or an exception encountered during a failed one.
 */
public abstract class Try {

    private Try() {
    }

    /**
     * Construct a {@code Try} by executing the given operation.
     * The returned object will either hold the result or the exception thrown during the operation.
     */
    public static  Try ofFailable(Callable failable) {
        try {
            return Try.successful(failable.call());
        } catch (Exception e) {
            return Try.failure(e);
        }
    }

    /**
     * Construct a {@code Try} representing a successful execution.
     * The returned object will hold the given result.
     */
    public static  Try successful(U result) {
        return new Success(result);
    }

    /**
     * Construct a {@code Try} representing a failed execution.
     * The returned object will hold the given failure.
     */
    public static  Try failure(Throwable failure) {
        return new Failure(failure);
    }

    /**
     * Returns whether this {@code Try} represents a successful execution.
     */
    public abstract boolean isSuccessful();

    /**
     * Return the result if the represented operation was successful.
     * Throws the original failure otherwise (wrapped in an {@link UncheckedException} if necessary).
     */
    public abstract T get();

    /**
     * Return the result if the represented operation was successful or return the result of the given function.
     * In the latter case the failure is passed to the function.
     */
    public abstract T getOrMapFailure(Function f);

    /**
     * Returns the failure for a failed result, or {@link Optional#empty()} otherwise.
     */
    public abstract Optional getFailure();

    /**
     * If the represented operation was successful, returns the result of applying the given
     * {@code Try}-bearing mapping function to the value, otherwise returns
     * the {@code Try} representing the original failure.
     *
     * Exceptions thrown by the given function are propagated.
     */
    public abstract  Try flatMap(Function> f);

    /**
     * If the represented operation was successful, returns the result of applying the given
     * mapping function to the value, otherwise returns
     * the {@code Try} representing the original failure.
     *
     * This is similar to {@link #tryMap(Function)} but propagates any exception the given function throws.
     */
    public abstract  Try map(Function f);

    /**
     * If the represented operation was successful, returns the result of applying the given
     * mapping function to the value, otherwise returns
     * the {@code Try} representing the original failure.
     *
     * This is similar to {@link #map(Function)} but converts any exception the given function
     * throws into a failed {@code Try}.
     */
    public abstract  Try tryMap(Function f);

    /**
     * If the represented operation was successful, returns the original result,
     * otherwise returns the given mapping function applied to the failure.
     */
    public abstract Try mapFailure(Function f);

    /**
     * Calls the given consumer with the result iff the represented operation was successful.
     */
    public abstract void ifSuccessful(Consumer consumer);

    /**
     * Calls {successConsumer} with the result if the represented operation was successful,
     * otherwise calls {failureConsumer} with the failure.
     */
    public abstract void ifSuccessfulOrElse(Consumer successConsumer, Consumer failureConsumer);

    private static final class Success extends Try {
        private final T value;

        public Success(T value) {
            this.value = value;
        }

        @Override
        public boolean isSuccessful() {
            return true;
        }

        @Override
        public T get() {
            return value;
        }

        @Override
        public T getOrMapFailure(Function f) {
            return value;
        }

        @Override
        public Optional getFailure() {
            return Optional.empty();
        }

        @Override
        public  Try flatMap(Function> f) {
            return f.apply(value);
        }

        @Override
        public  Try map(Function f) {
            return Try.successful(f.apply(value));
        }

        @Override
        public  Try tryMap(final Function f) {
            return Try.ofFailable(new Callable() {
                @Override
                public U call() {
                    return f.apply(value);
                }
            });
        }

        @Override
        public Try mapFailure(Function f) {
            return this;
        }

        @Override
        public void ifSuccessful(Consumer consumer) {
            consumer.accept(value);
        }

        @Override
        public void ifSuccessfulOrElse(Consumer successConsumer, Consumer failureConsumer) {
            successConsumer.accept(value);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Success success = (Success) o;

            return value.equals(success.value);
        }

        @Override
        public int hashCode() {
            return value.hashCode();
        }
    }

    private static final class Failure extends Try {
        private final Throwable failure;

        public Failure(Throwable failure) {
            this.failure = failure;
        }

        @Override
        public boolean isSuccessful() {
            return false;
        }

        @Override
        public T get() {
            throw UncheckedException.throwAsUncheckedException(failure);
        }

        @Override
        public T getOrMapFailure(Function f) {
            return f.apply(failure);
        }

        @Override
        public Optional getFailure() {
            return Optional.of(failure);
        }

        @Override
        public  Try flatMap(Function> f) {
            return Cast.uncheckedNonnullCast(this);
        }

        @Override
        public  Try map(Function f) {
            return Cast.uncheckedNonnullCast(this);
        }

        @Override
        public  Try tryMap(Function f) {
            return Cast.uncheckedNonnullCast(this);
        }

        @Override
        public Try mapFailure(Function f) {
            return Try.failure(f.apply(failure));
        }

        @Override
        public void ifSuccessful(Consumer consumer) {
        }

        @Override
        public void ifSuccessfulOrElse(Consumer successConsumer, Consumer failureConsumer) {
            failureConsumer.accept(failure);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Failure failure1 = (Failure) o;

            return failure.equals(failure1.failure);
        }

        @Override
        public int hashCode() {
            return failure.hashCode();
        }
    }
}