use of org.simpleflatmapper.converter.ContextFactoryBuilder in project SimpleFlatMapper by arnaudroger.
the class JdbcConverterFactoryProducer method produce.
@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
// IFJAVA8_START
constantConverter(consumer, Time.class, LocalTime.class, new TimeToLocalTimeConverter());
constantConverter(consumer, Date.class, LocalDate.class, new DateToLocalDateConverter());
factoryConverter(consumer, new AbstractContextualConverterFactory<Time, OffsetTime>(Time.class, OffsetTime.class) {
@Override
public ContextualConverter<Time, OffsetTime> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
ZoneOffset zoneOffset = getZoneOffset(params);
return new TimeToOffsetTimeConverter(zoneOffset);
}
@SuppressWarnings("unchecked")
private ZoneOffset getZoneOffset(Object[] params) {
for (Object prop : params) {
if (prop instanceof ZoneOffset) {
return (ZoneOffset) prop;
} else if (SupplierHelper.isSupplierOf(prop, ZoneOffset.class)) {
return ((Supplier<ZoneOffset>) prop).get();
}
}
return ZoneOffset.UTC;
}
});
// IFJAVA8_END
factoryConverter(consumer, new AbstractContextualConverterFactory<Array, Object>(Array.class, java.lang.reflect.Array.class) {
@Override
public ContextualConverter<? super Array, ? extends Object> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
return new SqlArrayToJavaArrayConverter<Object>(TypeHelper.<Object>toClass(elementType), getter);
}
@Override
public ConvertingScore score(ConvertingTypes targetedTypes) {
return new ConvertingScore(super.score(targetedTypes).getFromScore(), TypeHelper.isArray(targetedTypes.getTo()) ? 1 : -1);
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<Array, List>(Array.class, List.class) {
@Override
public ContextualConverter<? super Array, ? extends List> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
return new SqlArrayToListConverter<Object>(getter);
}
});
constantConverter(consumer, Calendar.class, Timestamp.class, new CalendarToTimestampConverter());
constantConverter(consumer, java.util.Date.class, Timestamp.class, new UtilDateToTimestampConverter());
constantConverter(consumer, java.util.Date.class, Time.class, new UtilDateToTimeConverter());
constantConverter(consumer, java.util.Date.class, Date.class, new UtilDateToDateConverter());
constantConverter(consumer, Blob.class, byte[].class, new BlobToByteConverter());
}
use of org.simpleflatmapper.converter.ContextFactoryBuilder 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());
}
});
}
Aggregations