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.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 extends T>) 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;
}
}