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

org.reactfx.util.Try Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.reactfx.util;

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

public interface Try extends Either {

    static  Try success(T value) {
        return new Success<>(value);
    }

    static  Try failure(Throwable thrown) {
        return new Failure<>(thrown);
    }

    static  Try tryGet(Callable f) {
        try {
            return success(f.call());
        } catch(Throwable t) {
            return failure(t);
        }
    }

    default boolean isSuccess() { return isRight(); }
    default boolean isFailure() { return isLeft(); }
    default T get() { return getRight(); }
    default Throwable getFailure() { return getLeft(); }
    default Optional toOptional() { return asRight(); }
    default void ifSuccess(Consumer f) { ifRight(f); }
    default void ifFailure(Consumer f) { ifLeft(f); }

    T getOrElse(T fallback);
    T getOrElse(Supplier fallback);
    Try orElse(Try fallback);
    Try orElse(Supplier> fallback);
    Try orElseTry(Callable fallback);
    Try recover(Function> f);
     Try map(Function f);
     Try flatMap(Function> f);
}

class Success extends Right implements Try {

    public Success(T value) { super(value); }

    @Override
    public T getOrElse(T fallback) { return getRight(); }

    @Override
    public T getOrElse(Supplier fallback) { return getRight(); }

    @Override
    public Try orElse(Try fallback) { return this; }

    @Override
    public Try orElse(Supplier> fallback) { return this; }

    @Override
    public Try orElseTry(Callable fallback) { return this; }

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

    @Override
    public  Try map(Function f) {
        return new Success<>(f.apply(get()));
    }

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

    @Override
    public String toString() {
        return "success(" + get() + ")";
    }
}

class Failure extends Left implements Try {

    public Failure(Throwable thrown) { super(thrown); }

    @Override
    public T getOrElse(T fallback) { return fallback; }

    @Override
    public T getOrElse(Supplier fallback) { return fallback.get(); }

    @Override
    public Try orElse(Try fallback) { return fallback; }

    @Override
    public Try orElse(Supplier> fallback) { return fallback.get(); }

    @Override
    public Try orElseTry(Callable fallback) { return Try.tryGet(fallback); }

    @Override
    public Try recover(Function> f) {
        Optional recovered = f.apply(getFailure());
        if(recovered.isPresent()) {
            return new Success<>(recovered.get());
        } else {
            return this;
        }
    }

    @Override
    public  Try map(Function f) {
        return new Failure<>(getFailure());
    }

    @Override
    public  Try flatMap(Function> f) {
        return new Failure<>(getFailure());
    }

    @Override
    public String toString() {
        return "failure(" + getFailure() + ")";
    }
}