Search in sources :

Example 1 with ConvertingScore

use of org.simpleflatmapper.converter.ConvertingScore in project SimpleFlatMapper by arnaudroger.

the class JdbcConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ConverterFactory<?, ?>> consumer) {
    // IFJAVA8_START
    constantConverter(consumer, Time.class, LocalTime.class, new TimeToLocalTimeConverter());
    constantConverter(consumer, Date.class, LocalDate.class, new DateToLocalDateConverter());
    factoryConverter(consumer, new AbstractConverterFactory<Time, OffsetTime>(Time.class, OffsetTime.class) {

        @Override
        public Converter<Time, OffsetTime> newConverter(ConvertingTypes targetedTypes, 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 AbstractConverterFactory<Array, Object>(Array.class, java.lang.reflect.Array.class) {

        @Override
        public Converter<? super Array, ? extends Object> newConverter(ConvertingTypes targetedTypes, 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 AbstractConverterFactory<Array, List>(Array.class, List.class) {

        @Override
        public Converter<? super Array, ? extends List> newConverter(ConvertingTypes targetedTypes, 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());
}
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) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) OffsetTime(java.time.OffsetTime) ResultSet(java.sql.ResultSet) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) Converter(org.simpleflatmapper.converter.Converter) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) Supplier(org.simpleflatmapper.util.Supplier) List(java.util.List) ConvertingScore(org.simpleflatmapper.converter.ConvertingScore) Array(java.sql.Array) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter)

Example 2 with ConvertingScore

use of org.simpleflatmapper.converter.ConvertingScore in project SimpleFlatMapper by arnaudroger.

the class JavaBaseConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ConverterFactory<?, ?>> consumer) {
    constantConverter(consumer, Number.class, Byte.class, new NumberByteConverter());
    constantConverter(consumer, Number.class, Short.class, new NumberShortConverter());
    constantConverter(consumer, Number.class, Integer.class, new NumberIntegerConverter());
    constantConverter(consumer, Number.class, Long.class, new NumberLongConverter());
    constantConverter(consumer, Number.class, Float.class, new NumberFloatConverter());
    constantConverter(consumer, Number.class, Double.class, new NumberDoubleConverter());
    constantConverter(consumer, Number.class, BigDecimal.class, new NumberBigDecimalConverter());
    constantConverter(consumer, Number.class, BigInteger.class, new NumberBigIntegerConverter());
    constantConverter(consumer, CharSequence.class, Byte.class, new CharSequenceByteConverter());
    constantConverter(consumer, CharSequence.class, BigInteger.class, new CharSequenceBigInteger());
    constantConverter(consumer, CharSequence.class, BigDecimal.class, new CharSequenceBigDecimal());
    constantConverter(consumer, CharSequence.class, Character.class, new CharSequenceCharacterConverter());
    constantConverter(consumer, CharSequence.class, Short.class, new CharSequenceShortConverter());
    constantConverter(consumer, CharSequence.class, Integer.class, new CharSequenceIntegerConverter());
    constantConverter(consumer, CharSequence.class, Long.class, new CharSequenceLongConverter());
    constantConverter(consumer, CharSequence.class, Float.class, new CharSequenceFloatConverter());
    constantConverter(consumer, CharSequence.class, Double.class, new CharSequenceDoubleConverter());
    constantConverter(consumer, CharSequence.class, UUID.class, new CharSequenceUUIDConverter());
    constantConverter(consumer, CharSequence.class, Boolean.class, new CharSequenceBooleanConverter());
    factoryConverter(consumer, new AbstractConverterFactory<CharSequence, Date>(CharSequence.class, Date.class) {

        @Override
        public Converter<? super CharSequence, ? extends Date> newConverter(ConvertingTypes targetedTypes, Object... params) {
            String format = getFormat(params);
            if (format != null) {
                return new CharSequenceToDateConverter(format);
            } else
                return null;
        }

        private String getFormat(Object[] params) {
            String defaultValue = null;
            if (params != null) {
                for (Object o : params) {
                    if (o instanceof DefaultDateFormatSupplier) {
                        defaultValue = ((DefaultDateFormatSupplier) o).get();
                    } else if (o instanceof DateFormatSupplier) {
                        return ((DateFormatSupplier) o).get();
                    }
                }
            }
            return defaultValue;
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<CharSequence, Enum>(CharSequence.class, Enum.class) {

        @SuppressWarnings("unchecked")
        public Converter<? super CharSequence, ? extends Enum> newConverter(ConvertingTypes targetedTypes, Object... params) {
            return new CharSequenceToEnumConverter(TypeHelper.toClass(targetedTypes.getTo()));
        }

        @Override
        public ConvertingScore score(ConvertingTypes targetedTypes) {
            if (TypeHelper.isAssignable(Enum.class, targetedTypes.getTo())) {
                return new ConvertingScore(ConvertingTypes.getSourceScore(convertingTypes.getFrom(), targetedTypes.getFrom()), ConvertingScore.MAX_SCORE);
            }
            return ConvertingScore.NO_MATCH;
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<Number, Enum>(Number.class, Enum.class) {

        @SuppressWarnings("unchecked")
        public Converter<? super Number, ? extends Enum> newConverter(ConvertingTypes targetedTypes, Object... params) {
            return new NumberToEnumConverter(TypeHelper.toClass(targetedTypes.getTo()));
        }

        @Override
        public ConvertingScore score(ConvertingTypes targetedTypes) {
            if (TypeHelper.isAssignable(Enum.class, targetedTypes.getTo())) {
                return new ConvertingScore(ConvertingTypes.getSourceScore(convertingTypes.getFrom(), targetedTypes.getFrom()), ConvertingScore.MAX_SCORE);
            }
            return ConvertingScore.NO_MATCH;
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<Object, Enum>(Object.class, Enum.class) {

        @SuppressWarnings("unchecked")
        public Converter<? super Object, ? extends Enum> newConverter(ConvertingTypes targetedTypes, Object... params) {
            return new ObjectToEnumConverter(TypeHelper.toClass(targetedTypes.getTo()));
        }

        @Override
        public ConvertingScore score(ConvertingTypes targetedTypes) {
            if (TypeHelper.isAssignable(Enum.class, targetedTypes.getTo())) {
                return new ConvertingScore(ConvertingTypes.getSourceScore(convertingTypes.getFrom(), targetedTypes.getFrom()), ConvertingScore.MAX_SCORE);
            }
            return ConvertingScore.NO_MATCH;
        }
    });
    constantConverter(consumer, Object.class, String.class, ToStringConverter.INSTANCE);
    constantConverter(consumer, Object.class, URL.class, new ToStringToURLConverter());
}
Also used : ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) DefaultDateFormatSupplier(org.simpleflatmapper.util.date.DefaultDateFormatSupplier) DateFormatSupplier(org.simpleflatmapper.util.date.DateFormatSupplier) Converter(org.simpleflatmapper.converter.Converter) ToStringConverter(org.simpleflatmapper.converter.ToStringConverter) ConvertingScore(org.simpleflatmapper.converter.ConvertingScore) Date(java.util.Date) DefaultDateFormatSupplier(org.simpleflatmapper.util.date.DefaultDateFormatSupplier)

Aggregations

Converter (org.simpleflatmapper.converter.Converter)2 ConvertingScore (org.simpleflatmapper.converter.ConvertingScore)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 ToStringConverter (org.simpleflatmapper.converter.ToStringConverter)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 DateToLocalDateConverter (org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter)1 TimeToLocalTimeConverter (org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter)1 TimeToOffsetTimeConverter (org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter)1 Getter (org.simpleflatmapper.reflect.Getter)1 Supplier (org.simpleflatmapper.util.Supplier)1 DateFormatSupplier (org.simpleflatmapper.util.date.DateFormatSupplier)1