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
public final class IfExpression<RETURN_TYPE> extends Object
An if expression is anIf(boolean, Object)andElseIfExpression.Else(Object), with multiple optional intermediateElseIfExpression.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:
with the different that the If expression also can contain Else-If combinations:String description = value < 0 ? "Negative number" : "Zero or Positive Number";
In these examples we've used simple predicate expressions and fixed return valuesimport 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");
However the If expression also supports using Lambda expressions, both for theIfPredicateand for the return value.
Using this has the advantage of only evaluating theIfPredicateand the return valueSupplierIF 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected 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>If(boolean ifPredicate, Supplier<RETURN_TYPE> ifReturnValueSupplier)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_TYPEresolveReturnValue()Resolve the Return Value
-
-
-
Method Detail
-
If
public static <RETURN_TYPE> ElseIfExpression<RETURN_TYPE> If(boolean ifPredicate, Supplier<RETURN_TYPE> ifReturnValueSupplier)
-
If
public static <RETURN_TYPE> ElseIfExpression<RETURN_TYPE> If(IfPredicate ifPredicate, Supplier<RETURN_TYPE> ifReturnValueSupplier)
-
If
public static <RETURN_TYPE> ElseIfExpression<RETURN_TYPE> If(boolean ifPredicate, RETURN_TYPE ifFixedValue)
-
If
public static <RETURN_TYPE> ElseIfExpression<RETURN_TYPE> If(IfPredicate ifPredicate, RETURN_TYPE ifFixedValue)
-
getChild
protected 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.- Returns:
- the child (if any) of this
IfThenElseLogicelement
-
evaluate
protected boolean evaluate()
Evaluate theIfPredicate- Returns:
- the result of
IfPredicate.test()
-
resolveReturnValue
protected RETURN_TYPE resolveReturnValue()
Resolve the Return Value- Returns:
- the result of the return-value
Supplier.get()
-
-