use of org.simpleflatmapper.map.property.DefaultValueProperty in project SimpleFlatMapper by arnaudroger.
the class CsvMapperFactoryDefaultValueTest method testDefaultValueJodaDateTime.
@Test
public void testDefaultValueJodaDateTime() throws IOException {
LocalDateTime localDateTime = LocalDateTime.now();
final Tuple2<String, LocalDateTime> value = CsvMapperFactory.newInstance().addColumnProperty("element1", new DefaultValueProperty<LocalDateTime>(localDateTime)).newMapper(new TypeReference<Tuple2<String, LocalDateTime>>() {
}).iterator(CsvParser.reader("element0\nv0")).next();
assertEquals("v0", value.getElement0());
assertEquals(localDateTime, value.getElement1());
}
use of org.simpleflatmapper.map.property.DefaultValueProperty in project SimpleFlatMapper by arnaudroger.
the class CellSetterFactory method getDelayedCellSetter.
public <T, P> DelayedCellSetterFactory<T, P> getDelayedCellSetter(PropertyMeta<T, P> prop, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder parsingContextFactoryBuilder) {
Class<? extends P> propertyClass = TypeHelper.toClass(prop.getPropertyType());
CellValueReader<? extends P> reader = getReader(prop, index, columnDefinition, parsingContextFactoryBuilder);
DelayedCellSetterFactory<T, P> factory;
final Setter<? super T, ? super P> setter = prop.isConstructorProperty() ? null : getSetter(prop);
if (propertyClass.isPrimitive()) {
factory = getPrimitiveDelayedCellSetter(propertyClass, reader, setter);
} else {
factory = new DelayedCellSetterFactoryImpl<T, P>(reader, setter);
}
final DefaultValueProperty defaultValueProperty = columnDefinition.lookFor(DefaultValueProperty.class);
if (defaultValueProperty != null) {
factory = new DefaultValueDelayedCallSetterFactory<T, P>(factory, defaultValueProperty, setter);
}
return factory;
}
use of org.simpleflatmapper.map.property.DefaultValueProperty in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceMapperBuilder method mapper.
public Mapper<S, T> mapper() {
// look for property with a default value property but no definition.
mapperConfig.columnDefinitions().forEach(DefaultValueProperty.class, new BiConsumer<Predicate<? super K>, DefaultValueProperty>() {
@Override
public void accept(Predicate<? super K> predicate, DefaultValueProperty columnProperty) {
if (propertyMappingsBuilder.hasKey(predicate)) {
return;
}
if (predicate instanceof Named) {
String name = ((Named) predicate).getName();
GetterProperty getterProperty = new GetterProperty(new ConstantGetter<S, Object>(columnProperty.getValue()), mapperSource.source(), columnProperty.getValue().getClass());
final FieldMapperColumnDefinition<K> columnDefinition = FieldMapperColumnDefinition.<K>identity().add(columnProperty, getterProperty);
propertyMappingsBuilder.addPropertyIfPresent(keyFactory.newKey(name, propertyMappingsBuilder.maxIndex() + 1), columnDefinition);
}
}
});
FieldMapper<S, T>[] fields = fields();
InstantiatorAndFieldMappers constructorFieldMappersAndInstantiator = getConstructorFieldMappersAndInstantiator();
Mapper<S, T> mapper;
if (isEligibleForAsmMapper()) {
try {
mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {
@Override
public MapperAsmFactory newInstance(AsmFactory asmFactory) {
return new MapperAsmFactory(asmFactory);
}
}).createMapper(getKeys(), fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator, mapperSource.source(), getTargetClass());
} catch (Throwable e) {
if (mapperConfig.failOnAsm()) {
return ErrorHelper.rethrow(e);
} else {
mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
}
}
} else {
mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
}
return mapper;
}
Aggregations