org.simpleflatmapper.reflect.meta.OptionalPropertyFinder Maven / Gradle / Ivy
Show all versions of sfm-reflect Show documentation
package org.simpleflatmapper.reflect.meta;
import org.simpleflatmapper.reflect.InstantiatorDefinition;
import org.simpleflatmapper.util.Predicate;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Optional;
public class OptionalPropertyFinder extends PropertyFinder> {
private final OptionalClassMeta optionalClassMeta;
private final PropertyFinder propertyFinder;
private final ClassMeta innerMeta;
public OptionalPropertyFinder(OptionalClassMeta optionalClassMeta, Predicate> propertyFilter, boolean selfScoreFullName) {
super(propertyFilter, selfScoreFullName);
this.optionalClassMeta = optionalClassMeta;
innerMeta = optionalClassMeta.getInnerMeta();
this.propertyFinder = innerMeta != null ? innerMeta.newPropertyFinder(propertyFilter) : null;
}
@SuppressWarnings("unchecked")
@Override
public void lookForProperties(
PropertyNameMatcher propertyNameMatcher,
Object[] properties, FoundProperty matchingProperties,
PropertyMatchingScore score, boolean allowSelfReference, PropertyFinderTransformer propertyFinderTransformer){
propertyFinderTransformer.apply(propertyFinder).lookForProperties(propertyNameMatcher, properties, new FoundProperty() {
@Override
public > void found(P propertyMeta, Runnable selectionCallback, PropertyMatchingScore score) {
matchingProperties.found(getSubPropertyMeta(propertyMeta), selectionCallback, score);
}
}, score, allowSelfReference, propertyFinderTransformer);
}
@SuppressWarnings("unchecked")
private PropertyMeta, E> getSubPropertyMeta(PropertyMeta property) {
return new SubPropertyMeta, I, E>(
optionalClassMeta.getReflectionService(),
(PropertyMeta, I>) optionalClassMeta.getProperty(),
(PropertyMeta)property);
}
@Override
public List getEligibleInstantiatorDefinitions() {
return optionalClassMeta.getInstantiatorDefinitions();
}
@Override
public PropertyFinder> getSubPropertyFinder(PropertyMeta, ?> owner) {
if (owner.equals(optionalClassMeta.getProperty())) {
return propertyFinder;
}
throw new IllegalArgumentException("Unexpected owner " + owner);
}
@Override
public PropertyFinder> getOrCreateSubPropertyFinder(SubPropertyMeta, ?, ?> subPropertyMeta) {
return getSubPropertyFinder(subPropertyMeta.getOwnerProperty());
}
@Override
public Type getOwnerType() {
return optionalClassMeta.getType();
}
}