diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index 7c7d201..75d5178 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -5,52 +5,103 @@ import java.util.function.Supplier; /** * @author Paul Campbell (pcampbell@kemitix.net). */ -public class Value { +public interface Value { - private boolean clause; - - private Supplier trueSupplier; - - private Value(final boolean clause) { - this.clause = clause; + static ValueClause where(final boolean clause) { + if (clause) { + return new TrueValueClause<>(); + } + return new FalseValueClause<>(); } - static Value where(final boolean clause) { - return new Value<>(clause); - } - - static Value whereNot(final boolean clause) { + static ValueClause whereNot(boolean clause) { return where(!clause); } - Value then(final Supplier trueSupplier) { - this.trueSupplier = trueSupplier; - return this; - } + interface ValueClause { - T otherwise(final Supplier falseSupplier) { - if (clause) { - return trueSupplier.get(); + ValueSupplier then(Supplier trueSupplier); + + ValueClause and(boolean clause); + + ValueClause or(boolean clause); + + default ValueClause andNot(final boolean clause) { + return and(!clause); } - return falseSupplier.get(); + + default ValueClause orNot(boolean clause) { + return or(!clause); + } + + interface ValueSupplier { + + T otherwise(Supplier falseSupplier); + + } + } - Value and(final boolean clause) { - this.clause = this.clause && clause; - return this; + class TrueValueClause implements ValueClause { + + @Override + public ValueSupplier then(final Supplier trueSupplier) { + return new TrueValueSupplier(trueSupplier); + } + + @Override + public ValueClause and(final boolean clause) { + return Value.where(clause); + } + + @Override + public ValueClause or(final boolean clause) { + return this; + } + + private class TrueValueSupplier implements ValueSupplier { + + private final Supplier valueSupplier; + + TrueValueSupplier(final Supplier valueSupplier) { + this.valueSupplier = valueSupplier; + } + + @Override + public T otherwise(final Supplier falseSupplier) { + return valueSupplier.get(); + } + + } + } - Value or(final boolean clause) { - this.clause = this.clause || clause; - return this; - } + class FalseValueClause implements ValueClause { - Value andNot(final boolean clause) { - return and(!clause); - } + @Override + public ValueSupplier then(final Supplier trueSupplier) { + return new FalseValueSupplier(); + } + + @Override + public ValueClause and(final boolean clause) { + return this; + } + + @Override + public ValueClause or(final boolean clause) { + return Value.where(clause); + } + + private class FalseValueSupplier implements ValueSupplier { + + @Override + public T otherwise(final Supplier falseSupplier) { + return falseSupplier.get(); + } + + } - Value orNot(final boolean clause) { - return or(!clause); } }