All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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 objects = CollectionHelper.toStream(value).collect(Collectors.toList());
                if (objects.isEmpty() || !property.contains("$"))
                    newBuilder.properties.append(trimIndexAlias(property), objects);
                else for (int i = 0; i < objects.size(); i++)
                    newBuilder.properties.append(property.replaceFirst("\\$", String.valueOf(i)), objects.get(i));
            } else
                newBuilder.properties.append(property, value);
        });
        return newBuilder;
    }

    private String trimIndexAlias(String property) {
        if (property.contains("[$]"))
            return property.substring(0, property.indexOf("[$]"));
        return property;
    }

    private boolean isCollection(Object value) {
        return value != null && BeanClass.createFrom(value).isCollection();
    }

    private String replaceStartsWithIndexBracket(String key, DefaultBuilder newBuilder) {
        if (key.startsWith("[")) {
            String[] indexAndSub = key.substring(1).split("]", 2);
            newBuilder.collectionSize = Math.max(newBuilder.collectionSize, Integer.parseInt(indexAndSub[0]) + 1);
            return indexAndSub[0] + indexAndSub[1];
        }
        return key;
    }

    @Override
    public Collection queryAll() {
        KeyValueCollection.Matcher matcher = properties.matcher(objectFactory.getType(), objectFactory);
        return jFactory.getDataRepository().queryAll(objectFactory.getType().getType()).stream()
                .filter(matcher::matches).collect(Collectors.toList());
    }

    @Override
    public int hashCode() {
        return hash(DefaultBuilder.class, objectFactory, properties, traits);
    }

    @Override
    public boolean equals(Object another) {
        return cast(another, DefaultBuilder.class)
                .map(builder -> objectFactory.equals(builder.objectFactory) && properties.equals(builder.properties)
                        && traits.equals(builder.traits))
                .orElseGet(() -> super.equals(another));
    }

    public void processSpecAndInputProperty(ObjectProducer objectProducer, RootInstance instance) {
        collectSpec(objectProducer, instance);
        processInputProperty(objectProducer);
        instance.setCollectionSize(collectionSize);
    }

    private void collectSpec(ObjectProducer objectProducer, Instance instance) {
        objectFactory.collectSpec(traits, instance);
        instance.spec().apply(jFactory, objectProducer);
        objectProducer.processSpecIgnoreProperties();
    }

    private void processInputProperty(ObjectProducer producer) {
        properties.expressions(objectFactory.getType(), objectFactory).forEach(exp -> producer.changeChild(exp.getProperty(),
                intentlyCreateWhenReverseAssociation(producer, exp).buildProducer(jFactory, producer)));
    }

    private Expression intentlyCreateWhenReverseAssociation(ObjectProducer producer, Expression exp) {
        return exp instanceof SingleValueExpression ? exp :
                producer.isReverseAssociation(exp.getProperty()) ? exp.setIntently(true) : exp;
    }

    public DefaultBuilder marge(DefaultBuilder another) {
        ObjectFactory objectFactory = another.buildFrom == SPEC && another.objectFactory instanceof SpecClassFactory
                ? another.objectFactory : this.objectFactory;
        DefaultBuilder newBuilder = clone(objectFactory, BuildFrom.TYPE);
        newBuilder.properties.appendAll(another.properties);
        newBuilder.traits.addAll(another.traits);
        newBuilder.collectionSize = collectionSize;
        return newBuilder;
    }

    public DefaultArguments getArguments() {
        return arguments;
    }
}