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());
}
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());
}
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);
}
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));
}
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;
}
Aggregations