Class CsvStats

java.lang.Object
cloud.opencode.base.csv.stats.CsvStats

public final class CsvStats extends Object
CSV Stats - Statistical operations on CSV columns CSV统计 - 对CSV列的统计操作

Provides count, numeric aggregate, distinct, and frequency operations on columns of a CsvDocument. All methods are static and the class cannot be instantiated.

提供对 CsvDocument 列的计数、数值聚合、去重和频率操作。 所有方法均为静态方法,该类不可实例化。

Features | 主要功能:

  • Count operations (non-blank, total rows) - 计数操作(非空白、总行数)
  • Numeric aggregation (sum, avg, min, max) - 数值聚合(求和、平均、最小、最大)
  • String operations (distinct, frequency) - 字符串操作(去重、频率)
  • Column summary (all stats at once) - 列摘要(一次性获取所有统计)

Usage Examples | 使用示例:

CsvDocument doc = CsvDocument.builder()
    .header("name", "score")
    .addRow("Alice", "95")
    .addRow("Bob", "87")
    .build();

long count = CsvStats.count(doc, "name");           // 2
BigDecimal total = CsvStats.sum(doc, "score");       // 182
BigDecimal average = CsvStats.avg(doc, "score");     // 91.000000
List<String> names = CsvStats.distinct(doc, "name"); // [Alice, Bob]

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: All null params throw NullPointerException - 空值安全: 所有null参数抛出NullPointerException
Since:
JDK 25, opencode-base-csv V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • count

      public static long count(CsvDocument doc, String column)
      Counts non-blank values in a column 计算列中非空白值的数量
      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      the count of non-blank values | 非空白值数量
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • countAll

      public static long countAll(CsvDocument doc)
      Counts all rows in the document 计算文档中所有行的数量
      Parameters:
      doc - the document | 文档
      Returns:
      the total row count | 总行数
      Throws:
      NullPointerException - if doc is null | 如果doc为null
    • sum

      public static BigDecimal sum(CsvDocument doc, String column)
      Computes the sum of numeric values in a column 计算列中数值的总和

      Non-numeric and blank values are silently skipped. Returns BigDecimal.ZERO if no numeric values are found.

      非数值和空白值被静默跳过。 如果没有找到数值,返回 BigDecimal.ZERO

      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      the sum | 总和
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • avg

      public static BigDecimal avg(CsvDocument doc, String column)
      Computes the average of numeric values in a column 计算列中数值的平均值

      Non-numeric and blank values are silently skipped. Returns null if no numeric values are found. Uses HALF_UP rounding with scale of 6.

      非数值和空白值被静默跳过。 如果没有找到数值,返回null。 使用HALF_UP舍入模式,精度为6。

      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      the average, or null if no numeric values | 平均值,无数值时为null
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • min

      public static BigDecimal min(CsvDocument doc, String column)
      Finds the minimum numeric value in a column 查找列中的最小数值

      Non-numeric and blank values are silently skipped. Returns null if no numeric values are found.

      非数值和空白值被静默跳过。 如果没有找到数值,返回null。

      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      the minimum value, or null if no numeric values | 最小值,无数值时为null
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • max

      public static BigDecimal max(CsvDocument doc, String column)
      Finds the maximum numeric value in a column 查找列中的最大数值

      Non-numeric and blank values are silently skipped. Returns null if no numeric values are found.

      非数值和空白值被静默跳过。 如果没有找到数值,返回null。

      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      the maximum value, or null if no numeric values | 最大值,无数值时为null
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • distinct

      public static List<String> distinct(CsvDocument doc, String column)
      Returns distinct non-null values in a column, preserving insertion order 返回列中去重的非null值,保留插入顺序
      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      unmodifiable list of distinct values | 不可修改的去重值列表
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • frequency

      public static Map<String,Long> frequency(CsvDocument doc, String column)
      Computes frequency of each value in a column, ordered by count descending 计算列中每个值的频率,按计数降序排列
      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      unmodifiable map of value to count, sorted by count descending | 不可修改的值到计数Map,按计数降序排列
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到
    • summary

      public static CsvColumnStats summary(CsvDocument doc, String column)
      Computes a full statistical summary for a column 计算列的完整统计摘要
      Parameters:
      doc - the document | 文档
      column - the column name | 列名
      Returns:
      the column statistics | 列统计
      Throws:
      NullPointerException - if any parameter is null | 如果任何参数为null
      OpenCsvException - if the column is not found | 如果列未找到