Hello Team,
I have another question regarding my small project that I'm doing with your lib. I have a Bean with 4 columns, 3 of them are, let's say normal, but in the last one I'm using my own converter. This is how it looks like:
`@AllArgsConstructor
@NoArgsConstructor
@Data
public class CSVItem {
@CsvBindByPosition(position = 0, required = true)
private String title;
@CsvBindByPosition(position = 1, required = true)
private String authorOrMagazineNumber;
@CsvBindByPosition(position = 2, required = true)
private int quantity;
@CsvCustomBindByPosition(position = 3, converter = TextToEnum.class, required = true)
public ItemType type;
}`
The Last column in my CSV file can be only B or M so I have prepared converter TextToEnum.
This is enum:
public enum ItemType {
BOOK("B"),
MAGAZINE("M");
private String value;
ItemType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
This is converter:
public class TextToEnum extends AbstractBeanField {
@Override
protected Object convert(String s) throws CsvDataTypeMismatchException, CsvConstraintViolationException {
for(ItemType type : ItemType.values()) {
if(type.getValue().equalsIgnoreCase(s)){
return type;
}
}
throw new IllegalArgumentException("Unable to find item type " + s);
}
}
I don't want to thow exceptions during parsing file so I have disabled this option:
CsvToBean<CSVItem> cb = new CsvToBeanBuilder<CSVItem>(inputStreamReader)
.withType(CSVItem.class)
.withThrowExceptions(false)
.build();
There is no problem when a parsing exception occurs for first 3 columns, it is not thrown ,but is collected after data processing for futher review.
But the last column must be B or M, otherwise exception IllegalArgumentException("Unable to find item type " + s) is thrown. Even if withThrowExceptions is set to false, exception from my converter is still thown.
When withThrowExceptions is set to true, all the exception is thrown.
In other words collecting parsing excetpion works only for 'built-in' excetions, but for own converion exception does not? It there any way to collect my own parsing exception together with 'built-in' excetpions?
I hope that my problem was described properly.
OK! This was easy... I have solved my problem. I made my own exception that throw: throw new CsvDataTypeMismatchException("Unable to find item type " + s);
Please close the ticket . ;-)
Actually, this is exactly what CsvConstraintViolationException was created for. You have placed a constraint on the last field (it must be "B" or "M") but the input data violate that. It would work just as well as CsvDataTypeMismatchException, but it would be more in keeping with the intentions of the library as developed. Your call.