Search in sources :

Example 1 with DefaultValueProperty

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());
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) Tuple2(org.simpleflatmapper.tuple.Tuple2) Test(org.junit.Test)

Example 2 with DefaultValueProperty

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;
}
Also used : DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty)

Example 3 with DefaultValueProperty

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;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) Named(org.simpleflatmapper.util.Named) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Predicate(org.simpleflatmapper.util.Predicate) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Aggregations

DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)3 LocalDateTime (org.joda.time.LocalDateTime)1 Test (org.junit.Test)1 FieldMapper (org.simpleflatmapper.map.FieldMapper)1 MapperAsmFactory (org.simpleflatmapper.map.asm.MapperAsmFactory)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)1 GetterProperty (org.simpleflatmapper.map.property.GetterProperty)1 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)1 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)1 Tuple2 (org.simpleflatmapper.tuple.Tuple2)1 Named (org.simpleflatmapper.util.Named)1 Predicate (org.simpleflatmapper.util.Predicate)1