import java.util.Objects;

/**
 * 布尔表达式工具类
 */
public class BooleanExpHelper {

    private BooleanExpHelper() {
    }

    @FunctionalInterface
    public interface EmptyFunc {
        void accept();
    }

    public static BooleanExpHelper build() {
        return new BooleanExpHelper();
    }
    
    public BooleanExpHelper when(boolean exp, EmptyFunc condition) {
        Objects.requireNonNull(condition, "condition is null");
        if (exp) {
            condition.accept();
        }
        return this;
    }

    public BooleanExpHelper choose(boolean exp, EmptyFunc then, EmptyFunc otherwise) {
        Objects.requireNonNull(then, "then is null");
        Objects.requireNonNull(otherwise, "otherwise is null");
        if (exp) {
            then.accept();
        } else {
            otherwise.accept();
        }
        return this;
    }
}

更多推荐