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

com.alphasystem.docbook.builder.impl.AbstractBuilder Maven / Gradle / Ivy

The newest version!
package com.alphasystem.docbook.builder.impl;

import com.alphasystem.commons.util.AppUtil;
import com.alphasystem.docbook.builder.Builder;
import com.alphasystem.docbook.builder.BuilderFactory;
import com.alphasystem.docbook.util.ConfigurationUtils;
import com.alphasystem.docbook.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;


public abstract class AbstractBuilder implements Builder {

    protected final Logger logger = LoggerFactory.getLogger(getClass());
    protected final ConfigurationUtils configurationUtils = ConfigurationUtils.getInstance();
    protected final BuilderFactory builderFactory = BuilderFactory.getInstance();
    protected String id;
    protected String role;
    protected Builder parent;
    protected S source;
    private final String childContentMethodName;

    protected AbstractBuilder(S source, Builder parent) {
        this("getContent", source, parent);
    }

    protected AbstractBuilder(String childContentMethodName, S source, Builder parent) {
        if (source == null) {
            throw new NullPointerException(String.format("Source object is null in \"%s\"", getClass().getName()));
        }
        this.source = source;
        this.parent = parent;
        this.id = Utils.getId(source);
        this.role = (String) AppUtil.invokeMethod(source, "getRole");
        this.childContentMethodName = childContentMethodName;
    }

    public String getId() {
        return id;
    }

    @Override
    public Builder getParent() {
        return parent;
    }

    @Override
    public S getSource() {
        return source;
    }

    @Override
    public List process() {
        preProcess();
        return doProcess(processChildContent(getChildContent()));
    }

    protected void preProcess() {
    }

    @SuppressWarnings("unchecked")
    protected List getChildContent() {
        if (StringUtils.isNotBlank(childContentMethodName)) {
            return (List) AppUtil.invokeMethod(source, childContentMethodName);
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public String getRole() {
        if (Objects.isNull(role)) {
            role = (String) AppUtil.invokeMethod(source, "getRole");
        }
        return role;
    }

    protected List processChildContent(List childContent) {
        return childContent.stream().map(content -> builderFactory.process(content, this))
                .flatMap(Collection::stream).collect(Collectors.toList());
    }

    protected List doProcess(List processedChildContent) {
        return processedChildContent;
    }

    @SuppressWarnings({"unchecked"})
    protected > B getParent(Class parentBuilderClass) {
        B result = null;
        var currentParent = parent;
        while (currentParent != null) {
            if (AppUtil.isInstanceOf(parentBuilderClass, currentParent)) {
                result = (B) currentParent;
                break;
            }
            currentParent = currentParent.getParent();
        }
        return result;
    }

    /**
     * Returns list of parent builders of this builder, will return empty list if there is no parent.
     * @return list of parents of this builder.
     */
    protected List> getParents() {
        final var parents = new ArrayList>();
        var currentParent = parent;
        while (currentParent != null) {
            parents.add(currentParent);
            currentParent = currentParent.getParent();
        }
        return parents;
    }
}