Search in sources :

Example 1 with ContextFactoryBuilder

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());
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) ZoneOffset(java.time.ZoneOffset) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) ContextFactoryBuilder(org.simpleflatmapper.converter.ContextFactoryBuilder) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) OffsetTime(java.time.OffsetTime) ResultSet(java.sql.ResultSet) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) Supplier(org.simpleflatmapper.util.Supplier) List(java.util.List) ConvertingScore(org.simpleflatmapper.converter.ConvertingScore) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) Array(java.sql.Array) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter)

Example 2 with ContextFactoryBuilder

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());
        }
    });
}
Also used : DateToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaDateTimeConverter) DateToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateTimeConverter) JodaInstantTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaInstantTojuDateConverter) ReadableInstant(org.joda.time.ReadableInstant) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) ContextFactoryBuilder(org.simpleflatmapper.converter.ContextFactoryBuilder) ReadablePartial(org.joda.time.ReadablePartial) JodaLocalTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalTimeTojuDateConverter) DateToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateConverter) LocalTime(org.joda.time.LocalTime) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) CharSequenceToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateConverter) JodaLocalDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTimeTojuDateConverter) DateToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaInstantConverter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) LocalDateTime(org.joda.time.LocalDateTime) JodaReadablePartialToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadablePartialToStringConverter) JodaReadableInstantToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadableInstantToStringConverter) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) CharSequenceToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaDateTimeConverter) JodaLocalDateTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTojuDateConverter) JodaDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaDateTimeTojuDateConverter) CharSequenceToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaInstantConverter) Context(org.simpleflatmapper.converter.Context) CharSequenceToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalTimeConverter) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) CharSequenceToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateTimeConverter) DateToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalTimeConverter)

Aggregations

ContextFactoryBuilder (org.simpleflatmapper.converter.ContextFactoryBuilder)2 ContextualConverter (org.simpleflatmapper.converter.ContextualConverter)2 ConvertingTypes (org.simpleflatmapper.converter.ConvertingTypes)2 Type (java.lang.reflect.Type)1 Array (java.sql.Array)1 ResultSet (java.sql.ResultSet)1 Time (java.sql.Time)1 LocalTime (java.time.LocalTime)1 OffsetTime (java.time.OffsetTime)1 ZoneOffset (java.time.ZoneOffset)1 Date (java.util.Date)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 Instant (org.joda.time.Instant)1 LocalDate (org.joda.time.LocalDate)1 LocalDateTime (org.joda.time.LocalDateTime)1 LocalTime (org.joda.time.LocalTime)1 ReadableInstant (org.joda.time.ReadableInstant)1 ReadablePartial (org.joda.time.ReadablePartial)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1