Skip to content

Computing Functions ForCollections

anymaker edited this page Apr 4, 2021 · 4 revisions

These functions take a collection as a parameter and perform some calculations by it.

Count Calc count not null values in path
Max Detect max value
Min Detect minimal value
SubList Returning a subarray from an incoming array
TrimTo Removes all values from the array, starting from the beginning and ending with the given value
TrimAfter Removes all values from the array from the given value `lastValue` to the end

Count

Calc count not null values in a path or collection.

Incoming parameters

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

Output

Count of rows, type : int

Examples

count(null)    ->  0       
count(2)       ->  1
count((1,1,1)) ->  3
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add(null);
list.add("three");

int cnt = (int) engine.calc("count(.)", list);
assertEquals(3, cnt);

Max

Detect max value.

Incoming parameters

collection path or collection where required to find the max value or several values, among which you need to find the max value.

Output

max value

Examples

max(null)    ->  null       
max(1)       ->  1
max(1,2)     ->  2
max((1,2))   ->  2
LList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
Object max = engine.calc("max(.)", list);
assertEquals(2, max);

Min

Detect minimal value

Incoming parameters

collection path or collection where required to find the minimal value or several values, among which you need to find the max value.

Output

max value

Examples

min(null)    ->  null       
min(1)       ->  1
min(1,2)     ->  1
min((2,1))   ->  1
LList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
Object min= engine.calc("min(.)", list);
assertEquals(1, min);

SubList

Returning a subarray from an incoming array.
Subarray, starts with indexFrom and ends with indexTo. By default indexFrom and indexTo are included in the subarray

Syntax

subList(collection, indexFrom, indexTo)
subList(collection, indexFrom, indexTo, includeFirstValue)
subList(collection, indexFrom, indexTo, includeFirstValue, includeLastValue)

Incoming parameters

Parameter Name Type Require Description Default Value
collection List Yes Incoming collection
indexFrom int Yes The starting index in array which the subarray will be taken.
If it less than zero, then it will be counted from the end of the array.
indexTo String Yes The ending index in the array from which the subarray will be taken.
If it is less than zero, then it will be counted from the end of the array.
includeFirstValue boolean No Include first value to subarray true
includeLastValue boolean No Include last value to subarray true

Output

Type: java.util.List
Subarray.

Examples

assertEquals("[2, 3, 4]",       engine.calc(new Formula("subList( (1,2,3,4,5), 1,    3   )")).toString());
assertEquals("[1, 2, 3, 4, 5]", engine.calc(new Formula("subList( (1,2,3,4,5), 0,    4   )")).toString());
assertEquals("[5]",             engine.calc(new Formula("subList( (1,2,3,4,5), 100,  4   )")).toString());
assertEquals("[2, 3, 4, 5]",    engine.calc(new Formula("subList( (1,2,3,4,5), -3,   4   )")).toString());
assertEquals("[1, 2, 3, 4, 5]", engine.calc(new Formula("subList( (1,2,3,4,5), -100, 4   )")).toString());
assertEquals("[2, 3, 4, 5]",    engine.calc(new Formula("subList( (1,2,3,4,5), 1,    40  )")).toString());
assertEquals("[1, 2]",          engine.calc(new Formula("subList( (1,2,3,4,5), 1,    0   )")).toString());
assertEquals("[2, 3, 4]",       engine.calc(new Formula("subList( (1,2,3,4,5), 1,    -1  )")).toString());
assertEquals("[1, 2]",          engine.calc(new Formula("subList( (1,2,3,4,5), 1,    -100)")).toString());
assertEquals("[]", engine.calc(new Formula("subList( null,        1,   3  )")).toString());
assertEquals("[]", engine.calc(new Formula("subList( toList(),    1,   3  )")).toString());
assertEquals("[]", engine.calc(new Formula("subList( (1,2,3,4,5), 100, 100)")).toString());

TrimTo

Removes all values from the array, starting from the beginning and ending with the given value firstValue.
The optional leaveFirstValue parameter allows you to leave the specified value firstValue in the array. By default, it is true.

Syntax

trimTo(collection, firstValue)
trimTo(collection, firstValue, leaveFirstValue)

Incoming parameters

Parameter Name Type Require Description Default Value
collection List Yes Incoming collection
firstValue Object Yes Value as border to trim.
leaveFirstValue boolean No Leave the specified value in the array true

Output

Type: java.util.List
Trimmed array.

Examples

assertEquals("[3, 4, 5]",       engine.calc(new Formula("trimTo( (1,2,3,4,5), 3       )")).toString());
assertEquals("[4, 5]",          engine.calc(new Formula("trimTo( (1,2,3,4,5), 3, false)")).toString());

assertEquals("[]",              engine.calc(new Formula("trimTo( (1,2,3,4,5), 6       )")).toString());
assertEquals("[1, 2, 3, 4, 5]", engine.calc(new Formula("trimTo( (1,2,3,4,5), 1       )")).toString());

assertEquals("[5]",             engine.calc(new Formula("trimTo( (1,2,3,4,5), 5       )")).toString());
assertEquals("[]",              engine.calc(new Formula("trimTo( (1,2,3,4,5), 5, false)")).toString());

assertEquals("[]",              engine.calc(new Formula("trimTo( (1,2,3,4,5), null    )")).toString());
assertEquals("[]",              engine.calc(new Formula("trimTo( null,        5       )")).toString());

TrimAfter

Removes all values from the array from the given value lastValue to the end.
The optional leaveLastValue parameter allows you to leave the specified value lastValue in the array. By default, it is true.

Syntax

trimAfter(collection, lastValue)
trimAfter(collection, lastValue, leaveLastValue)

Incoming parameters

Parameter Name Type Require Description Default Value
collection List Yes Incoming collection
lastValue Object Yes Value as border to trim.
leaveLastValue boolean No Leave the specified value in the array true

Output

Type: java.util.List
Trimmed array.

Examples

assertEquals("[1, 2, 3]",       engine.calc(new Formula("trimAfter( (1,2,3,4,5), 3       )")).toString());
assertEquals("[1, 2]",          engine.calc(new Formula("trimAfter( (1,2,3,4,5), 3, false)")).toString());

assertEquals("[]",              engine.calc(new Formula("trimAfter( (1,2,3,4,5), 6       )")).toString());
assertEquals("[]",              engine.calc(new Formula("trimAfter( (1,2,3,4,5), 6, false)")).toString());

assertEquals("[1]",             engine.calc(new Formula("trimAfter( (1,2,3,4,5), 1       )")).toString());
assertEquals("[]",              engine.calc(new Formula("trimAfter( (1,2,3,4,5), 1, false)")).toString());

assertEquals("[1, 2, 3, 4, 5]", engine.calc(new Formula("trimAfter( (1,2,3,4,5), 5       )")).toString());
assertEquals("[1, 2, 3, 4]",    engine.calc(new Formula("trimAfter( (1,2,3,4,5), 5, false)")).toString());

assertEquals("[]",              engine.calc(new Formula("trimAfter( (1,2,3,4,5), null    )")).toString());
assertEquals("[]",              engine.calc(new Formula("trimAfter( null,        5       )")).toString());