Replies: 2 comments
-
|
If I understand correctly, you have a list of structures like that: [
{
"name" : "Peter",
"age" : 23
},
{
"name" : "Mary",
"age" : 21
},
{
"name" : "Barky",
"age" : 5
}
]And you want to calculate the sum of all the Then a function that does this could go like the following implementation (not tested). @FunctionParameter(name = "list")
@FunctionParameter(name = "attributeName")
public class SumAllFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) throws EvaluationException {
List<EvaluationValue> list = parameterValues[0].getArrayValue();
String attributeName = parameterValues[1].getStringValue();
BigDecimal sum = BigDecimal.ZERO;
for (EvaluationValue entry : list) {
Map<String, EvaluationValue> map = entry.getStructureValue();
if (map.containsKey(attributeName)) {
sum = sum.add(map.get(attributeName).getNumberValue());
}
}
return EvaluationValue.numberValue(sum);
}
}Then doing a It probably needs some type checks, like is it an array, is each element a structure and i the attribute a number. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Thanks a lot. That works. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone. I am trying to create a function that compute the sum of structured elements in a list ( list of hashmap ), I would like to express that like : SUMALL( list[item.amount] ) SUMALL( list[*.amount] ) but there is no way apparently. Am I wrong ? As a first step, I added (successfully) a function to get the size à a list :
@functionParameter(name = "list")
static public class CollectionSizeFunction extends AbstractFunction {
@OverRide
public EvaluationValue evaluate(Expression expression, Token functionToken,
EvaluationValue... parameterValues) {
return expression.convertValue(parameterValues[0].getArrayValue().size());
}
}
Thanks for your help,
Phil
Beta Was this translation helpful? Give feedback.
All reactions