Example #1
0
  public static double variance(Collection<?> inputList, String propertyPath) {
    double mean = mean(inputList, propertyPath);
    double temp = 0;

    double a;

    if (propertyPath.contains(".") || propertyPath.contains("[")) {

      String[] properties = StringScanner.splitByDelimiters(propertyPath, ".[]");

      for (Object o : inputList) {
        a = BeanUtils.getPropertyInt(o, properties);
        temp += (mean - a) * (mean - a);
      }

    } else {

      Map<String, FieldAccess> fields = BeanUtils.getFieldsFromObject(inputList.iterator().next());
      FieldAccess fieldAccess = fields.get(propertyPath);
      for (Object o : inputList) {
        a = fieldAccess.getInt(o);
        temp += (mean - a) * (mean - a);
      }
    }

    return Math.round(temp / inputList.size());
  }
Example #2
0
  /**
   * Calculate a sum of a property from a list.
   *
   * @param inputList
   * @param propertyPath to item we want to sum
   * @return sum
   */
  public static long sum(Collection<?> inputList, String propertyPath) {
    if (inputList.size() == 0) {
      return 0;
    }

    long sum = 0l;

    if (propertyPath.contains(".") || propertyPath.contains("[")) {

      String[] properties = StringScanner.splitByDelimiters(propertyPath, ".[]");

      for (Object o : inputList) {
        sum += BeanUtils.getPropertyInt(o, properties);
      }

    } else {

      Map<String, FieldAccess> fields = BeanUtils.getFieldsFromObject(inputList.iterator().next());
      FieldAccess fieldAccess = fields.get(propertyPath);
      for (Object o : inputList) {
        sum += fieldAccess.getInt(o);
      }
    }

    return sum;
  }