其他分享
首页 > 其他分享> > 数据传输包装类

数据传输包装类

作者:互联网

`import java.io.Serializable;
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 <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;
}

}`

标签:Opt,return,包装,tip,some,exp,数据传输,public
来源: https://www.cnblogs.com/xiaojianwen/p/15837005.html