Class IfExpression<RETURN_TYPE>

java.lang.Object
dk.cloudcreate.essentials.shared.logic.IfExpression<RETURN_TYPE>
Type Parameters:
RETURN_TYPE - the return type from the IfThenElseLogic's return value supplier lambda or the provided fixed value

public final class IfExpression<RETURN_TYPE> extends Object
An if expression is an 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 values
However 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 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(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 this IfThenElseLogic element.
      The Else element in an If/(ElseIf)/Else sequence will have a child that's null.
      Returns:
      the child (if any) of this IfThenElseLogic element
    • evaluate

      protected boolean evaluate()
      Evaluate the IfPredicate
      Returns:
      the result of IfPredicate.test()
    • resolveReturnValue

      protected RETURN_TYPE resolveReturnValue()
      Resolve the Return Value
      Returns:
      the result of the return-value Supplier.get()