Class CsvField
java.lang.Object
cloud.opencode.base.csv.CsvField
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 TypeMethodDescriptionstatic BigDecimalasBigDecimal(String field) Converts a field value to BigDecimal 将字段值转换为BigDecimalstatic booleanConverts a field value to boolean 将字段值转换为booleanstatic doubleConverts a field value to double 将字段值转换为doublestatic intConverts a field value to int 将字段值转换为intstatic LocalDateasLocalDate(String field, DateTimeFormatter formatter) Converts a field value to LocalDate using the given formatter 使用给定的格式化器将字段值转换为LocalDatestatic LocalDateTimeasLocalDateTime(String field, DateTimeFormatter formatter) Converts a field value to LocalDateTime using the given formatter 使用给定的格式化器将字段值转换为LocalDateTimestatic longConverts a field value to long 将字段值转换为longstatic StringReturns the field value as a string (identity conversion) 将字段值作为字符串返回(恒等转换)static booleanChecks if a field value is blank (null, empty, or whitespace only) 检查字段值是否为空白(null、空或仅空格)
-
Method Details
-
asString
-
asInt
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
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
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
Converts a field value to boolean 将字段值转换为booleanAccepts "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
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
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
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
Checks if a field value is blank (null, empty, or whitespace only) 检查字段值是否为空白(null、空或仅空格)- Parameters:
field- the field value | 字段值- Returns:
- true if blank | 如果为空白返回true
-