/*
 * 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;

public abstract class Try<T> {

    private Try() {
    }

    public static <U> Try<U> ofFailable(Callable<U> failable) {
        try {
            return Try.successful(failable.call());
        } catch (Exception e) {
            return Try.failure(e);
        }
    }

    public static <U> Try<U> successful(U u) {
        return new Success<U>(u);
    }

    public static <U> Try<U> failure(Throwable e) {
        return new Failure<U>(e);
    }

    public abstract boolean isSuccessful();

    public abstract T get();

    public abstract T orElseMapFailure(Function<Throwable, T> f);

    public abstract Optional<Throwable> getFailure();

    public abstract <U> Try<U> flatMap(Function<? super T, Try<U>> f);

    public <U> Try<U> map(final Function<? super T, U> f) {
        return flatMap(new Function<T, Try<U>>() {
            @Override
            public Try<U> apply(T input) {
                return Try.successful(f.apply(input));
            }
        });
    }

    public abstract Try<T> mapFailure(Function<Throwable, Throwable> f);

    public abstract void ifSuccessful(Consumer<T> consumer);

    public abstract void ifSuccessfulOrElse(Consumer<T> successConsumer, Consumer<Throwable> failureConsumer);

    private static class Success<T> extends Try<T> {
        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 orElseMapFailure(Function<Throwable, T> f) {
            return value;
        }

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

        @Override
        public <U> Try<U> flatMap(Function<? super T, Try<U>> f) {
            try {
                return f.apply(value);
            } catch (Exception e) {
                return Try.failure(e);
            }
        }

        @Override
        public Try<T> mapFailure(Function<Throwable, Throwable> f) {
            return this;
        }

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

        @Override
        public void ifSuccessfulOrElse(Consumer<T> successConsumer, Consumer<Throwable> 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 class Failure<T> extends Try<T> {
        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 orElseMapFailure(Function<Throwable, T> f) {
            return f.apply(failure);
        }

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

        @Override
        public <U> Try<U> flatMap(Function<? super T, Try<U>> f) {
            return Try.failure(failure);
        }

        @Override
        public Try<T> mapFailure(Function<Throwable, Throwable> f) {
            return Try.failure(f.apply(failure));
        }

        @Override
        public void ifSuccessful(Consumer<T> consumer) {
        }

        @Override
        public void ifSuccessfulOrElse(Consumer<T> successConsumer, Consumer<Throwable> 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();
        }
    }
}

@@ -1946,32 +1946,40 @@
 OrElse(Consumer%3C
+? super 
 T%3E successConsum
@@ -1983,32 +1983,40 @@
 sumer, Consumer%3C
+? super 
 Throwable%3E failu
@@ -2025,24 +2025,180 @@
 Consumer);%0A%0A
+    public abstract %3CR%3E R getSuccessfulOrElse(Function%3C? super T, ? extends R%3E successFunction, Function%3C? super Throwable, ? extends R%3E failureFunction);%0A%0A
     private 
@@ -3299,32 +3299,40 @@
 OrElse(Consumer%3C
+? super 
 T%3E successConsum
@@ -3336,32 +3336,40 @@
 sumer, Consumer%3C
+? super 
 Throwable%3E failu
@@ -3428,32 +3428,261 @@
 ue);%0A        %7D%0A%0A
+        @Override%0A        public %3CR%3E R getSuccessfulOrElse(Function%3C? super T, ? extends R%3E successFunction, Function%3C? super Throwable, ? extends R%3E failureFunction) %7B%0A            return successFunction.apply(value);%0A        %7D%0A%0A
         @Overrid
@@ -5207,16 +5207,24 @@
 onsumer%3C
+? super 
 T%3E succe
@@ -5244,16 +5244,24 @@
 onsumer%3C
+? super 
 Throwabl
@@ -5330,32 +5330,263 @@
 re);%0A        %7D%0A%0A
+        @Override%0A        public %3CR%3E R getSuccessfulOrElse(Function%3C? super T, ? extends R%3E successFunction, Function%3C? super Throwable, ? extends R%3E failureFunction) %7B%0A            return failureFunction.apply(failure);%0A        %7D%0A%0A
         @Overrid

Add Try.getSuccessfulOrElse()