Search in sources :

Example 1 with CellValueReaderFactory

use of org.simpleflatmapper.csv.CellValueReaderFactory in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCustomReaderTest method testCustomCsvReaderValueFactory.

@Test
public void testCustomCsvReaderValueFactory() throws IOException {
    CsvMapper<Tuple2<String, String>> csvMapper = CsvMapperFactory.newInstance().addColumnDefinition(new Predicate<CsvColumnKey>() {

        @Override
        public boolean test(CsvColumnKey csvColumnKey) {
            return true;
        }
    }, CsvColumnDefinition.customCellValueReaderFactoryDefinition(new CellValueReaderFactory() {

        @SuppressWarnings("unchecked")
        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, final int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder builder) {
            return (CellValueReader<P>) new CellValueReader<String>() {

                @Override
                public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
                    return "g" + index;
                }
            };
        }
    })).newMapper(Tuples.typeDef(String.class, String.class));
    Tuple2<String, String> value = csvMapper.iterator(new StringReader("b0,b1\nc0,c1")).next();
    assertEquals("g0", value.first());
    assertEquals("g1", value.second());
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) CellValueReader(org.simpleflatmapper.csv.CellValueReader) Predicate(org.simpleflatmapper.util.Predicate) Type(java.lang.reflect.Type) Tuple2(org.simpleflatmapper.tuple.Tuple2) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) StringReader(java.io.StringReader) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 2 with CellValueReaderFactory

use of org.simpleflatmapper.csv.CellValueReaderFactory in project SimpleFlatMapper by arnaudroger.

the class CellSetterFactory method cellValueReaderFromFactory.

private <P> CellValueReader<P> cellValueReaderFromFactory(PropertyMeta<?, ?> pm, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder parsingContextFactoryBuilder) {
    Type propertyType = pm.getPropertyType();
    CellValueReader<P> reader = null;
    if (columnDefinition.hasCustomReaderFactory()) {
        CellValueReaderFactory factory = columnDefinition.getCustomCellValueReaderFactory();
        reader = factory.getReader(propertyType, index, columnDefinition, parsingContextFactoryBuilder);
    }
    if (reader == null) {
        reader = cellValueReaderFactory.getReader(propertyType, index, columnDefinition, parsingContextFactoryBuilder);
    }
    if (reader == null) {
        if (!pm.isSelf()) {
            final ClassMeta<?> classMeta = pm.getPropertyClassMeta();
            InstantiatorDefinition id = InstantiatorDefinitions.lookForCompatibleOneArgument(classMeta.getInstantiatorDefinitions(), COMPATIBILITY_SCORER);
            if (id != null) {
                final Parameter parameter = id.getParameters()[0];
                // look for constructor property matching name
                final PropertyMeta<?, Object> property = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {

                    @Override
                    public boolean test(PropertyMeta<?, ?> propertyMeta) {
                        return propertyMeta.isConstructorProperty() || propertyMeta.isSubProperty() && ((SubPropertyMeta) propertyMeta).getOwnerProperty().isConstructorProperty();
                    }
                }).findProperty(DefaultPropertyNameMatcher.exact(parameter.getName()), columnDefinition.properties());
                reader = cellValueReaderFromFactory(property, index, columnDefinition, parsingContextFactoryBuilder);
                if (reader != null) {
                    Instantiator<P, P> instantiator = classMeta.getReflectionService().getInstantiatorFactory().getOneArgIdentityInstantiator(id, classMeta.getReflectionService().builderIgnoresNullValues());
                    return new InstantiatorOnReader<P, P>(instantiator, reader);
                }
            }
        }
    }
    return reader;
}
Also used : Predicate(org.simpleflatmapper.util.Predicate) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) Type(java.lang.reflect.Type) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) Parameter(org.simpleflatmapper.reflect.Parameter) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta)

Example 3 with CellValueReaderFactory

use of org.simpleflatmapper.csv.CellValueReaderFactory in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCustomReaderFactoryTest method testCustomReaderFactory.

