All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sfm.reflect.meta.AbstractIndexPropertyFinder Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
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);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy