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

com.fireflysource.common.sys.Result Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.common.sys;

import com.fireflysource.common.string.StringUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import static com.fireflysource.common.func.FunctionInterfaceUtils.createEmptyConsumer;

/**
 * @author Pengtao Qiu
 */
public class Result {

    public static final CompletableFuture DONE = doneFuture();
    public static final Result SUCCESS = new Result<>(true, null, null);
    private static final Consumer EMPTY = createEmptyConsumer();

    private final boolean success;
    private final T value;
    private final Throwable throwable;

    public Result(boolean success, T value, Throwable throwable) {
        this.success = success;
        this.value = value;
        this.throwable = throwable;
    }

    public static void done(CompletableFuture future) {
        future.complete(null);
    }

    public static CompletableFuture doneFuture() {
        CompletableFuture future = new CompletableFuture<>();
        future.complete(null);
        return future;
    }

    @SuppressWarnings("unchecked")
    public static  Consumer emptyConsumer() {
        return EMPTY;
    }

    public static  Result createFailedResult(T value, Throwable throwable) {
        return new Result<>(false, value, throwable);
    }

    public static Result createFailedResult(Throwable throwable) {
        return new Result<>(false, null, throwable);
    }

    public static Result createSuccessResult() {
        return new Result<>(true, null, null);
    }

    @SuppressWarnings("unchecked")
    public static  Consumer> discard() {
        return EMPTY;
    }

    public static  Consumer> futureToConsumer(CompletableFuture future) {
        return result -> {
            if (result.isSuccess()) {
                future.complete(result.getValue());
            } else {
                future.completeExceptionally(result.getThrowable());
            }
        };
    }

    public static  Result runCaching(Callable callable) {
        try {
            T t = callable.call();
            return new Result<>(true, t, null);
        } catch (Throwable t) {
            return new Result<>(false, null, t);
        }
    }

    public boolean isSuccess() {
        return success;
    }

    public T getValue() {
        return value;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", value=" + value +
                ", throwable=" + (throwable != null && StringUtils.hasText(throwable.getMessage()) ? throwable.getMessage() : "null") +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy