Condition: remove unnecessare object instantiation
Only one each, true and false, Condition object needed.
This commit is contained in:
parent
870c3c2b00
commit
14c494ffdd
1 changed files with 21 additions and 4 deletions
|
@ -21,6 +21,9 @@
|
||||||
|
|
||||||
package net.kemitix.conditional;
|
package net.kemitix.conditional;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If-then-else in a functional-style.
|
* If-then-else in a functional-style.
|
||||||
*
|
*
|
||||||
|
@ -37,9 +40,9 @@ public interface Condition {
|
||||||
*/
|
*/
|
||||||
static Condition where(final boolean clause) {
|
static Condition where(final boolean clause) {
|
||||||
if (clause) {
|
if (clause) {
|
||||||
return new TrueCondition();
|
return TrueCondition.getInstance();
|
||||||
}
|
}
|
||||||
return new FalseCondition();
|
return FalseCondition.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,14 +126,21 @@ public interface Condition {
|
||||||
/**
|
/**
|
||||||
* 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) {
|
if (clause) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
return new FalseCondition();
|
return FalseCondition.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -153,8 +163,15 @@ 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 this;
|
||||||
|
@ -163,7 +180,7 @@ public interface Condition {
|
||||||
@Override
|
@Override
|
||||||
public Condition or(final boolean secondClause) {
|
public Condition or(final boolean secondClause) {
|
||||||
if (secondClause) {
|
if (secondClause) {
|
||||||
return new TrueCondition();
|
return TrueCondition.getInstance();
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue