Class Conditionals


  • public class Conditionals
    extends Object
    Provides implementations of common conditional evaluation functions.
    See Also:
    ScalarValuedFunction, DeriveFields
    • Constructor Detail

      • Conditionals

        public Conditionals()
    • Method Detail

      • ifThenElse

        public static ScalarValuedFunction ifThenElse​(ScalarValuedFunction predicate,
                                                      ScalarValuedFunction trueValue,
                                                      ScalarValuedFunction falseValue)
        Builds a conditional expression which evaluates to one of two subexpressions based on the result of the given predicate. This is equivalent to to the ternary operator in Java:
         predicate ? trueValue : falseValue
         

        The subexpressions must evaluate to comparable types; the result is of the wider of the two types.

        The result expressions are only evaluated as required; that is, only one of the value expressions will be evaluated per invocation.

        Parameters:
        predicate - the expression selecting which of the subexpressions to which to evaluate
        trueValue - the expression for the value to return when the predicate evaluates to true
        falseValue - the expression for the value to return when the predicate does not evaluate to true
        Returns:
        a function evaluating the expression
      • ifNull

        public static ScalarValuedFunction ifNull​(String field,
                                                  ScalarValuedFunction replacement)
        Build a conditional expression which evaluates to the value of a field, if non-null. Otherwise, it evaluates to the value of the replacement expression. The field and subexpression must evaluate to comparable types; the result is of the wider of the two types. The replacement value is only evaluated if the test field is null-valued.
        Parameters:
        field - the record field value to test/return
        replacement - the expression for the value when the field value is null
        Returns:
        a function evaluating the expression
      • ifNull

        public static ScalarValuedFunction ifNull​(ScalarValuedFunction value,
                                                  ScalarValuedFunction replacement)
        Build a conditional expression which evaluates to the value of the specified expression, if non-null. If the value expression evaluates to null, the evaluated value of the replacement expression is returned instead. The subexpressions must evaluate to comparable types; the result is of the wider of the two types. The replacement value is only evaluated if the test expression evaluates to null-valued.
        Parameters:
        value - the expression to evaluate, test, and possibly return
        replacement - the expression for the value when the value expression evaluates to null
        Returns:
        a function evaluating the expression
      • caseWhen

        public static ScalarValuedFunction caseWhen​(ScalarValuedFunction base,
                                                    List<ScalarValuedFunction> cases,
                                                    List<ScalarValuedFunction> results)
        Builds a conditional expression which evaluates similarly to a switch statement.

        The base expression is compared to each case expression. When the first match is found, the result with the same index as the matching case expression is returned. If no match is found, null is returned.

        Parameters:
        base - The base expression used for comparison.
        cases - The list of case expressions to compare to the base expression.
        results - The list of results to return for each case expression.
        Returns:
        a function evaluating the expression
      • caseWhen

        public static ScalarValuedFunction caseWhen​(ScalarValuedFunction base,
                                                    List<ScalarValuedFunction> cases,
                                                    List<ScalarValuedFunction> results,
                                                    ScalarValuedFunction defaultResult)
        Builds a conditional expression which evaluates similarly to a switch statement.

        The base expression is compared to each case expression. When the first match is found, the result with the same index as the matching case expression is returned. If no match is found, the default result is returned.

        Parameters:
        base - The base expression used for comparison.
        cases - The list of case expressions to compare to the base expression.
        results - The list of results to return for each case expression.
        defaultResult - The default result to return if no match is found.
        Returns:
        a function evaluating the expression
      • caseWhen

        public static ScalarValuedFunction caseWhen​(List<ScalarValuedFunction> expressions,
                                                    List<ScalarValuedFunction> results)
        Builds a conditional expression which evaluates similarly to a chain of if..else statement.

        Each expression is evaluated. When the first expression which returns true is found, the result with the same index as the true expression is returned. If no true expression is found, null is returned.

        Parameters:
        expressions - The list of expressions to evaluate.
        results - The list of results to return for each expression.
        Returns:
        a function evaluating the expression
      • caseWhen

        public static ScalarValuedFunction caseWhen​(List<ScalarValuedFunction> expressions,
                                                    List<ScalarValuedFunction> results,
                                                    ScalarValuedFunction defaultResult)
        Builds a conditional expression which evaluates similarly to a chain of if..else statement.

        Each expression is evaluated. When the first expression which returns true is found, the result with the same index as the true expression is returned. If no true expression is found, the default result is returned.

        Parameters:
        expressions - The list of expressions to evaluate.
        results - The list of results to return for each expression.
        defaultResult - The result to return if no true expression is found.
        Returns:
        a function evaluating the expression