io.burt.jmespath.function.AvgFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmespath-core Show documentation
Show all versions of jmespath-core Show documentation
A JMESPath implementation for Java
package io.burt.jmespath.function;
import java.util.List;
import io.burt.jmespath.Adapter;
import io.burt.jmespath.JmesPathType;
public class AvgFunction extends ArrayMathFunction {
public AvgFunction() {
super(ArgumentConstraints.typeOf(JmesPathType.NUMBER));
}
@Override
protected T performMathOperation(Adapter runtime, List values) {
if (values.isEmpty()) {
return runtime.createNull();
} else {
double sum = 0;
int count = 0;
for (T n : values) {
sum += runtime.toNumber(n).doubleValue();
count += 1;
}
return runtime.createNumber(sum/count);
}
}
}