use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.
the class JodaTimeConverterFactoryProducer method produce.
@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
// Date to joda time
factoryConverter(consumer, new AbstractContextualConverterFactory<Date, DateTime>(Date.class, DateTime.class) {
@Override
public ContextualConverter<Date, DateTime> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
return new DateToJodaDateTimeConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
}
});
constantConverter(consumer, Date.class, Instant.class, new DateToJodaInstantConverter());
constantConverter(consumer, Date.class, LocalDate.class, new DateToJodaLocalDateConverter());
constantConverter(consumer, Date.class, LocalDateTime.class, new DateToJodaLocalDateTimeConverter());
constantConverter(consumer, Date.class, LocalTime.class, new DateToJodaLocalTimeConverter());
// joda time to date
constantConverter(consumer, DateTime.class, Date.class, new JodaDateTimeTojuDateConverter());
constantConverter(consumer, Instant.class, Date.class, new JodaInstantTojuDateConverter());
constantConverter(consumer, LocalDate.class, Date.class, new JodaLocalDateTojuDateConverter());
factoryConverter(consumer, new AbstractContextualConverterFactory<LocalDateTime, Date>(LocalDateTime.class, Date.class) {
@Override
public ContextualConverter<LocalDateTime, Date> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
return new JodaLocalDateTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<LocalTime, Date>(LocalTime.class, Date.class) {
@Override
public ContextualConverter<LocalTime, Date> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
return new JodaLocalTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
}
});
// char sequence to joda time
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, DateTime>(CharSequence.class, DateTime.class) {
@Override
protected ContextualConverter<CharSequence, DateTime> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaDateTimeConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, Instant>(CharSequence.class, Instant.class) {
@Override
protected ContextualConverter<CharSequence, Instant> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaInstantConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalDate>(CharSequence.class, LocalDate.class) {
@Override
protected ContextualConverter<CharSequence, LocalDate> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaLocalDateConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalDateTime>(CharSequence.class, LocalDateTime.class) {
@Override
protected ContextualConverter<CharSequence, LocalDateTime> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaLocalDateTimeConverter(formatter);
}
});
factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalTime>(CharSequence.class, LocalTime.class) {
@Override
protected ContextualConverter<CharSequence, LocalTime> newConverter(DateTimeFormatter formatter) {
return new CharSequenceToJodaLocalTimeConverter(formatter);
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<ReadableInstant, String>(ReadableInstant.class, String.class) {
@Override
public ContextualConverter<? super ReadableInstant, String> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
if (dateTimeFormatter != null) {
return new JodaReadableInstantToStringConverter(dateTimeFormatter);
} else {
return ToStringConverter.INSTANCE;
}
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<ReadablePartial, String>(ReadablePartial.class, String.class) {
@Override
public ContextualConverter<? super ReadablePartial, String> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
if (dateTimeFormatter != null) {
return new JodaReadablePartialToStringConverter(dateTimeFormatter);
} else {
return ToStringConverter.INSTANCE;
}
}
});
// IFJAVA8_START
constantConverter(consumer, LocalDateTime.class, java.time.LocalDateTime.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalDateTimeTojuLocalDateTimeConverter());
constantConverter(consumer, LocalDate.class, java.time.LocalDate.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalDateTojuLocalDateConverter());
constantConverter(consumer, LocalTime.class, java.time.LocalTime.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalTimeTojuLocalTimeConverter());
// IFJAVA8_END
constantConverter(consumer, LocalTime.class, Long.class, new ContextualConverter<LocalTime, Long>() {
@Override
public Long convert(LocalTime in, Context context) throws Exception {
if (in == null)
return null;
return TimeUnit.MILLISECONDS.toNanos(in.getMillisOfDay());
}
});
}
use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.
the class CsvMapperImplTest method testCsvFieldMappingError.
@Test
public void testCsvFieldMappingError() throws IOException {
CsvMapper<Integer> mapper = CsvMapperFactory.newInstance().newMapper(Integer.class);
final Iterator<Integer> iterator = mapper.iterator(new StringReader("value\n1\n2\nnnn"));
assertEquals(1, iterator.next().intValue());
assertEquals(2, iterator.next().intValue());
try {
iterator.next();
} catch (NumberFormatException e) {
System.out.println(e.toString());
}
CsvMapper<Integer> csvMapper = CsvMapperFactory.newInstance().fieldMapperErrorHandler(new FieldMapperErrorHandler<CsvColumnKey>() {
@Override
public void errorMappingField(CsvColumnKey key, Object source, Object target, Exception error, Context mappingContext) throws MappingException {
System.out.println(error.toString());
}
}).newMapper(Integer.class);
Integer next = csvMapper.iterator(new StringReader("val\nnnnn")).next();
System.out.println("next = " + next);
}
use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.
the class CsvMapperImplTest method testConverterProperty.
@Test
public void testConverterProperty() throws IOException {
DbObject d = CsvParser.mapWith(CsvMapperFactory.newInstance().addColumnProperty("typeName", ConverterProperty.of(new ContextualConverter<String, DbObject.Type>() {
@Override
public DbObject.Type convert(String in, Context context) throws Exception {
return DbObject.Type.shortForm(in);
}
})).newMapper(DbObject.class)).iterator("typeName\nt1").next();
assertEquals(d.getTypeName(), DbObject.Type.type1);
}
use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.
the class JooqConverterFactoryProducer method produce.
@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
this.constantConverter(consumer, byte.class, UByte.class, new ContextualConverter<Byte, UByte>() {
@Override
public UByte convert(Byte in, Context context) throws Exception {
if (in == null) {
return null;
}
return UByte.valueOf(in);
}
});
this.constantConverter(consumer, short.class, UByte.class, new ContextualConverter<Short, UByte>() {
@Override
public UByte convert(Short in, Context context) throws Exception {
if (in == null) {
return null;
}
return UByte.valueOf(in);
}
});
this.constantConverter(consumer, int.class, UByte.class, new ContextualConverter<Integer, UByte>() {
@Override
public UByte convert(Integer in, Context context) throws Exception {
if (in == null) {
return null;
}
return UByte.valueOf(in);
}
});
this.constantConverter(consumer, long.class, UByte.class, new ContextualConverter<Long, UByte>() {
@Override
public UByte convert(Long in, Context context) throws Exception {
if (in == null) {
return null;
}
return UByte.valueOf(in);
}
});
this.constantConverter(consumer, short.class, UShort.class, new ContextualConverter<Short, UShort>() {
@Override
public UShort convert(Short in, Context context) throws Exception {
if (in == null) {
return null;
}
return UShort.valueOf(in);
}
});
this.constantConverter(consumer, int.class, UShort.class, new ContextualConverter<Integer, UShort>() {
@Override
public UShort convert(Integer in, Context context) throws Exception {
if (in == null) {
return null;
}
return UShort.valueOf(in);
}
});
this.constantConverter(consumer, int.class, UInteger.class, new ContextualConverter<Integer, UInteger>() {
@Override
public UInteger convert(Integer in, Context context) throws Exception {
if (in == null) {
return null;
}
return UInteger.valueOf(in);
}
});
this.constantConverter(consumer, long.class, UInteger.class, new ContextualConverter<Long, UInteger>() {
@Override
public UInteger convert(Long in, Context context) throws Exception {
if (in == null) {
return null;
}
return UInteger.valueOf(in);
}
});
this.constantConverter(consumer, long.class, ULong.class, new ContextualConverter<Long, ULong>() {
@Override
public ULong convert(Long in, Context context) throws Exception {
if (in == null) {
return null;
}
return ULong.valueOf(in);
}
});
this.constantConverter(consumer, BigInteger.class, ULong.class, new ContextualConverter<BigInteger, ULong>() {
@Override
public ULong convert(BigInteger in, Context context) throws Exception {
if (in == null) {
return null;
}
return ULong.valueOf(in);
}
});
this.constantConverter(consumer, String.class, JSONObject.class, new ContextualConverter<String, JSONObject>() {
@Override
public JSONObject convert(String in, Context context) throws Exception {
if (in == null) {
return null;
}
JSONParser parser = new JSONParser();
return (JSONObject) parser.parse(in);
}
});
}
use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperEnumTest method testIndexedEnumFactoryMethod.
@Test
public void testIndexedEnumFactoryMethod() throws Exception {
JdbcMapperBuilder<DbEnumObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(DbEnumObject.class);
builder.addMapping("val", 1, Types.VARCHAR, new Object[] { ConverterProperty.of(new ContextualConverter<String, DbObject.Type>() {
@Override
public DbObject.Type convert(String s, Context context) throws Exception {
return DbObject.Type.shortForm(s);
}
}) });
JdbcMapper<DbEnumObject> mapper = builder.mapper();
ResultSet rs = mock(ResultSet.class);
when(rs.getString(1)).thenReturn("t2");
assertEquals(DbObject.Type.type2, mapper.map(rs).getVal());
}
Aggregations