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

io.github.shanqiang.util.AggregationUtil Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
package io.github.shanqiang.util;

import io.github.shanqiang.table.Row;

import java.util.List;

public class AggregationUtil {
    public static String groupConcat(List rows, String columnName) {
        StringBuilder sb = new StringBuilder();
        for (Row row : rows) {
            sb.append(row.getComparable(columnName));
            sb.append(',');
        }
        return sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1).toString() : sb.toString();
    }

    public static int sumInt(List rows, String columnName) {
        int ret = 0;
        for (Row row : rows) {
            Integer i = (Integer) row.getComparable(columnName);
            if (null == i) {
                continue;
            }
            ret += i;
        }

        return ret;
    }

    public static double sumLong(List rows, String columnName) {
        double ret = 0;
        for (Row row : rows) {
            Long l = (Long) row.getComparable(columnName);
            if (null == l) {
                continue;
            }
            ret += l;
        }

        return ret;
    }

    public static double sumDouble(List rows, String columnName) {
        double ret = 0;
        for (Row row : rows) {
            Number n = (Number) row.getComparable(columnName);
            if (null == n) {
                continue;
            }
            ret += n.doubleValue();
        }

        return ret;
    }

    public static Comparable max(List rows, String columnName) {
        Comparable ret = null;
        for (Row row : rows) {
            Comparable comparable = row.getComparable(columnName);
            if (null == comparable) {
                continue;
            }
            if (null == ret) {
                ret = comparable;
                continue;
            }
            ret = comparable.compareTo(ret) > 0 ? comparable : ret;
        }

        return ret;
    }

    public static double avg(List rows, String columnName) {
        return sumDouble(rows, columnName) / rows.size();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy