使用 Univocity Parser 验证解析的字段
Validate parsed fields using Univocity Parser
我想知道在使用 CsvRoutines 包时是否有一种方法可以检查和验证字段。基本上,如果第一列只有数字,我想处理一行,否则 skip/possibly 会抛出异常。我猜 2.7.0 中发布的 @Validate 注解可以用来实现这一点。但我想知道是否有任何其他方法可以实现与 2.5.9 等早期版本相同的功能?
这里是图书馆的作者。除了更新到最新版本别无他法。有什么特别的原因不能升级吗?
更新:您可以将 @Parsed
注释放在 class' getter 或 setter 上,并在其中执行验证。这可能是最干净的方法。例如:
class Test {
private Integer number;
//accepts a String here... so this is straight from the parser before it tries to convert anything into an integer - which lets you validate or throw a custom exception
@Parsed
void setNumber(String number){
try{
this.number = Integer.valueOf(number);
} catch(NumberFormatException e){
throw new IllegalArgumentException(number + " is not a valid integer");
}
}
}
另一种选择是使用自定义转换 class。复制最新版本中使用的classValidatedConversion的代码,然后创建子class,如:
public static class RangeLimiter extends ValidatedConversion {
int min;
int max;
public RangeLimiter(String[] args) {
super(false, false); //not null, not blank
min = Integer.parseInt(args[0]);
max = Integer.parseInt(args[1]);
}
protected void validate(Object value) {
super.validate(value); //runs the existing validations for not null and not blank
int v = ((Number) value).intValue();
if (v < min || v > max) {
throw new DataValidationException("out of range: " + min + " >= " + value + " <=" + max);
}
}
}
现在在你的代码中,使用这个:
@Parsed(field = "number")
@Convert(conversionClass = RangeLimiter.class, args = {"1", "10"}) //min = 1, max = 10
public int number;
我没有针对旧版本对此进行测试。我认为您可能需要在 @Parsed
注释中设置标志 applyDefaultConversion=false
,并使您的转换 class 将 String
转换为 int
除了 运行 验证。
总而言之,只需升级到最新版本即可轻松避免这些工作。
我想知道在使用 CsvRoutines 包时是否有一种方法可以检查和验证字段。基本上,如果第一列只有数字,我想处理一行,否则 skip/possibly 会抛出异常。我猜 2.7.0 中发布的 @Validate 注解可以用来实现这一点。但我想知道是否有任何其他方法可以实现与 2.5.9 等早期版本相同的功能?
这里是图书馆的作者。除了更新到最新版本别无他法。有什么特别的原因不能升级吗?
更新:您可以将 @Parsed
注释放在 class' getter 或 setter 上,并在其中执行验证。这可能是最干净的方法。例如:
class Test {
private Integer number;
//accepts a String here... so this is straight from the parser before it tries to convert anything into an integer - which lets you validate or throw a custom exception
@Parsed
void setNumber(String number){
try{
this.number = Integer.valueOf(number);
} catch(NumberFormatException e){
throw new IllegalArgumentException(number + " is not a valid integer");
}
}
}
另一种选择是使用自定义转换 class。复制最新版本中使用的classValidatedConversion的代码,然后创建子class,如:
public static class RangeLimiter extends ValidatedConversion {
int min;
int max;
public RangeLimiter(String[] args) {
super(false, false); //not null, not blank
min = Integer.parseInt(args[0]);
max = Integer.parseInt(args[1]);
}
protected void validate(Object value) {
super.validate(value); //runs the existing validations for not null and not blank
int v = ((Number) value).intValue();
if (v < min || v > max) {
throw new DataValidationException("out of range: " + min + " >= " + value + " <=" + max);
}
}
}
现在在你的代码中,使用这个:
@Parsed(field = "number")
@Convert(conversionClass = RangeLimiter.class, args = {"1", "10"}) //min = 1, max = 10
public int number;
我没有针对旧版本对此进行测试。我认为您可能需要在 @Parsed
注释中设置标志 applyDefaultConversion=false
,并使您的转换 class 将 String
转换为 int
除了 运行 验证。
总而言之,只需升级到最新版本即可轻松避免这些工作。