org.sfm.map.impl.AbstractColumnDefinitionProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.map.impl;
import org.sfm.map.ColumnDefinition;
import org.sfm.map.ColumnDefinitionProvider;
import org.sfm.map.FieldKey;
import org.sfm.tuples.Tuple2;
import org.sfm.utils.Predicate;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractColumnDefinitionProvider, K extends FieldKey> implements ColumnDefinitionProvider {
protected final List, C>> definitions;
public AbstractColumnDefinitionProvider() {
definitions = new ArrayList, C>>();
}
public AbstractColumnDefinitionProvider(List, C>> definitions) {
this.definitions = definitions;
}
public void addColumnDefinition(Predicate super K> predicate, C definition) {
definitions.add(new Tuple2, C>(predicate, definition));
}
@Override
public C getColumnDefinition(K key) {
C definition = identity();
for(Tuple2, C> def : definitions) {
if (def.first().test(key)) {
definition = compose(definition, def.second());
}
}
return definition;
}
protected abstract C compose(C definition, C second);
protected abstract C identity();
}