All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.burt.jmespath.function.AvgFunction Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
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);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy