Condition: remove use of 'if' internaly

By using a look-up map for true and false the only branch in the code
is removed.
This commit is contained in:
Paul Campbell 2017-04-22 07:34:44 +01:00
parent 826dcc17ca
commit dcf1847def

View file

@ -21,8 +21,11 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import lombok.AccessLevel; import java.util.AbstractMap.SimpleEntry;
import lombok.NoArgsConstructor; import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* If-then-else in a functional-style. * If-then-else in a functional-style.
@ -31,6 +34,14 @@ import lombok.NoArgsConstructor;
*/ */
public interface Condition { public interface Condition {
Condition TRUE = new TrueCondition();
Condition FALSE = new FalseCondition();
Map<Boolean, Condition> CONDITIONS = Collections.unmodifiableMap(
Stream.of(new SimpleEntry<>(true, TRUE), new SimpleEntry<>(false, FALSE))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
/** /**
* Create a new {@code Condition} for the clause. * Create a new {@code Condition} for the clause.
* *
@ -39,10 +50,7 @@ public interface Condition {
* @return the Condition * @return the Condition
*/ */
static Condition where(final boolean clause) { static Condition where(final boolean clause) {
if (clause) { return CONDITIONS.get(clause);
return TrueCondition.getInstance();
}
return FalseCondition.getInstance();
} }
/** /**
@ -52,7 +60,7 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
static Condition whereNot(boolean clause) { static Condition whereNot(final boolean clause) {
return where(!clause); return where(!clause);
} }
@ -72,7 +80,7 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
default Condition andNot(boolean clause) { default Condition andNot(final boolean clause) {
return and(!clause); return and(!clause);
} }
@ -92,7 +100,7 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
default Condition orNot(boolean clause) { default Condition orNot(final boolean clause) {
return or(!clause); return or(!clause);
} }
@ -119,39 +127,29 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
default Condition otherwise(boolean clause) { default Condition otherwise(final boolean clause) {
return where(clause); return where(clause);
} }
/** /**
* A {@code Condition} that has evaluated to {@code true}. * A {@code Condition} that has evaluated to {@code true}.
*/ */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
class TrueCondition implements Condition { class TrueCondition implements Condition {
private static final Condition INSTANCE = new TrueCondition();
private static Condition getInstance() {
return INSTANCE;
}
@Override @Override
public Condition and(final boolean clause) { public Condition and(final boolean clause) {
if (clause) { return where(clause);
return this;
}
return FalseCondition.getInstance();
} }
@Override @Override
public Condition or(final boolean secondClause) { public Condition or(final boolean secondClause) {
return this; return TRUE;
} }
@Override @Override
public Condition then(final Runnable response) { public Condition then(final Runnable response) {
response.run(); response.run();
return this; return TRUE;
} }
@Override @Override
@ -163,31 +161,21 @@ public interface Condition {
/** /**
* A {@code Condition} that has evaluated to {@code false}. * A {@code Condition} that has evaluated to {@code false}.
*/ */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
class FalseCondition implements Condition { class FalseCondition implements Condition {
private static final Condition INSTANCE = new FalseCondition();
private static Condition getInstance() {
return INSTANCE;
}
@Override @Override
public Condition and(final boolean clause) { public Condition and(final boolean clause) {
return this; return FALSE;
} }
@Override @Override
public Condition or(final boolean secondClause) { public Condition or(final boolean secondClause) {
if (secondClause) { return where(secondClause);
return TrueCondition.getInstance();
}
return this;
} }
@Override @Override
public Condition then(final Runnable response) { public Condition then(final Runnable response) {
return this; return FALSE;
} }
@Override @Override