Search in sources :

Example 1 with Converter

use of org.simpleflatmapper.converter.Converter 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 Converter

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

the class JodaTimeConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ConverterFactory<?, ?>> consumer) {
    // Date to joda time
    factoryConverter(consumer, new AbstractConverterFactory<Date, DateTime>(Date.class, DateTime.class) {

        @Override
        public Converter<Date, DateTime> newConverter(ConvertingTypes targetedTypes, 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 AbstractConverterFactory<LocalDateTime, Date>(LocalDateTime.class, Date.class) {

        @Override
        public Converter<LocalDateTime, Date> newConverter(ConvertingTypes targetedTypes, Object... params) {
            return new JodaLocalDateTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<LocalTime, Date>(LocalTime.class, Date.class) {

        @Override
        public Converter<LocalTime, Date> newConverter(ConvertingTypes targetedTypes, Object... params) {
            return new JodaLocalTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
        }
    });
    // char sequence to joda time
    factoryConverter(consumer, new AbstractMultiFormatConverterFactory<CharSequence, DateTime>(CharSequence.class, DateTime.class) {

        @Override
        protected Converter<CharSequence, DateTime> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaDateTimeConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatConverterFactory<CharSequence, Instant>(CharSequence.class, Instant.class) {

        @Override
        protected Converter<CharSequence, Instant> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaInstantConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatConverterFactory<CharSequence, LocalDate>(CharSequence.class, LocalDate.class) {

        @Override
        protected Converter<CharSequence, LocalDate> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaLocalDateConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatConverterFactory<CharSequence, LocalDateTime>(CharSequence.class, LocalDateTime.class) {

        @Override
        protected Converter<CharSequence, LocalDateTime> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaLocalDateTimeConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatConverterFactory<CharSequence, LocalTime>(CharSequence.class, LocalTime.class) {

        @Override
        protected Converter<CharSequence, LocalTime> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaLocalTimeConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<ReadableInstant, String>(ReadableInstant.class, String.class) {

        @Override
        public Converter<? super ReadableInstant, String> newConverter(ConvertingTypes targetedTypes, Object... params) {
            DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
            if (dateTimeFormatter != null) {
                return new JodaReadableInstantToStringConverter(dateTimeFormatter);
            } else {
                return ToStringConverter.INSTANCE;
            }
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<ReadablePartial, String>(ReadablePartial.class, String.class) {

        @Override
        public Converter<? super ReadablePartial, String> newConverter(ConvertingTypes targetedTypes, Object... params) {
            DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
            if (dateTimeFormatter != null) {
                return new JodaReadablePartialToStringConverter(dateTimeFormatter);
            } else {
                return ToStringConverter.INSTANCE;
            }
        }
    });
}
Also used : DateToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaDateTimeConverter) LocalDateTime(org.joda.time.LocalDateTime) DateToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateTimeConverter) JodaInstantTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaInstantTojuDateConverter) ReadableInstant(org.joda.time.ReadableInstant) JodaReadablePartialToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadablePartialToStringConverter) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) JodaReadableInstantToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadableInstantToStringConverter) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) CharSequenceToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaDateTimeConverter) ReadablePartial(org.joda.time.ReadablePartial) JodaLocalTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalTimeTojuDateConverter) CharSequenceToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaInstantConverter) CharSequenceToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaDateTimeConverter) JodaReadableInstantToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadableInstantToStringConverter) DateToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaDateTimeConverter) JodaReadablePartialToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadablePartialToStringConverter) CharSequenceToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalTimeConverter) DateToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateConverter) CharSequenceToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateTimeConverter) DateToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaInstantConverter) JodaLocalDateTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTojuDateConverter) ToStringConverter(org.simpleflatmapper.converter.ToStringConverter) CharSequenceToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateConverter) DateToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalTimeConverter) JodaLocalTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalTimeTojuDateConverter) JodaLocalDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTimeTojuDateConverter) Converter(org.simpleflatmapper.converter.Converter) JodaInstantTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaInstantTojuDateConverter) JodaDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaDateTimeTojuDateConverter) DateToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateTimeConverter) JodaLocalDateTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTojuDateConverter) JodaDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaDateTimeTojuDateConverter) CharSequenceToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaInstantConverter) DateToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateConverter) LocalTime(org.joda.time.LocalTime) CharSequenceToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalTimeConverter) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) CharSequenceToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateConverter) JodaLocalDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTimeTojuDateConverter) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) DateToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaInstantConverter) CharSequenceToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateTimeConverter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalTimeConverter)

Example 3 with Converter

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

