Search in sources :

Example 1 with ForEachCallBack

use of org.simpleflatmapper.util.ForEachCallBack in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilder method getSetters.

@SuppressWarnings({ "unchecked", "rawtypes" })
private CellSetter<T>[] getSetters(final ParsingContextFactoryBuilder parsingContextFactoryBuilder, final int delayedSetterEnd) {
    final Map<String, CsvMapperBuilder<?>> delegateMapperBuilders = new HashMap<String, CsvMapperBuilder<?>>();
    final Map<String, Integer> propertyToMapperIndex = new HashMap<String, Integer>();
    // calculate maxIndex
    int maxIndex = propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {

        int maxIndex = delayedSetterEnd;

        @Override
        public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
            if (propMapping != null) {
                maxIndex = Math.max(propMapping.getColumnKey().getIndex(), maxIndex);
                PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
                if (prop != null) {
                    CsvColumnKey key = propMapping.getColumnKey();
                    if (prop.isConstructorProperty()) {
                        throw new IllegalStateException("Unexpected ConstructorPropertyMeta at " + key.getIndex());
                    } else if (prop.isSubProperty()) {
                        final PropertyMeta<?, ?> propOwner = ((SubPropertyMeta) prop).getOwnerProperty();
                        CsvMapperBuilder<?> delegateMapperBuilder = delegateMapperBuilders.get(propOwner.getName());
                        if (delegateMapperBuilder == null) {
                            delegateMapperBuilder = new CsvMapperBuilder(propOwner.getPropertyType(), propOwner.getPropertyClassMeta(), minDelayedSetter, cellValueReaderFactory, mapperConfig);
                            delegateMapperBuilders.put(propOwner.getName(), delegateMapperBuilder);
                        }
                        Integer currentIndex = propertyToMapperIndex.get(propOwner.getName());
                        if (currentIndex == null || currentIndex < key.getIndex()) {
                            propertyToMapperIndex.put(propOwner.getName(), key.getIndex());
                        }
                        delegateMapperBuilder.addMapping(((SubPropertyMeta) prop).getSubProperty(), key, propMapping.getColumnDefinition());
                    }
                }
            }
        }
    }, delayedSetterEnd).maxIndex;
    // builder se setters
    final CellSetter<T>[] setters = new CellSetter[maxIndex + 1 - delayedSetterEnd];
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {

        final Map<String, CsvMapperImpl<?>> mappers = new HashMap<String, CsvMapperImpl<?>>();

        final CellSetterFactory cellSetterFactory = new CellSetterFactory(cellValueReaderFactory, mapperConfig.mapperBuilderErrorHandler());

        @Override
        public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
            if (propMapping == null) {
                return;
            }
            PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
            if (prop == null || prop instanceof SelfPropertyMeta) {
                return;
            }
            if (prop instanceof SubPropertyMeta) {
                DelegateMarkerSetter<T, ?> delegateMarkerSetter = getDelegateMarkerSetter((SubPropertyMeta) prop);
                setters[propMapping.getColumnKey().getIndex() - delayedSetterEnd] = delegateMarkerSetter;
            } else {
                setters[propMapping.getColumnKey().getIndex() - delayedSetterEnd] = cellSetterFactory.getCellSetter(prop, propMapping.getColumnKey().getIndex(), propMapping.getColumnDefinition(), parsingContextFactoryBuilder);
            }
        }

        private <I, P> DelegateMarkerSetter<T, I> getDelegateMarkerSetter(SubPropertyMeta<T, I, P> prop) {
            final String propName = prop.getOwnerProperty().getName();
            CsvMapperImpl<I> mapper = (CsvMapperImpl<I>) mappers.get(propName);
            if (mapper == null) {
                CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propName);
                mapper = (CsvMapperImpl<I>) delegateMapperBuilder.mapper();
                mappers.put(propName, mapper);
            }
            int indexOfMapper = propertyToMapperIndex.get(propName);
            return new DelegateMarkerSetter<T, I>(mapper, prop.getOwnerProperty().getSetter(), indexOfMapper);
        }
    }, delayedSetterEnd);
    return setters;
}
Also used : HashMap(java.util.HashMap) ForEachCallBack(org.simpleflatmapper.util.ForEachCallBack) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) CellSetter(org.simpleflatmapper.csv.mapper.CellSetter) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) DelayedCellSetterFactory(org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping)

Aggregations

HashMap (java.util.HashMap)1 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)1 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)1 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)1 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)1 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)1 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)1 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)1 ForEachCallBack (org.simpleflatmapper.util.ForEachCallBack)1