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

com.hadii.clarpse.sourcemodel.OOPSourceCodeModel Maven / Gradle / Ivy

There is a newer version: 7.1.1
Show newest version
package com.hadii.clarpse.sourcemodel;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * A language independent representation of a codebase that reveals its
 * structural buildup.
 */
public class OOPSourceCodeModel implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = LogManager.getLogger(OOPSourceCodeModel.class);
    private Map components = new HashMap<>();

    public OOPSourceCodeModel() {
    }

    public OOPSourceCodeModel(Map components) {
        this.components = new HashMap(components);
    }

    private Map getComponents() {
        return components;
    }

    public void merge(final OOPSourceCodeModel sourceModel) {
        insertComponents(sourceModel.getComponents());
    }

    public int size() {
        return components.size();
    }

    public void insertComponent(final Component component) {
        LOGGER.debug("Inserted component " + component + ".");
        components.put(component.uniqueName(), component);
    }

    public boolean containsComponent(final String componentName) {
        return getComponents().containsKey(componentName);
    }

    public Optional getComponent(final String componentName) {
        return Optional.ofNullable(this.getComponents().get(componentName));
    }

    public void insertComponents(final Map newCmps) {
        for (final Map.Entry entry : newCmps.entrySet()) {
            insertComponent(entry.getValue());
        }
    }

    public void removeComponent(String cmpUniqueName) {
        this.components.remove(cmpUniqueName);
        LOGGER.debug("Removed component " + cmpUniqueName + ".");
    }

    public Stream components() {
        return components.values().stream();
    }

    public OOPSourceCodeModel copy() {
        return new OOPSourceCodeModel(this.components);
    }

    /**
     * Fetches the current component's parent base component if it exists. This may
     * not be the component's direct parent.
     */
    public Component parentBaseCmp(String cmpUniqueName) throws IllegalArgumentException {
        String currParentClassName = cmpUniqueName;
        Optional parent;
        for (parent = this.getComponent(currParentClassName); parent.isPresent()
                && !parent.get().componentType().isBaseComponent(); parent = this.getComponent(currParentClassName)) {
            currParentClassName = parent.get().parentUniqueName();
        }
        if (parent.isPresent()) {
            return parent.get();
        } else {
            throw new IllegalArgumentException("No parent exists for given component: " + cmpUniqueName);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy