Class CsvRow

java.lang.Object
cloud.opencode.base.csv.CsvRow

public final class CsvRow extends Object
CSV Row - Immutable representation of a single CSV row CSV行 - 单行CSV数据的不可变表示

Represents a single row in a CSV document. Each row contains an ordered list of string field values and an optional 1-based row number for location tracking. All instances are immutable and thread-safe.

表示CSV文档中的单行。每行包含一个有序的字符串字段值列表和一个可选的1起始行号用于位置跟踪。 所有实例都是不可变且线程安全的。

Features | 主要功能:

  • Immutable field values - 不可变字段值
  • 1-based row number tracking - 1起始行号跟踪
  • Index and header-based field access - 基于索引和标题的字段访问
  • Conversion to Map with headers - 使用标题转换为Map
  • Stream support - 流支持

Usage Examples | 使用示例:

CsvRow row = CsvRow.of("Alice", "30", "Engineer");
String name = row.get(0);

CsvRow numbered = CsvRow.of(1, "Bob", "25", "Designer");
Map<String, String> map = numbered.toMap(List.of("name", "age", "role"));

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Fields may contain null - 空值安全: 字段可能包含null
Since:
JDK 25, opencode-base-csv V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Row number indicating unknown position | 表示未知位置的行号
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    Returns an unmodifiable list of field values (alias for values()) 返回字段值的不可修改列表(values() 的别名)
    get(int index)
    Gets a field value by index 通过索引获取字段值
    get(String headerName, CsvDocument parent)
    Gets a field value by header name using the parent document's headers 使用父文档的标题通过标题名获取字段值
    int
     
    boolean
    Checks if all fields are empty or the row has no fields 检查所有字段是否为空或行没有字段
    static CsvRow
    of(int rowNumber, String... fields)
    Creates a row from field values with a specific row number 创建指定行号的字段值行
    static CsvRow
    of(int rowNumber, List<String> fields)
    Creates a row from a list of field values with a specific row number 从字段值列表创建指定行号的行
    static CsvRow
    of(String... fields)
    Creates a row from field values with unknown row number 创建字段值行(行号未知)
    int
    Gets the 1-based row number 获取1起始行号
    int
    Gets the number of fields in this row 获取此行的字段数
    Returns a stream of field values 返回字段值的流
    toMap(List<String> headers)
    Converts this row to a map using the provided headers as keys 使用提供的标题作为键将此行转换为Map
     
    Returns an unmodifiable list of field values 返回字段值的不可修改列表

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • UNKNOWN_ROW

      public static final int UNKNOWN_ROW
      Row number indicating unknown position | 表示未知位置的行号
      See Also:
  • Method Details

    • of

      public static CsvRow of(String... fields)
      Creates a row from field values with unknown row number 创建字段值行(行号未知)
      Parameters:
      fields - the field values | 字段值
      Returns:
      the row | 行
    • of

      public static CsvRow of(int rowNumber, String... fields)
      Creates a row from field values with a specific row number 创建指定行号的字段值行
      Parameters:
      rowNumber - the 1-based row number | 1起始行号
      fields - the field values | 字段值
      Returns:
      the row | 行
    • of

      public static CsvRow of(int rowNumber, List<String> fields)
      Creates a row from a list of field values with a specific row number 从字段值列表创建指定行号的行
      Parameters:
      rowNumber - the 1-based row number | 1起始行号
      fields - the field values | 字段值
      Returns:
      the row | 行
      Throws:
      NullPointerException - if fields is null | 如果fields为null
    • get

      public String get(int index)
      Gets a field value by index 通过索引获取字段值
      Parameters:
      index - the 0-based field index | 0起始字段索引
      Returns:
      the field value | 字段值
      Throws:
      IndexOutOfBoundsException - if index is out of range | 如果索引越界
    • get

      public String get(String headerName, CsvDocument parent)
      Gets a field value by header name using the parent document's headers 使用父文档的标题通过标题名获取字段值
      Parameters:
      headerName - the header name | 标题名
      parent - the parent document providing headers | 提供标题的父文档
      Returns:
      the field value | 字段值
      Throws:
      IllegalArgumentException - if header name is not found | 如果标题名未找到
      IndexOutOfBoundsException - if the header index exceeds this row's fields | 如果标题索引超过此行字段数
    • size

      public int size()
      Gets the number of fields in this row 获取此行的字段数
      Returns:
      the number of fields | 字段数
    • isEmpty

      public boolean isEmpty()
      Checks if all fields are empty or the row has no fields 检查所有字段是否为空或行没有字段
      Returns:
      true if empty | 如果为空返回true
    • rowNumber

      public int rowNumber()
      Gets the 1-based row number 获取1起始行号
      Returns:
      the row number, or -1 if unknown | 行号,未知则返回-1
    • values

      public List<String> values()
      Returns an unmodifiable list of field values 返回字段值的不可修改列表
      Returns:
      unmodifiable field list | 不可修改的字段列表
    • fields

      public List<String> fields()
      Returns an unmodifiable list of field values (alias for values()) 返回字段值的不可修改列表(values() 的别名)
      Returns:
      unmodifiable field list | 不可修改的字段列表
    • stream

      public Stream<String> stream()
      Returns a stream of field values 返回字段值的流
      Returns:
      a stream of field values | 字段值的流
    • toMap

      public Map<String,String> toMap(List<String> headers)
      Converts this row to a map using the provided headers as keys 使用提供的标题作为键将此行转换为Map

      If this row has fewer fields than headers, missing fields map to null. If this row has more fields than headers, extra fields are ignored.

      如果此行的字段少于标题,缺少的字段映射为null。 如果此行的字段多于标题,多余的字段被忽略。

      Parameters:
      headers - the header names | 标题名
      Returns:
      an unmodifiable map of header-to-value | 不可修改的标题到值的Map
      Throws:
      NullPointerException - if headers is null | 如果headers为null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object