Class CsvField

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

public final class CsvField extends Object
CSV Field - Utility class for field value conversion CSV字段 - 字段值转换工具类

Provides type-safe conversion methods for CSV field values. All conversion failures throw OpenCsvException with clear diagnostic messages.

提供CSV字段值的类型安全转换方法。所有转换失败都会抛出带有清晰诊断消息的 OpenCsvException

Features | 主要功能:

  • Type conversion: String, int, long, double, boolean, BigDecimal - 类型转换
  • Date/time parsing with DateTimeFormatter - 使用DateTimeFormatter解析日期时间
  • Null and blank detection - 空值和空白检测
  • Clear error messages for conversion failures - 转换失败的清晰错误消息

Usage Examples | 使用示例:

int age = CsvField.asInt("30");
BigDecimal amount = CsvField.asBigDecimal("1234.56");
LocalDate date = CsvField.asLocalDate("2024-01-15", DateTimeFormatter.ISO_LOCAL_DATE);
boolean blank = CsvField.isBlank("");

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
Since:
JDK 25, opencode-base-csv V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static BigDecimal
    Converts a field value to BigDecimal 将字段值转换为BigDecimal
    static boolean
    Converts a field value to boolean 将字段值转换为boolean
    static double
    Converts a field value to double 将字段值转换为double
    static int
    asInt(String field)
    Converts a field value to int 将字段值转换为int
    static LocalDate
    Converts a field value to LocalDate using the given formatter 使用给定的格式化器将字段值转换为LocalDate
    Converts a field value to LocalDateTime using the given formatter 使用给定的格式化器将字段值转换为LocalDateTime
    static long
    asLong(String field)
    Converts a field value to long 将字段值转换为long
    static String
    Returns the field value as a string (identity conversion) 将字段值作为字符串返回(恒等转换)
    static boolean
    isBlank(String field)
    Checks if a field value is blank (null, empty, or whitespace only) 检查字段值是否为空白(null、空或仅空格)

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • asString

      public static String asString(String field)
      Returns the field value as a string (identity conversion) 将字段值作为字符串返回(恒等转换)
      Parameters:
      field - the field value | 字段值
      Returns:
      the string value | 字符串值
    • asInt

      public static int asInt(String field)
      Converts a field value to int 将字段值转换为int
      Parameters:
      field - the field value | 字段值
      Returns:
      the int value | int值
      Throws:
      OpenCsvException - if the value cannot be converted | 如果值无法转换
    • asLong

      public static long asLong(String field)
      Converts a field value to long 将字段值转换为long
      Parameters:
      field - the field value | 字段值
      Returns:
      the long value | long值
      Throws:
      OpenCsvException - if the value cannot be converted | 如果值无法转换
    • asDouble

      public static double asDouble(String field)
      Converts a field value to double 将字段值转换为double
      Parameters:
      field - the field value | 字段值
      Returns:
      the double value | double值
      Throws:
      OpenCsvException - if the value cannot be converted | 如果值无法转换
    • asBoolean

      public static boolean asBoolean(String field)
      Converts a field value to boolean 将字段值转换为boolean

      Accepts "true"/"false" (case-insensitive), "1"/"0", "yes"/"no".

      接受 "true"/"false"(不区分大小写)、"1"/"0"、"yes"/"no"。

      Parameters:
      field - the field value | 字段值
      Returns:
      the boolean value | boolean值
      Throws:
      OpenCsvException - if the value cannot be converted | 如果值无法转换
    • asBigDecimal

      public static BigDecimal asBigDecimal(String field)
      Converts a field value to BigDecimal 将字段值转换为BigDecimal
      Parameters:
      field - the field value | 字段值
      Returns:
      the BigDecimal value | BigDecimal值
      Throws:
      OpenCsvException - if the value cannot be converted | 如果值无法转换
    • asLocalDate

      public static LocalDate asLocalDate(String field, DateTimeFormatter formatter)
      Converts a field value to LocalDate using the given formatter 使用给定的格式化器将字段值转换为LocalDate
      Parameters:
      field - the field value | 字段值
      formatter - the date formatter | 日期格式化器
      Returns:
      the LocalDate value | LocalDate值
      Throws:
      OpenCsvException - if the value cannot be parsed | 如果值无法解析
    • asLocalDateTime

      public static LocalDateTime asLocalDateTime(String field, DateTimeFormatter formatter)
      Converts a field value to LocalDateTime using the given formatter 使用给定的格式化器将字段值转换为LocalDateTime
      Parameters:
      field - the field value | 字段值
      formatter - the date-time formatter | 日期时间格式化器
      Returns:
      the LocalDateTime value | LocalDateTime值
      Throws:
      OpenCsvException - if the value cannot be parsed | 如果值无法解析
    • isBlank

      public static boolean isBlank(String field)
      Checks if a field value is blank (null, empty, or whitespace only) 检查字段值是否为空白(null、空或仅空格)
      Parameters:
      field - the field value | 字段值
      Returns:
      true if blank | 如果为空白返回true