数据传输包装类


点击查看代码
import java.util.function.Function;
import lombok.NonNull;

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

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

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

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

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

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

    public static  Opt 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  Opt map(@NonNull Function 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  Opt flatMap(@NonNull Function> 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;
    }
}