com.github.leeonky.jfactory.SubObjectExpression Maven / Gradle / Ivy
package com.github.leeonky.jfactory;
import com.github.leeonky.util.BeanClass;
import com.github.leeonky.util.Property;
import java.util.Optional;
class SubObjectExpression extends Expression
{
private final KeyValueCollection properties;
private final TraitsSpec traitsSpec;
private final ObjectFactory objectFactory;
public SubObjectExpression(KeyValueCollection properties, TraitsSpec traitsSpec, Property
property, ObjectFactory> objectFactory) {
super(property);
this.properties = properties;
this.traitsSpec = traitsSpec;
this.objectFactory = objectFactory;
}
@Override
protected boolean isPropertyMatch(Object propertyValue) {
return properties.matcher(property.getReaderType(), objectFactory).matches(propertyValue);
}
@Override
public Producer> buildProducer(JFactory jFactory, Producer
parent) {
Builder> builder = toBuilder(jFactory, property.getWriterType());
return query(jFactory).>map(object -> new BuilderValueProducer<>(property.getWriterType(), builder))
.orElseGet(builder::createProducer);
}
private Optional> query(JFactory jFactory) {
if (intently)
return Optional.empty();
return toBuilder(jFactory, property.getReaderType()).queryAll().stream().findFirst();
}
private Builder> toBuilder(JFactory jFactory, BeanClass> propertyType) {
return properties.apply(traitsSpec.toBuilder(jFactory, propertyType));
}
@Override
public Expression mergeTo(Expression
newExpression) {
return newExpression.mergeFrom(this);
}
@Override
protected Expression
mergeFrom(SubObjectExpression
origin) {
properties.insertAll(origin.properties);
traitsSpec.merge(origin.traitsSpec, property.toString());
setIntently(intently || origin.intently);
return this;
}
}