the class SettableDataSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<SettableByIndexData, P> getSetter(PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>> arg) {
    Setter<SettableByIndexData, P> setter = null;
    Type propertyType = arg.getPropertyMeta().getPropertyType();
    final DataType dataType = arg.getColumnKey().getDataType();
    Type type = dataType != null ? DataTypeHelper.asJavaClass(dataType) : null;
    if (type == null) {
        type = propertyType;
    }
    if (TypeHelper.isEnum(propertyType)) {
        if (TypeHelper.areEquals(type, String.class)) {
            return (Setter<SettableByIndexData, P>) new StringEnumSettableDataSetter(arg.getColumnKey().getIndex());
        } else {
            return (Setter<SettableByIndexData, P>) new OrdinalEnumSettableDataSetter(arg.getColumnKey().getIndex());
        }
    }
    SetterFactory<SettableByIndexData, PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>>> setterFactory = this.factoryPerClass.get(TypeHelper.toClass(type));
    if (setterFactory != null) {
        setter = setterFactory.getSetter(arg);
        if (!TypeHelper.areEquals(TypeHelper.toBoxedClass(type), TypeHelper.toBoxedClass(propertyType))) {
            Converter<?, ?> converter = getConverter(propertyType, TypeHelper.toClass(type), dataType, arg.getColumnDefinition());
            if (converter != null) {
                setter = (Setter<SettableByIndexData, P>) new ConvertDelegateSetter<SettableByIndexData, Object, P>(setter, (Converter<Object, P>) converter);
            } else {
                setter = null;
            }
        }
    }
    if (setter == null && dataType != null) {
        if (dataType instanceof UserType) {
            if (propertyType.equals(UDTValue.class)) {
                setter = (Setter<SettableByIndexData, P>) new UDTValueSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = (Setter<SettableByIndexData, P>) UDTObjectSettableDataSetter.newInstance(propertyType, (UserType) dataType, arg.getColumnKey().getIndex(), mapperConfig, reflectionService);
            }
        } else if (TypeHelper.isAssignable(List.class, type) && TypeHelper.isAssignable(List.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new ListSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new ListWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Set.class, type) && TypeHelper.isAssignable(Set.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new SetSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new SetWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Map.class, type) && TypeHelper.isAssignable(Map.class, propertyType)) {
            DataType dtKeyType = dataType.getTypeArguments().get(0);
            DataType dtValueType = dataType.getTypeArguments().get(1);
            TypeHelper.MapEntryTypes keyValueTypeOfMap = TypeHelper.getKeyValueTypeOfMap(propertyType);
            if (areSame(dtKeyType, keyValueTypeOfMap.getKeyType()) && areSame(dtValueType, keyValueTypeOfMap.getValueType())) {
                setter = new MapSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = new MapWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), getConverter(keyValueTypeOfMap.getKeyType(), DataTypeHelper.asJavaClass(dtKeyType), dtKeyType, arg.getColumnDefinition()), getConverter(keyValueTypeOfMap.getValueType(), DataTypeHelper.asJavaClass(dtValueType), dtValueType, arg.getColumnDefinition()));
            }
        }
    }
    return setter;
}
Also used : OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) Converter(org.simpleflatmapper.converter.Converter) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ColumnDefinition(org.simpleflatmapper.map.mapper.ColumnDefinition) Type(java.lang.reflect.Type) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) DoubleSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter) BigDecimalSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter) TimeSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimeSettableDataSetter) BigIntegerSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter) IntSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.IntSettableDataSetter) UUIDSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UUIDSettableDataSetter) TupleValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TupleValueSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) InetAddressSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.InetAddressSettableDataSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) ShortSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ShortSettableDataSetter) UDTObjectSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter) DateSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter) TimestampSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimestampSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) FloatSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.FloatSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) LongSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.LongSettableDataSetter) OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) Setter(org.simpleflatmapper.reflect.Setter) ByteSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) StringSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter)

Example 4 with Converter

use of org.simpleflatmapper.converter.Converter 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)

Example 5 with Converter

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

the class AbstractMultiFormatConverterFactory method newConverter.

@SuppressWarnings("unchecked")
@Override
public Converter<? super I, ? extends O> newConverter(ConvertingTypes targetedTypes, Object... params) {
    DateTimeFormatter[] dateTimeFormatters = JavaTimeHelper.getDateTimeFormatters(params);
    if (dateTimeFormatters.length == 0) {
        return null;
    }
    ZoneId zoneId = JavaTimeHelper.getZoneId(params);
    Converter<I, O>[] converters = new Converter[dateTimeFormatters.length];
    for (int i = 0; i < dateTimeFormatters.length; i++) {
        DateTimeFormatter dateTimeFormatter = dateTimeFormatters[i];
        if (dateTimeFormatter.getZone() == null) {
            dateTimeFormatter.withZone(zoneId);
        }
        converters[i] = newConverter(dateTimeFormatter);
    }
    if (converters.length == 1) {
        return converters[0];
    } else {
        return new MultiDateTimeFormatterConverter<I, O>(converters);
    }
}
Also used : ZoneId(java.time.ZoneId) Converter(org.simpleflatmapper.converter.Converter) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

Converter (org.simpleflatmapper.converter.Converter)8 Type (java.lang.reflect.Type)3 ConvertingTypes (org.simpleflatmapper.converter.ConvertingTypes)3 ResultSet (java.sql.ResultSet)2 Date (java.util.Date)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 ConvertingScore (org.simpleflatmapper.converter.ConvertingScore)2 ToStringConverter (org.simpleflatmapper.converter.ToStringConverter)2 Array (java.sql.Array)1 SQLException (java.sql.SQLException)1 Time (java.sql.Time)1 LocalTime (java.time.LocalTime)1 OffsetTime (java.time.OffsetTime)1 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1