Class IfExpression<RETURN_TYPE>
java.lang.Object
dk.cloudcreate.essentials.shared.logic.IfExpression<RETURN_TYPE>
- Type Parameters:
RETURN_TYPE- the return type from theIfThenElseLogic's return value supplier lambda or the provided fixed value
An if expression is an
However the If expression also supports using Lambda expressions, both for the
Using this has the advantage of only evaluating the
If(boolean, Object) and ElseIfExpression.Else(Object),
with multiple optional intermediate ElseIfExpression.ElseIf(boolean, Object)'s, which
returns a value of the evaluation of the if expression:
import static dk.cloudcreate.essentials.shared.logic.IfExpression.If;
int value = getValue();
String description = If(value < 0, "Negative number").
Else("Zero or Positive number");
This is similar to the Java ternary operation:
String description = value < 0 ? "Negative number" : "Zero or Positive Number";
with the different that the If expression also can contain Else-If combinations:
import static dk.cloudcreate.essentials.shared.logic.IfExpression.If;
int value = getValue();
String description = If(value < 0, "Negative number").
ElseIf(value == 0, "Zero").
Else("Positive number");
In these examples we've used simple predicate expressions and fixed return valuesHowever the If expression also supports using Lambda expressions, both for the
IfPredicate and for the return value.Using this has the advantage of only evaluating the
IfPredicate and the return value Supplier IF necessary, which for calculations, DB lookups or other IO intensive operation
will yield a much better result:
import static dk.cloudcreate.essentials.shared.logic.IfExpression.If;
OrderId orderId = ...;
Amount orderAmount = ...;
var orderProcessingResult = If(() -> orderAmountExceedsAccountThreshold(orderAmount),
() -> cancelOrder(orderId)).
Else(() -> acceptOrder(orderId));
-
Method Summary
Modifier and TypeMethodDescriptionprotected booleanevaluate()Evaluate theIfPredicateprotected dk.cloudcreate.essentials.shared.logic.IfThenElseLogic<RETURN_TYPE>getChild()Get the child (if any) of thisIfThenElseLogicelement.
The Else element in an If/(ElseIf)/Else sequence will have a child that's null.static <RETURN_TYPE>
ElseIfExpression<RETURN_TYPE>static <RETURN_TYPE>
ElseIfExpression<RETURN_TYPE>If(boolean ifPredicate, RETURN_TYPE ifFixedValue) static <RETURN_TYPE>
ElseIfExpression<RETURN_TYPE>If(IfPredicate ifPredicate, Supplier<RETURN_TYPE> ifReturnValueSupplier) static <RETURN_TYPE>
ElseIfExpression<RETURN_TYPE>If(IfPredicate ifPredicate, RETURN_TYPE ifFixedValue) protected RETURN_TYPEResolve the Return Value
-
Method Details
-
If
public static <RETURN_TYPE> ElseIfExpression<RETURN_TYPE> If(boolean ifPredicate, Supplier<RETURN_TYPE> ifReturnValueSupplier) -
If
public static <RETURN_TYPE> ElseIfExpression<RETURN_TYPE> If(boolean ifPredicate, RETURN_TYPE ifFixedValue) -
getChild
Get the child (if any) of thisIfThenElseLogicelement.
The Else element in an If/(ElseIf)/Else sequence will have a child that's null.- Returns:
- the child (if any) of this
IfThenElseLogicelement
-
evaluate
protected boolean evaluate()Evaluate theIfPredicate- Returns:
- the result of
IfPredicate.test()
-
resolveReturnValue
Resolve the Return Value- Returns:
- the result of the return-value
Supplier.get()
-