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

org.sfm.reflect.meta.TuplePropertyFinder 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.reflect.ConstructorDefinition;
import org.sfm.reflect.ConstructorParameter;
import org.sfm.reflect.TypeHelper;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class TuplePropertyFinder implements PropertyFinder {

	private final TupleClassMeta tupleClassMeta;

	private final List> elements;

    private final List> constructorDefinitions;


	public TuplePropertyFinder(TupleClassMeta tupleClassMeta) {
		this.tupleClassMeta = tupleClassMeta;
		this.constructorDefinitions = tupleClassMeta.getConstructorDefinitions();

		this.elements = new ArrayList>();

		for(int i = 0; i < tupleClassMeta.getTupleSize(); i++) {
			elements.add(newIndexedElement(tupleClassMeta, i));
		}
	}

	private  IndexedElement newIndexedElement(TupleClassMeta tupleClassMeta, int i) {
		Type resolvedType = ((ParameterizedType) tupleClassMeta.getType()).getActualTypeArguments()[i];
		ConstructorPropertyMeta prop =
                newConstructorPropertyMeta(tupleClassMeta, i, resolvedType);
		ClassMeta classMeta = tupleClassMeta.getReflectionService().getClassMeta(resolvedType, false);
		return new IndexedElement(prop, classMeta);
	}

    private  ConstructorPropertyMeta newConstructorPropertyMeta(TupleClassMeta tupleClassMeta, int i, Type resolvedType) {
        Class tClass = TypeHelper.toClass(tupleClassMeta.getType());
        return new ConstructorPropertyMeta("element" + (i),
            "element" + (i), 	tupleClassMeta.getReflectionService(),
            new ConstructorParameter("element" + (i), Object.class, resolvedType), tClass);
    }

    @Override
	public  PropertyMeta findProperty(PropertyNameMatcher propertyNameMatcher) {

		IndexedColumn indexedColumn = propertyNameMatcher.matchesIndex();

		if (indexedColumn == null) {
			indexedColumn = extrapolateIndex(propertyNameMatcher);
		}

		if (indexedColumn == null || calculateTupleIndex(indexedColumn) >= elements.size()) {
			return null;
		}

		IndexedElement indexedElement = elements.get(calculateTupleIndex(indexedColumn));

		if (!indexedColumn.hasSubProperty()) {
			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(tupleClassMeta.getReflectionService(), indexedElement.getPropertyMeta(), subProp);
	}

	private int calculateTupleIndex(IndexedColumn indexedColumn) {
		return indexedColumn.getIndexValue();
	}


	private IndexedColumn extrapolateIndex(PropertyNameMatcher propertyNameMatcher) {
		for(int i = 0; i < elements.size(); i++) {
			IndexedElement element = elements.get(i);

			if (element.getElementClassMeta() != null) {
				PropertyFinder pf = element.getPropertyFinder();
				PropertyMeta property = pf.findProperty(propertyNameMatcher);
				if (property != null) {
					if (!element.hasProperty(property)) {
						return new IndexedColumn(i , propertyNameMatcher);
					}
				}

			}
		}
		return null;
	}

	@Override
	public List> getEligibleConstructorDefinitions() {
		return constructorDefinitions;
	}

    @Override
    public  ConstructorPropertyMeta findConstructor(ConstructorDefinition constructorDefinition) {
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy