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

org.sfm.reflect.meta.ArrayPropertyFinder 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.TypeHelper;
import org.sfm.utils.BooleanSupplier;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

	private final ArrayClassMeta arrayClassMeta;

	private final List> elements = new ArrayList>();


    public ArrayPropertyFinder(ArrayClassMeta arrayClassMeta) {
        this.arrayClassMeta = arrayClassMeta;
    }

    @Override
	public PropertyMeta findProperty(PropertyNameMatcher propertyNameMatcher) {
		IndexedColumn indexedColumn;

       indexedColumn = propertyNameMatcher.matchesIndex();

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

		if (indexedColumn == null) {
			return null;
		}

		IndexedElement indexedElement = getIndexedElement(indexedColumn);

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

    private IndexedElement getIndexedElement(IndexedColumn indexedColumn) {
        while (elements.size() <= indexedColumn.getIndexValue()) {
            elements.add(new IndexedElement(newElementPropertyMeta(elements.size(), "element" + elements.size()), arrayClassMeta.getElementClassMeta()));
        }

        return elements.get(indexedColumn.getIndexValue());
	}

    private PropertyMeta newElementPropertyMeta(int index, String name) {
        if (arrayClassMeta.isArray()) {
            return new ArrayElementPropertyMeta(name, name,
                    arrayClassMeta.getReflectionService(), index, arrayClassMeta);
        } else {
            return new ListElementPropertyMeta(name, name,
                    arrayClassMeta.getReflectionService(), index, arrayClassMeta, new BooleanSupplier() {

                @Override
                public boolean getAsBoolean() {
                    return elements.size() == 1;
                }
            });
        }
    }

    private IndexedColumn extrapolateIndex(PropertyNameMatcher propertyNameMatcher) {
        final ClassMeta elementClassMeta = arrayClassMeta.getElementClassMeta();

        PropertyMeta property;

        if (elementClassMeta != null) {
            property = elementClassMeta.newPropertyFinder().findProperty(propertyNameMatcher);
            if (property != null) {

                for (int i = 0; i < elements.size(); i++) {
                    IndexedElement element = elements.get(i);
                    if (!element.hasProperty(property)) {
                        return new IndexedColumn(i, propertyNameMatcher);
                    }
                }

                return new IndexedColumn(elements.size(), propertyNameMatcher);
            }
        } else {
            return new IndexedColumn(elements.size(), null);
        }
		return null;
	}

	@Override
	public List> getEligibleConstructorDefinitions() {
		if (List.class.isAssignableFrom(TypeHelper.toClass(this.arrayClassMeta.getType()))) {
			try {
				return Arrays.asList(new ConstructorDefinition((Constructor) ArrayList.class.getConstructor()));
			} catch (NoSuchMethodException e) {
				throw new Error("Unexpected error " + e, e);
			}
		} else {
			return Collections.emptyList();
		}
	}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy