org.sfm.reflect.meta.AbstractIndexPropertyFinder 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.reflect.meta;
import org.sfm.tuples.Tuple2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class AbstractIndexPropertyFinder implements PropertyFinder {
protected final ClassMeta classMeta;
protected final List> elements;
private final Map speculativeIndexes = new HashMap();
public AbstractIndexPropertyFinder(ClassMeta classMeta) {
this.elements = new ArrayList>();
this.classMeta = classMeta;
}
@SuppressWarnings("unchecked")
public PropertyMeta findProperty(PropertyNameMatcher propertyNameMatcher) {
IndexedColumn indexedColumn = propertyNameMatcher.matchesIndex();
if (indexedColumn == null) {
indexedColumn = extrapolateIndex(propertyNameMatcher);
}
if (indexedColumn == null) {
indexedColumn = speculativeMatching(propertyNameMatcher);
}
if (indexedColumn == null || !isValidIndex(indexedColumn)) {
return null;
}
IndexedElement indexedElement = (IndexedElement) getIndexedElement(indexedColumn);
if (indexedElement.getElementClassMeta().isLeaf() || indexedColumn.getSubPropertyNameMatcher() == null) {
indexedElement.addProperty(".");
return indexedElement.getPropertyMeta();
}
PropertyFinder> propertyFinder = indexedElement.getPropertyFinder();
if (propertyFinder == null) {
return null;
}
PropertyMeta, ?> subProp = propertyFinder.findProperty(indexedColumn.getSubPropertyNameMatcher());
if (subProp == null) {
return null;
}
indexedElement.addProperty(subProp);
return new SubPropertyMeta(classMeta.getReflectionService(), indexedElement.getPropertyMeta(), subProp);
}
protected abstract boolean isValidIndex(IndexedColumn indexedColumn);
protected abstract IndexedElement getIndexedElement(IndexedColumn indexedColumn);
private IndexedColumn speculativeMatching(PropertyNameMatcher propertyNameMatcher) {
// try to match against prefix
Tuple2 speculativeMatch = propertyNameMatcher.speculativeMatch();
IndexedColumn indexedColumn = null;
if (speculativeMatch != null) {
Integer index = speculativeIndexes.get(speculativeMatch.first());
if (index == null) {
indexedColumn = extrapolateIndex(speculativeMatch.getElement1());
if (indexedColumn != null) {
speculativeIndexes.put(speculativeMatch.first(), indexedColumn.getIndexValue());
}
} else {
indexedColumn = new IndexedColumn(index, speculativeMatch.getElement1());
}
}
return indexedColumn;
}
protected abstract IndexedColumn extrapolateIndex(PropertyNameMatcher propertyNameMatcher);
}