use of org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory in project SimpleFlatMapper by arnaudroger.
the class CsvMapperBuilder method buildDelayedSetters.
@SuppressWarnings({ "unchecked" })
private DelayedCellSetterFactory<T, ?>[] buildDelayedSetters(final ParsingContextFactoryBuilder parsingContextFactoryBuilder, int delayedSetterEnd, boolean hasKeys) {
final Map<String, CsvMapperBuilder<?>> delegateMapperBuilders = new HashMap<String, CsvMapperBuilder<?>>();
final Map<String, Integer> propertyToMapperIndex = new HashMap<String, Integer>();
final DelayedCellSetterFactory<T, ?>[] delayedSetters = new DelayedCellSetterFactory[delayedSetterEnd];
final int newMinDelayedSetter = minDelayedSetter != 0 ? minDelayedSetter : hasKeys ? delayedSetterEnd : 0;
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {
final CellSetterFactory cellSetterFactory = new CellSetterFactory(cellValueReaderFactory, mapperConfig.mapperBuilderErrorHandler());
@Override
public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
if (propMapping != null) {
PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
CsvColumnKey key = propMapping.getColumnKey();
if (prop != null) {
if (prop.isSubProperty()) {
addSubProperty(delegateMapperBuilders, prop, key, propMapping.getColumnDefinition());
} else {
delayedSetters[propMapping.getColumnKey().getIndex()] = cellSetterFactory.getDelayedCellSetter(prop, key.getIndex(), propMapping.getColumnDefinition(), parsingContextFactoryBuilder);
}
}
}
}
private <I, P> void addSubProperty(Map<String, CsvMapperBuilder<?>> delegateMapperBuilders, PropertyMeta<T, P> prop, CsvColumnKey key, CsvColumnDefinition columnDefinition) {
SubPropertyMeta<T, I, P> subPropertyMeta = (SubPropertyMeta<T, I, P>) prop;
final PropertyMeta<T, I> propOwner = subPropertyMeta.getOwnerProperty();
CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propOwner.getName());
if (delegateMapperBuilder == null) {
delegateMapperBuilder = new CsvMapperBuilder<I>(propOwner.getPropertyType(), propOwner.getPropertyClassMeta(), newMinDelayedSetter, 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.getSubProperty(), key, columnDefinition);
}
}, 0, delayedSetterEnd);
final Map<String, CsvMapper<?>> mappers = new HashMap<String, CsvMapper<?>>();
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {
@Override
public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
if (propMapping == null)
return;
PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
if (prop.isSubProperty()) {
addSubPropertyDelayedSetter(delegateMapperBuilders, delayedSetters, propMapping.getColumnKey().getIndex(), prop);
}
}
private <I, P> void addSubPropertyDelayedSetter(Map<String, CsvMapperBuilder<?>> delegateMapperBuilders, DelayedCellSetterFactory<T, ?>[] delayedSetters, int setterIndex, PropertyMeta<T, P> prop) {
PropertyMeta<T, I> subProp = ((SubPropertyMeta<T, I, P>) prop).getOwnerProperty();
final String propName = subProp.getName();
CsvMapper<I> mapper = (CsvMapper<I>) mappers.get(propName);
if (mapper == null) {
CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propName);
mapper = delegateMapperBuilder.mapper();
mappers.put(propName, mapper);
}
int indexOfMapper = propertyToMapperIndex.get(propName);
Setter<? super T, ? super I> setter = null;
if (!subProp.isConstructorProperty()) {
setter = subProp.getSetter();
}
delayedSetters[setterIndex] = new DelegateMarkerDelayedCellSetterFactory<T, I>(mapper, setter, setterIndex, indexOfMapper);
}
}, 0, delayedSetterEnd);
return delayedSetters;
}
Aggregations