Skip to content

Computing Functions Auxiliary

anymaker edited this page Apr 3, 2021 · 1 revision

Auxiliary Functions

Decode Provide switch-case-default functionality
Ifnn Return value, if checked value is not null. Short synonym for function IfNotNull
IfNotNull Return value, if checked value is not null
Length Returns the length of the object, or -1 if not applicable
Nvl Return substitute if value is null

Decode

Provide switch-case-default and case-when-default functionality.

Syntax

decode(expression, caseValue, returnValue [, caseValue, returnValue] [, default])

Incoming parameters

expression - The value to compare
caseValue - The value that is compared against expression
returnValue - The value returned, if expression is equal to caseValue
default - Optional. If no matches are found, the DECODE function will return default.

Output

Result value with type as returnValue or default

Remark

If expression is a Collection, then comparison will run for every value in collection and result will be a collection.

Examples

Map<String, Object> testValues = new LinkedHashMap<>();
testValues.put("a", 1);
testValues.put("b", 2);

int res = engine.calc("   decode(.a,  1, 111,  2, 222,  3, 333,  5, 555)   ", testValues, Integer.class);
assertEquals(111, res);

int res = engine.calc("   decode(.b,  1, 111,  2, 222,  3, 333,  5, 555)   ", testValues, Integer.class);
assertEquals(222, res);

int res = engine.calc("   decode(.no,  1, 111,  2, 222, 555)   ", testValues, Integer.class);
assertEquals(555, res);

String str = engine.calc("   'aaa' + decode(null, '', ' ' + 'bbb')   ", testValues, String.class);
assertEquals("aaa", str);

Using as case-when-default

@Test
  public void run08() {
    Map<String, Object> map = new LinkedHashMap<String, Object>(){{
      put("fieldA", 10);
      put("fieldB", 20);
      put("fieldC", 30);
    }};

    Formula formula = new Formula("decode(true, .fieldA=10, 'resultA', .fieldB=20, 'resultB')");
    String str = engine.calc(formula, map, String.class);
    assertEquals("resultA", str);

    formula = new Formula("decode(true, .fieldA=11, 'resultA', .fieldB=20, 'resultB')");
    str = engine.calc(formula, map, String.class);
    assertEquals("resultB", str);
  }

IfNotNull

Return value, if checked value is not null.

Syntax

ifNotNull(expressionForCheck, expressionValue)
ifnn(expressionForCheck, expressionValue)

Incoming parameters

expressionForCheck - The expression whose result is checked for null.
expressionValue - The expression that will be the result if the expressionForCheck is not null.

Output

Result value with type as expressionValue

Examples

String str = engine.calc("   'aaa' + ifNotNull(null, ' ' + 'bbb')   ", String.class);
assertEquals("aaa", str);

String str = engine.calc("   'aaa' + ifnn('not null', ' ' + 'bbb'   ", String.class);
assertEquals("aaa bbb", str);

Length

Returns the length of the object, or -1 if not applicable.

Incoming parameters

value path or collection where required to count the number of rows

Output

Length of the object, type : long

Examples

int len = engine.calc("   length(null)   ", Integer.class);
assertEquals(0, len);

len = engine.calc("   length('abcdf')   ", Integer.class);
assertEquals(5, len);

len = engine.calc("   length(1)   ", Integer.class);
assertEquals(1, len);

len = engine.calc("   length(123)   ", Integer.class);
assertEquals(3, len);

len = engine.calc("   length((1,2,3,4,5))   ", Integer.class);
assertEquals(5, len);

Map<String, Integer> map = new HashMap<>();
map.put("1", 1);
map.put("2", 2);
Map<String, Object> data = new HashMap<>();
data.put("map", map);
len = engine.calc("   length(.map)   ", data, Integer.class);
assertEquals(2, len);

Nvl

Return substitute if value is null.

Incoming parameters

value The value to test to null.
substitute The value to be used if value is null.

Output

Result value with type as value or substitute

Examples

String str = engine.calc("   'aaa' + nvl(null, ' ' + 'bbb')   ", String.class);
assertEquals("aaa bbb", str);

str = engine.calc("   'aaa ' + nvl('no null', ' ' + 'bbb')   ", String.class);
assertEquals("aaa no null", str);