@Test
public void testCustomReaderFactory() throws IOException {
    CsvMapper<DbObject> mapper = CsvMapperFactory.newInstance().failOnAsm(true).cellValueReaderFactory(new CellValueReaderFactory() {

        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder builder) {
            return new CellValueReader<P>() {

                @SuppressWarnings("unchecked")
                @Override
                public P read(char[] chars, int offset, int length, ParsingContext parsingContext) {
                    return (P) "Hello!";
                }
            };
        }
    }).newBuilder(DbObject.class).addMapping("name").mapper();
    DbObject bop = mapper.iterator(new StringReader("bop")).next();
    assertEquals("Hello!", bop.getName());
}
Also used : Type(java.lang.reflect.Type) ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) DbObject(org.simpleflatmapper.test.beans.DbObject) CellValueReader(org.simpleflatmapper.csv.CellValueReader) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) StringReader(java.io.StringReader) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 4 with CellValueReaderFactory

use of org.simpleflatmapper.csv.CellValueReaderFactory in project SimpleFlatMapper by arnaudroger.

the class CsvColumnDefinitionTest method testComposition.

@Test
public void testComposition() {
    TimeZone tz = TimeZone.getTimeZone("Europe/Brussels");
    CellValueReaderFactory cellValueReaderFactory = new CellValueReaderFactory() {

        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder contextFactoryBuilder) {
            return null;
        }

        @Override
        public String toString() {
            return "CellValueReaderFactory";
        }
    };
    final Predicate<PropertyMeta<?, ?>> appliesTo = ConstantPredicate.falsePredicate();
    CsvColumnDefinition compose = CsvColumnDefinition.identity().addDateFormat("yyyyMM").addRename("blop").addCustomReader(new CellValueReader<Integer>() {

        @Override
        public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3;
        }

        @Override
        public String toString() {
            return "CellValueReader";
        }
    }).addCustomCellValueReaderFactory(cellValueReaderFactory).addTimeZone(tz).addKey(appliesTo);
    assertEquals("blop", compose.rename(new CsvColumnKey("bar", -1)).getName());
    assertEquals("blop", CsvColumnDefinition.renameDefinition("blop").rename(new CsvColumnKey("bar", -1)).getName());
    assertArrayEquals(new String[] { "yyyyMM" }, compose.dateFormats());
    assertEquals(new Integer(3), compose.getCustomReader().read(null, 0, 0, null));
    assertEquals(cellValueReaderFactory, compose.getCustomCellValueReaderFactory());
    assertEquals(tz, compose.getTimeZone());
    assertEquals(tz, CsvColumnDefinition.timeZoneDefinition(tz).getTimeZone());
    assertTrue(compose.hasCustomSourceFrom(Object.class));
    assertFalse(compose.ignore());
    assertEquals(Integer.class, compose.getCustomSourceReturnTypeFrom(Object.class));
    assertTrue(CsvColumnDefinition.identity().addIgnore().ignore());
    assertTrue(compose.isKey());
    assertTrue(CsvColumnDefinition.key(appliesTo).isKey());
    assertSame(appliesTo, compose.keyAppliesTo());
    final String toString = compose.addIgnore().toString();
    System.out.println("toString = " + toString);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) CellValueReader(org.simpleflatmapper.csv.CellValueReader) TimeZone(java.util.TimeZone) Type(java.lang.reflect.Type) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Aggregations

Type (java.lang.reflect.Type)4 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)4 Test (org.junit.Test)3 CellValueReader (org.simpleflatmapper.csv.CellValueReader)3 CsvColumnDefinition (org.simpleflatmapper.csv.CsvColumnDefinition)3 ParsingContext (org.simpleflatmapper.csv.ParsingContext)3 ParsingContextFactoryBuilder (org.simpleflatmapper.csv.ParsingContextFactoryBuilder)3 StringReader (java.io.StringReader)2 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)2 Predicate (org.simpleflatmapper.util.Predicate)2 TimeZone (java.util.TimeZone)1 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)1 Parameter (org.simpleflatmapper.reflect.Parameter)1 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)1 DbObject (org.simpleflatmapper.test.beans.DbObject)1 Tuple2 (org.simpleflatmapper.tuple.Tuple2)1