Search in sources :

Example 6 with Converter

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

the class DiscriminatorJdbcBuilder method mapper.

/**
 * @return a new jdbcMapper based on the current state
 */
public JdbcMapper<T> mapper() {
    List<DiscriminatorMapper.PredicatedMapper<ResultSet, ResultSet, T, SQLException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<ResultSet, ResultSet, T, SQLException>>();
    for (DiscriminatorJdbcSubBuilder subBuilder : builders) {
        JdbcMapper<T> mapper = subBuilder.createMapper();
        Predicate<ResultSet> predicate = new ResultSetDiscriminatorPredicate(column, subBuilder.predicate);
        mappers.add(new DiscriminatorMapper.PredicatedMapper<ResultSet, ResultSet, T, SQLException>(predicate, mapper, mapper));
    }
    DiscriminatorJdbcMapper<T> discriminatorMapper = new DiscriminatorJdbcMapper<T>(mappers, new UnaryFactory<ResultSet, Enumarable<ResultSet>>() {

        @Override
        public Enumarable<ResultSet> newInstance(ResultSet resultSet) {
            return new ResultSetEnumarable(resultSet);
        }
    }, UncheckedConverterHelper.<ResultSet, String>toUnchecked(new Converter<ResultSet, String>() {

        @Override
        public String convert(ResultSet in) throws SQLException {
            return column + ":" + in.getObject(column);
        }
    }), jdbcMapperFactory.consumerErrorHandler());
    return discriminatorMapper;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) ResultSetEnumarable(org.simpleflatmapper.jdbc.impl.ResultSetEnumarable) Enumarable(org.simpleflatmapper.util.Enumarable) ResultSetEnumarable(org.simpleflatmapper.jdbc.impl.ResultSetEnumarable) ResultSet(java.sql.ResultSet) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Converter(org.simpleflatmapper.converter.Converter)

Example 7 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 = JodaTimeHelper.getDateTimeFormatters(params);
    DateTimeZone zoneId = JodaTimeHelper.getDateTimeZoneOrDefault(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 : Converter(org.simpleflatmapper.converter.Converter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeZone(org.joda.time.DateTimeZone)

Example 8 with Converter

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

the class ConstantSourceFieldMapperFactoryImpl method getGettetInstantiator.

private <T, P> Getter<? super S, ? extends P> getGettetInstantiator(ClassMeta<P> classMeta, InstantiatorDefinition id, K key, FieldMapperColumnDefinition<K> columnDefinition, Collection<Type> types) {
    Instantiator<? super T, ? extends P> instantiator = classMeta.getReflectionService().getInstantiatorFactory().getOneArgIdentityInstantiator(id, classMeta.getReflectionService().builderIgnoresNullValues());
    final Type paramType = id.getParameters()[0].getGenericType();
    Getter<? super S, ? extends T> subGetter = getterFactory.newGetter(paramType, key, columnDefinition);
    if (subGetter == null) {
        if (types.contains(paramType)) {
            // loop circuit cutter
            return null;
        }
        types.add(paramType);
        // converter?
        Type sourceType = key.getType(paramType);
        Converter converter = converterService.findConverter(sourceType, paramType, columnDefinition.properties());
        if (converter != null) {
            Getter sourceTypeGetter = getterFactory.newGetter(sourceType, key, columnDefinition);
            subGetter = new GetterWithConverter(converter, sourceTypeGetter);
        } else {
            subGetter = lookForInstantiatorGetter(classMeta.getReflectionService().<T>getClassMeta(paramType), key, columnDefinition, types);
        }
    }
    if (subGetter != null) {
        return new InstantiatorGetter<T, S, P>(instantiator, subGetter);
    } else
        return null;
}
Also used : InstantiatorGetter(org.simpleflatmapper.reflect.getter.InstantiatorGetter) Type(java.lang.reflect.Type) Getter(org.simpleflatmapper.reflect.Getter) InstantiatorGetter(org.simpleflatmapper.reflect.getter.InstantiatorGetter) GetterWithConverter(org.simpleflatmapper.reflect.getter.GetterWithConverter) Converter(org.simpleflatmapper.converter.Converter) GetterWithConverter(org.simpleflatmapper.reflect.getter.GetterWithConverter)

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