Search in sources :

Example 1 with IntGetter

use of org.simpleflatmapper.reflect.primitive.IntGetter in project SimpleFlatMapper by arnaudroger.

the class FieldMapperToAppendableFactory method newFieldMapper.

@Override
@SuppressWarnings("unchecked")
public <S, P> FieldMapper<S, Appendable> newFieldMapper(PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> pm, MappingContextFactoryBuilder builder, MapperBuilderErrorHandler mappingErrorHandler) {
    if (pm == null)
        throw new NullPointerException("pm is null");
    Getter<? super S, ? extends P> getter;
    Getter<?, ?> customGetter = pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType());
    if (customGetter != null) {
        getter = (Getter<S, P>) customGetter;
    } else {
        getter = pm.getPropertyMeta().getGetter();
    }
    ColumnDefinition<CsvColumnKey, ?> columnDefinition = pm.getColumnDefinition();
    Type type = pm.getPropertyMeta().getPropertyType();
    if (TypeHelper.isPrimitive(type) && !columnDefinition.has(FormatProperty.class)) {
        if (getter instanceof BooleanGetter) {
            return new BooleanFieldMapper<S, Appendable>((BooleanGetter) getter, new BooleanAppendableSetter(cellWriter));
        } else if (getter instanceof ByteGetter) {
            return new ByteFieldMapper<S, Appendable>((ByteGetter) getter, new ByteAppendableSetter(cellWriter));
        } else if (getter instanceof CharacterGetter) {
            return new CharacterFieldMapper<S, Appendable>((CharacterGetter) getter, new CharacterAppendableSetter(cellWriter));
        } else if (getter instanceof ShortGetter) {
            return new ShortFieldMapper<S, Appendable>((ShortGetter) getter, new ShortAppendableSetter(cellWriter));
        } else if (getter instanceof IntGetter) {
            return new IntFieldMapper<S, Appendable>((IntGetter) getter, new IntegerAppendableSetter(cellWriter));
        } else if (getter instanceof LongGetter) {
            return new LongFieldMapper<S, Appendable>((LongGetter) getter, new LongAppendableSetter(cellWriter));
        } else if (getter instanceof FloatGetter) {
            return new FloatFieldMapper<S, Appendable>((FloatGetter) getter, new FloatAppendableSetter(cellWriter));
        } else if (getter instanceof DoubleGetter) {
            return new DoubleFieldMapper<S, Appendable>((DoubleGetter) getter, new DoubleAppendableSetter(cellWriter));
        }
    }
    Setter<Appendable, ? super P> setter = null;
    if (TypeHelper.isEnum(type) && columnDefinition.has(EnumOrdinalFormatProperty.class)) {
        setter = (Setter) new EnumOrdinalAppendableSetter(cellWriter);
    }
    Format format = null;
    if (columnDefinition.has(FormatProperty.class)) {
        format = columnDefinition.lookFor(FormatProperty.class).format();
    } else if (TypeHelper.areEquals(type, Date.class)) {
        String df = DEFAULT_DATE_FORMAT;
        DateFormatProperty dfp = columnDefinition.lookFor(DateFormatProperty.class);
        if (dfp != null) {
            df = dfp.get();
        }
        format = new SimpleDateFormat(df);
    }
    if (format != null) {
        final Format f = format;
        builder.addSupplier(pm.getColumnKey().getIndex(), new CloneFormatSupplier(f));
        return new FormatingAppender<S>(getter, new MappingContextFormatGetter<S>(pm.getColumnKey().getIndex()), cellWriter);
    }
    if (setter == null) {
        setter = getSetter(pm, cellWriter);
    }
    if (setter == null) {
        Converter<? super P, ? extends CharSequence> converter = converterService.findConverter(pm.getPropertyMeta().getPropertyType(), CharSequence.class, columnDefinition != null ? columnDefinition.properties() : new Object[0]);
        if (converter != null) {
            return new ConvertingAppender<S, P>(getter, converter, cellWriter);
        }
    }
    return new FieldMapperImpl<S, Appendable, P>(getter, setter);
}
Also used : ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) FieldMapperImpl(org.simpleflatmapper.map.fieldmapper.FieldMapperImpl) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) IntFieldMapper(org.simpleflatmapper.map.fieldmapper.IntFieldMapper) CharacterFieldMapper(org.simpleflatmapper.map.fieldmapper.CharacterFieldMapper) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) BooleanFieldMapper(org.simpleflatmapper.map.fieldmapper.BooleanFieldMapper) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) EnumOrdinalFormatProperty(org.simpleflatmapper.map.property.EnumOrdinalFormatProperty) Date(java.util.Date) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Type(java.lang.reflect.Type) FloatFieldMapper(org.simpleflatmapper.map.fieldmapper.FloatFieldMapper) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) SimpleDateFormat(java.text.SimpleDateFormat) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter)

