com.github.leeonky.jfactory.DefaultBuilder Maven / Gradle / Ivy
The newest version!
package com.github.leeonky.jfactory;
import com.github.leeonky.util.BeanClass;
import com.github.leeonky.util.CollectionHelper;
import java.util.*;
import java.util.stream.Collectors;
import static com.github.leeonky.jfactory.DefaultBuilder.BuildFrom.SPEC;
import static com.github.leeonky.util.BeanClass.cast;
import static java.util.Arrays.asList;
import static java.util.Objects.hash;
class DefaultBuilder implements Builder {
enum BuildFrom {
TYPE, SPEC
}
private final ObjectFactory objectFactory;
private final JFactory jFactory;
private final Set traits = new LinkedHashSet<>();
private final KeyValueCollection properties;
private final DefaultArguments arguments = new DefaultArguments();
private int collectionSize = 0;
private final BuildFrom buildFrom;
public DefaultBuilder(ObjectFactory objectFactory, JFactory jFactory, BuildFrom buildFrom) {
this.jFactory = jFactory;
this.objectFactory = objectFactory;
properties = new KeyValueCollection(objectFactory.getFactorySet());
this.buildFrom = buildFrom;
}
@Override
public T create() {
return createProducer().doDependenciesAndLinks().getValue();
}
@Override
public BeanClass getType() {
return objectFactory.getType();
}
@Override
public ObjectProducer createProducer() {
return new ObjectProducer<>(jFactory, objectFactory, this);
}
@Override
public Builder arg(String key, Object value) {
DefaultBuilder newBuilder = clone();
newBuilder.arguments.put(key, value);
return newBuilder;
}
@Override
public Builder args(Arguments arguments) {
DefaultBuilder newBuilder = clone();
newBuilder.arguments.merge((DefaultArguments) arguments);
return newBuilder;
}
@Override
public Builder args(Map args) {
DefaultBuilder newBuilder = clone();
args.forEach(newBuilder.arguments::put);
return newBuilder;
}
@Override
public Builder args(String property, Map args) {
DefaultBuilder newBuilder = clone();
args.forEach((key, value) -> newBuilder.arguments.put(property, key, value));
return newBuilder;
}
@Override
public Builder traits(String... traits) {
DefaultBuilder newBuilder = clone();
newBuilder.traits.addAll(asList(traits));
return newBuilder;
}
@Override
public DefaultBuilder clone() {
return clone(objectFactory, buildFrom);
}
private DefaultBuilder clone(ObjectFactory objectFactory, BuildFrom from) {
DefaultBuilder builder = new DefaultBuilder<>(objectFactory, jFactory, from);
builder.properties.appendAll(properties);
builder.traits.addAll(traits);
builder.arguments.merge(arguments);
return builder;
}
@Override
public Builder properties(Map properties) {
DefaultBuilder newBuilder = clone();
properties.forEach((key, value) -> {
String property;
property = replaceStartsWithIndexBracket(jFactory.aliasSetStore.resolve(
objectFactory, key, isCollection(value)), newBuilder);
if (isCollection(value)) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy