60.2.10. 10.FormatFactories
注释 @FormatFactories
的目的是在记录级别上定义一组转换器。提供的类必须实施 FormatFactoryInterface
接口。
@CsvRecord(separator = ",") @FormatFactories({OrderNumberFormatFactory.class}) public static class Order { @DataField(pos = 1) private OrderNumber orderNr; @DataField(pos = 2) private String firstName; } public static class OrderNumber { private int orderNr; public static OrderNumber ofString(String orderNumber) { OrderNumber result = new OrderNumber(); result.orderNr = Integer.valueOf(orderNumber); return result; } } public static class OrderNumberFormatFactory extends AbstractFormatFactory { { supportedClasses.add(OrderNumber.class); } @Override public Format<?> build(FormattingOptions formattingOptions) { return new Format<OrderNumber>() { @Override public String format(OrderNumber object) throws Exception { return String.valueOf(object.orderNr); } @Override public OrderNumber parse(String string) throws Exception { return OrderNumber.ofString(string); } }; } }
@CsvRecord(separator = ",")
@FormatFactories({OrderNumberFormatFactory.class})
public static class Order {
@DataField(pos = 1)
private OrderNumber orderNr;
@DataField(pos = 2)
private String firstName;
}
public static class OrderNumber {
private int orderNr;
public static OrderNumber ofString(String orderNumber) {
OrderNumber result = new OrderNumber();
result.orderNr = Integer.valueOf(orderNumber);
return result;
}
}
public static class OrderNumberFormatFactory extends AbstractFormatFactory {
{
supportedClasses.add(OrderNumber.class);
}
@Override
public Format<?> build(FormattingOptions formattingOptions) {
return new Format<OrderNumber>() {
@Override
public String format(OrderNumber object) throws Exception {
return String.valueOf(object.orderNr);
}
@Override
public OrderNumber parse(String string) throws Exception {
return OrderNumber.ofString(string);
}
};
}
}