Example 2 with IntGetter

use of org.simpleflatmapper.reflect.primitive.IntGetter in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementMapperBuilder method buildIndexFieldMappers.

@SuppressWarnings("unchecked")
public MultiIndexFieldMapper<T>[] buildIndexFieldMappers() {
    final List<MultiIndexFieldMapper<T>> fields = new ArrayList<MultiIndexFieldMapper<T>>();
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>>() {

        @Override
        public void handle(PropertyMapping<T, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            if (isMultiIndex(pm.getPropertyMeta())) {
                fields.add(newCollectionFieldMapper(pm));
            } else {
                fields.add(newFieldMapper(pm));
            }
        }

        private <P, C> MultiIndexFieldMapper<T> newCollectionFieldMapper(PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            PropertyMeta<T, ?> propertyMeta = pm.getPropertyMeta();
            IndexedGetter<C, P> indexedGetter;
            IntGetter<? super C> sizeGetter;
            Getter<T, C> collectionGetter = (Getter<T, C>) propertyMeta.getGetter();
            if (TypeHelper.isAssignable(List.class, propertyMeta.getPropertyType())) {
                indexedGetter = (IndexedGetter<C, P>) new ListIndexedGetter<P>();
                sizeGetter = (IntGetter<C>) new ListSizeGetter();
            } else if (TypeHelper.isArray(propertyMeta.getPropertyType())) {
                indexedGetter = (IndexedGetter<C, P>) new ArrayIndexedGetter<P>();
                sizeGetter = new ArraySizeGetter();
            } else {
                throw new IllegalArgumentException("Unexpected elementMeta" + propertyMeta);
            }
            PropertyMeta<C, P> childProperty = (PropertyMeta<C, P>) pm.getPropertyMeta().getPropertyClassMeta().newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).findProperty(DefaultPropertyNameMatcher.of("0"), pm.getColumnDefinition().properties());
            final PropertyMapping<C, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pmchildProperttMeta = pm.propertyMeta(childProperty);
            IndexedSetter<PreparedStatement, P> setter = getSetter(pmchildProperttMeta);
            return new CollectionIndexFieldMapper<T, C, P>(setter, collectionGetter, sizeGetter, indexedGetter);
        }

        private <P, C> IndexedSetter<PreparedStatement, P> getSetter(PropertyMapping<C, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            IndexedSetter<PreparedStatement, P> setter = null;
            IndexedSetterProperty indexedSetterProperty = pm.getColumnDefinition().lookFor(IndexedSetterProperty.class);
            if (indexedSetterProperty != null) {
                setter = (IndexedSetter<PreparedStatement, P>) indexedSetterProperty.getIndexedSetter();
            }
            if (setter == null) {
                setter = indexedSetterFactory(pm);
            }
            if (setter == null) {
                mapperConfig.mapperBuilderErrorHandler().accessorNotFound("Could not find setter for " + pm.getColumnKey() + " type " + pm.getPropertyMeta().getPropertyType() + " path " + pm.getPropertyMeta().getPath() + " See " + ErrorDoc.toUrl("PS_SETTER_NOT_FOUND"));
            }
            return setter;
        }

        private <P, C> IndexedSetter<PreparedStatement, P> indexedSetterFactory(PropertyMapping<C, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            IndexedSetter<PreparedStatement, P> setter = null;
            final IndexedSetterFactoryProperty indexedSetterPropertyFactory = pm.getColumnDefinition().lookFor(IndexedSetterFactoryProperty.class);
            if (indexedSetterPropertyFactory != null) {
                IndexedSetterFactory<PreparedStatement, PropertyMapping<?, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>> setterFactory = (IndexedSetterFactory<PreparedStatement, PropertyMapping<?, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>>) indexedSetterPropertyFactory.getIndexedSetterFactory();
                setter = setterFactory.getIndexedSetter(pm);
            }
            if (setter == null) {
                setter = indexedSetterFactory.getIndexedSetter(pm);
            }
            if (setter == null) {
                final ClassMeta<P> classMeta = pm.getPropertyMeta().getPropertyClassMeta();
                if (classMeta instanceof ObjectClassMeta) {
                    ObjectClassMeta<P> ocm = (ObjectClassMeta<P>) classMeta;
                    if (ocm.getNumberOfProperties() == 1) {
                        PropertyMeta<P, ?> subProp = ocm.getFirstProperty();
                        final PropertyMapping<?, ?, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> subPropertyMapping = pm.propertyMeta(subProp);
                        IndexedSetter<PreparedStatement, ?> subSetter = indexedSetterFactory(subPropertyMapping);
                        if (subSetter != null) {
                            setter = new PreparedStatementIndexSetterOnGetter<Object, P>((PreparedStatementIndexSetter<Object>) subSetter, (Getter<P, Object>) subProp.getGetter());
                        }
                    }
                }
            }
            return setter;
        }

        private <P> MultiIndexFieldMapper<T> newFieldMapper(PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> pm) {
            return new SingleIndexFieldMapper<T, P>(getSetter(pm), pm.getPropertyMeta().getGetter());
        }
    });
    return fields.toArray(new MultiIndexFieldMapper[0]);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ListIndexedGetter(org.simpleflatmapper.reflect.getter.ListIndexedGetter) Getter(org.simpleflatmapper.reflect.Getter) ArraySizeGetter(org.simpleflatmapper.reflect.getter.ArraySizeGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) ArrayIndexedGetter(org.simpleflatmapper.reflect.getter.ArrayIndexedGetter) IndexedGetter(org.simpleflatmapper.reflect.IndexedGetter) PreparedStatementIndexSetterOnGetter(org.simpleflatmapper.jdbc.impl.setter.PreparedStatementIndexSetterOnGetter) ListSizeGetter(org.simpleflatmapper.reflect.getter.ListSizeGetter) ArrayIndexedGetter(org.simpleflatmapper.reflect.getter.ArrayIndexedGetter) ArrayList(java.util.ArrayList) IndexedSetterFactoryProperty(org.simpleflatmapper.jdbc.property.IndexedSetterFactoryProperty) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) ArrayList(java.util.ArrayList) List(java.util.List) IndexedSetterProperty(org.simpleflatmapper.jdbc.property.IndexedSetterProperty) PreparedStatementIndexSetterOnGetter(org.simpleflatmapper.jdbc.impl.setter.PreparedStatementIndexSetterOnGetter) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta) ClassMeta(org.simpleflatmapper.reflect.meta.ClassMeta) IndexedSetterFactory(org.simpleflatmapper.reflect.IndexedSetterFactory) PreparedStatementIndexedSetterFactory(org.simpleflatmapper.jdbc.impl.PreparedStatementIndexedSetterFactory) PreparedStatement(java.sql.PreparedStatement) ArraySizeGetter(org.simpleflatmapper.reflect.getter.ArraySizeGetter) IndexedSetter(org.simpleflatmapper.reflect.IndexedSetter) ListIndexedGetter(org.simpleflatmapper.reflect.getter.ListIndexedGetter) ArrayIndexedGetter(org.simpleflatmapper.reflect.getter.ArrayIndexedGetter) IndexedGetter(org.simpleflatmapper.reflect.IndexedGetter) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta) ListSizeGetter(org.simpleflatmapper.reflect.getter.ListSizeGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter)

Aggregations

IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 Type (java.lang.reflect.Type)1 PreparedStatement (java.sql.PreparedStatement)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)1 PreparedStatementIndexedSetterFactory (org.simpleflatmapper.jdbc.impl.PreparedStatementIndexedSetterFactory)1 PreparedStatementIndexSetterOnGetter (org.simpleflatmapper.jdbc.impl.setter.PreparedStatementIndexSetterOnGetter)1 IndexedSetterFactoryProperty (org.simpleflatmapper.jdbc.property.IndexedSetterFactoryProperty)1 IndexedSetterProperty (org.simpleflatmapper.jdbc.property.IndexedSetterProperty)1 BooleanFieldMapper (org.simpleflatmapper.map.fieldmapper.BooleanFieldMapper)1 CharacterFieldMapper (org.simpleflatmapper.map.fieldmapper.CharacterFieldMapper)1 FieldMapperImpl (org.simpleflatmapper.map.fieldmapper.FieldMapperImpl)1 FloatFieldMapper (org.simpleflatmapper.map.fieldmapper.FloatFieldMapper)1 IntFieldMapper (org.simpleflatmapper.map.fieldmapper.IntFieldMapper)1 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)1 DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)1