数据传输类

数据传输类

import java.util.function.Function;
import lombok.NonNull;

public class Opt<S> implements Serializable {
    private static final long serialVersionUID = -1157381887545667511L;
    private S some;
    private DefaultTip tip;
    private Throwable exp;

    public static <S> Opt<S> empty() {
        return some((Object)null);
    }

    public static <S, R> Opt<S> some(R r, Function<R, S> f) {
        try {
            return some(f.apply(r));
        } catch (Exception var3) {
            return exp(var3);
        }
    }

    public static <S> Opt<S> some(S t) {
        return new Opt(t);
    }

    public static <S> Opt<S> tip(DefaultTip tip) {
        return new Opt(tip);
    }

    public static <S> Opt<S> tip(String tip) {
        return tip(new DefaultTip(tip));
    }

    public static <S> Opt<S> exp(Throwable throwable) {
        return new Opt(throwable);
    }

    private Opt(S some) {
        this.some = some;
    }

    private Opt(DefaultTip tip) {
        this.tip = tip;
    }

    private Opt(Throwable exp) {
        this.exp = exp;
    }

    public S some() {
        return this.some;
    }

    public DefaultTip tip() {
        return this.tip;
    }

    public Throwable exp() {
        return this.exp;
    }

    public boolean ok() {
        return this.tip == null && this.exp == null;
    }

    public boolean hasV() {
        return this.some != null;
    }

    public <R> Opt<R> map(@NonNull Function<S, R> mapFn) {
        if (mapFn == null) {
            throw new NullPointerException("mapFn is marked non-null but is null");
        } else {
            return this.flatMap((s) -> {
                return some(mapFn.apply(s));
            });
        }
    }

    public <R> Opt<R> flatMap(@NonNull Function<S, Opt<R>> mapFn) {
        if (mapFn == null) {
            throw new NullPointerException("mapFn is marked non-null but is null");
        } else if (this.exp != null) {
            return exp(this.exp);
        } else if (this.tip != null) {
            return tip(this.tip);
        } else {
            return this.some == null ? empty() : (Opt)mapFn.apply(this.some);
        }
    }

    public String toString() {
        return "Opt{some=" + this.some + ", tip=" + this.tip + ", exp=" + this.exp + '}';
    }

    public S getSome() {
        return this.some;
    }

    public DefaultTip getTip() {
        return this.tip;
    }

    public Throwable getExp() {
        return this.exp;
    }
}

统一结果返回类

import java.io.Serializable;

public class Rs<T> implements Serializable {
    private static final int CODE_SUCCESS = 200;
    private static final int CODE_FAIL = 90000;
    private static final int CODE_ERROR = 90000;
    private static final int CODE_NO_PERMISSION = 90001;
    private static final long serialVersionUID = 5393448305311433341L;
    protected int code;
    protected String msg;
    protected T data;

    public Rs(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Rs<T> success() {
        return new Rs(200, "success", (Object)null);
    }

    public static <T> Rs<T> success(String message) {
        return new Rs(200, message, (Object)null);
    }

    public static <T> Rs<T> success(T data) {
        return new Rs(200, "success", data);
    }

    public static <T, P extends Tip> Rs<T> success(Op<T, P> data) {
        if (data.getExp() != null) {
            return error(data.exp().getMessage());
        } else if (data.getTip() != null) {
            return data.getTip() instanceof DefaultTip && ((DefaultTip)data.getTip()).isNoPermission() ? noPermission(data.getTip().getContent(), data.some()) : error(data.getTip().getContent());
        } else {
            return data.getSome() != null ? success(data.some()) : success();
        }
    }

    public static <T> Rs<T> success(Opt<T> data) {
        if (data.getExp() != null) {
            return error(data.exp().getMessage());
        } else if (data.getTip() != null) {
            return data.getTip() != null && data.getTip().isNoPermission() ? noPermission(data.getTip().getContent(), data.some()) : error(data.getTip().getContent());
        } else {
            return data.getSome() != null ? success(data.some()) : success();
        }
    }

    public static <T> Rs<T> success(String message, T data) {
        return new Rs(200, message, data);
    }

    public static <T> Rs<T> error() {
        return new Rs(90000, "fail", (Object)null);
    }

    public static <T> Rs<T> error(String message) {
        return new Rs(90000, message, (Object)null);
    }

    public static <T> Rs<T> error(T data) {
        return new Rs(90000, "fail", data);
    }

    public static <T> Rs<T> error(String message, T data) {
        return new Rs(90000, message, data);
    }

    public static <T> Rs<T> badrequest() {
        return new Rs(90000, "no identifier arguments", (Object)null);
    }

    public static <T> Rs<T> badrequest(String message) {
        return new Rs(90000, message, (Object)null);
    }

    public static <T> Rs<T> badrequest(T data) {
        return new Rs(90000, "no identifier arguments", data);
    }

    public static <T> Rs<T> badrequest(String message, T data) {
        return new Rs(90000, message, data);
    }

    public static <T> Rs<T> noPermission(String message, T data) {
        return new Rs(90001, message, data);
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }
}
posted @ 2022-01-23 18:41  道之缘  阅读(10)  评论(0编辑  收藏  举报