Search in sources :

Example 1 with Predicate

use of org.simpleflatmapper.util.Predicate 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 Predicate

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

the class ConstantSourceMapperBuilderTest method testIssue495Simple.

@Test
public void testIssue495Simple() throws Exception {
    TypeReference<List<Tuple3<Foo, Foo, Foo>>> typeReference = new TypeReference<List<Tuple3<Foo, Foo, Foo>>>() {
    };
    ClassMeta<List<Tuple3<Foo, Foo, Foo>>> classMeta = this.classMeta.getReflectionService().getClassMeta(typeReference.getType());
    Predicate<PropertyMeta<?, ?>> propertyFilter = new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return true;
        }
    };
    PropertyFinder<?> propertyFinder = classMeta.newPropertyFinder(propertyFilter);
    PropertyMeta<?, ?> p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("bar", 0, false, false), new Object[0]);
    PropertyMeta<?, ?> p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("bar", 0, false, false), new Object[0]);
    assertEquals("[0].element0.bar", p1.getPath());
    assertEquals("[0].element1.bar", p2.getPath());
    propertyFinder = classMeta.newPropertyFinder(propertyFilter);
    p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("elt0_elt0_bar", 0, false, false), new Object[0]);
    p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("elt1_elt0_bar", 0, false, false), new Object[0]);
    assertEquals("[0].element0.bar", p1.getPath());
    assertEquals("[1].element0.bar", p2.getPath());
}
Also used : DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) Predicate(org.simpleflatmapper.util.Predicate) Tuple3(org.simpleflatmapper.tuple.Tuple3) List(java.util.List) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Example 3 with Predicate

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

the class DiscriminatorMapperTest method setUp.

@Before
public void setUp() {
    Mapper<Object[], Person> studentMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            StudentGS studentGS = new StudentGS();
            try {
                mapTo(source, studentGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return studentGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            StudentGS studentGS = (StudentGS) target;
            studentGS.setName((String) source[1]);
            studentGS.setId((Integer) source[2]);
        }
    };
    Mapper<Object[], Person> professorMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            ProfessorGS professorGS = new ProfessorGS();
            try {
                mapTo(source, professorGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return professorGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            ProfessorGS professorGS = (ProfessorGS) target;
            professorGS.setName((String) source[1]);
            professorGS.setId((Integer) source[2]);
        }
    };
    List<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>>();
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "student".equals(objects[0]);
        }
    }, studentMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "professor".equals(objects[0]);
        }
    }, professorMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mapper = new DiscriminatorMapper<Object[], Object[][], Person, RuntimeException>(mappers, new UnaryFactory<Object[][], Enumarable<Object[]>>() {

        @Override
        public Enumarable<Object[]> newInstance(Object[][] objects) {
            return new ArrayEnumarable<Object[]>(objects);
        }
    }, new UncheckedConverter<Object[], String>() {

        @Override
        public String convert(Object[] in) {
            return Arrays.toString(in);
        }
    }, RethrowConsumerErrorHandler.INSTANCE);
}
Also used : ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) MappingContextFactoryFromRows(org.simpleflatmapper.map.context.MappingContextFactoryFromRows) MappingException(org.simpleflatmapper.map.MappingException) Predicate(org.simpleflatmapper.util.Predicate) MappingContext(org.simpleflatmapper.map.MappingContext) ProfessorGS(org.simpleflatmapper.test.beans.ProfessorGS) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) Mapper(org.simpleflatmapper.map.Mapper) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Person(org.simpleflatmapper.test.beans.Person) ArrayEnumarable(org.simpleflatmapper.util.ArrayEnumarable) StudentGS(org.simpleflatmapper.test.beans.StudentGS) Before(org.junit.Before)

Example 4 with Predicate

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

the class FieldMapperColumnDefinitionProviderImplTest method testProvides.

@Test
public void testProvides() {
    final Object prop1 = "prop1";
    final Object prop2 = "prop2";
    final ConstantUnaryFactory<SampleFieldKey, Object> prop1Fact = ConstantUnaryFactory.<SampleFieldKey, Object>of(prop1);
    final ConstantUnaryFactory<SampleFieldKey, Object> prop2Fact = ConstantUnaryFactory.<SampleFieldKey, Object>of(prop2);
    FieldMapperColumnDefinitionProviderImpl<SampleFieldKey> provider = new FieldMapperColumnDefinitionProviderImpl<SampleFieldKey>();
    final Predicate<SampleFieldKey> prop1Predicate = new Predicate<SampleFieldKey>() {

        @Override
        public boolean test(SampleFieldKey sampleFieldKey) {
            return sampleFieldKey.getIndex() == 0;
        }
    };
    final Predicate<SampleFieldKey> prop2Predicate = new Predicate<SampleFieldKey>() {

        @Override
        public boolean test(SampleFieldKey sampleFieldKey) {
            return sampleFieldKey.getIndex() == 1;
        }
    };
    provider.addColumnProperty(prop1Predicate, prop1Fact);
    provider.addColumnProperty(prop2Predicate, prop2Fact);
    List<AbstractColumnDefinitionProvider.PredicatedColunnPropertyFactory<FieldMapperColumnDefinition<SampleFieldKey>, SampleFieldKey>> properties = provider.getProperties();
    assertEquals(2, properties.size());
    assertEquals(prop1Predicate, properties.get(0).getPredicate());
    assertEquals(prop2Predicate, properties.get(1).getPredicate());
    assertEquals(prop1Fact, properties.get(0).getColumnPropertyFactory());
    assertEquals(prop2Fact, properties.get(1).getColumnPropertyFactory());
    final ArrayList<String> props = new ArrayList<String>();
    provider.addColumnProperty(prop1Predicate, new Object());
    provider.forEach(String.class, new BiConsumer<Predicate<? super SampleFieldKey>, String>() {

        @Override
        public void accept(Predicate<? super SampleFieldKey> predicate, String s) {
            if (s == prop1) {
                assertEquals(predicate, prop1Predicate);
            }
            if (s == prop2) {
                assertEquals(predicate, prop2Predicate);
            }
            props.add(s);
        }
    });
    assertArrayEquals(new String[] { "prop1", "prop2" }, props.toArray(new String[0]));
    assertArrayEquals(new String[] { "prop1" }, provider.getColumnDefinition(new SampleFieldKey("", 0)).lookForAll(String.class));
}
Also used : ArrayList(java.util.ArrayList) Predicate(org.simpleflatmapper.util.Predicate) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperColumnDefinitionProviderImpl(org.simpleflatmapper.map.mapper.FieldMapperColumnDefinitionProviderImpl) Test(org.junit.Test)

Example 5 with Predicate

use of org.simpleflatmapper.util.Predicate 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)

Aggregations

Predicate (org.simpleflatmapper.util.Predicate)13 Test (org.junit.Test)9 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)7 ArrayList (java.util.ArrayList)3 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)3 Tuple2 (org.simpleflatmapper.tuple.Tuple2)3 StringReader (java.io.StringReader)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)2 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 ArrayClassMeta (org.simpleflatmapper.reflect.meta.ArrayClassMeta)2 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 Tuple3 (org.simpleflatmapper.tuple.Tuple3)2 TypeReference (org.simpleflatmapper.util.TypeReference)2 ResultSet (java.sql.ResultSet)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1