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.InstantiatorDefinition;
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 extends AbstractIndexPropertyFinder {

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


    public ArrayPropertyFinder(ArrayClassMeta arrayClassMeta) {
        super(arrayClassMeta);
    }

    @Override
    protected IndexedElement getIndexedElement(IndexedColumn indexedColumn) {
        while (elements.size() <= indexedColumn.getIndexValue()) {
            elements.add(new IndexedElement(newElementPropertyMeta(elements.size(), "element" + elements.size()), ((ArrayClassMeta)classMeta).getElementClassMeta()));
        }

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

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

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

    @Override
    protected IndexedColumn extrapolateIndex(PropertyNameMatcher propertyNameMatcher) {
        final ClassMeta elementClassMeta = ((ArrayClassMeta)classMeta).getElementClassMeta();

        PropertyMeta property;

        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);
        }
		return null;
	}

    @Override
    protected boolean isValidIndex(IndexedColumn indexedColumn) {
        return indexedColumn.getIndexValue() >= 0;
    }

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

    @Override
    public  ConstructorPropertyMeta findConstructor(InstantiatorDefinition instantiatorDefinition) {
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy