org.sfm.reflect.meta.ArrayPropertyFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
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 extends T>) 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;
}
}