数据传输包装类

`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;
}

}`

上一篇:Photoshop给废墟人像添上一抹梦幻粉色朦胧美效果


下一篇:装饰者模式