
org.sfm.reflect.meta.AbstractIndexPropertyFinder Maven / Gradle / Ivy
package org.sfm.reflect.meta;
import org.sfm.reflect.InstantiatorDefinition;
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);
@Override
public List getEligibleInstantiatorDefinitions() {
return classMeta.getInstantiatorDefinitions();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy