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

com.github.tankist88.carpenter.generator.dto.PackageTree Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package com.github.tankist88.carpenter.generator.dto;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class PackageTree {
    private static final String DELIMETER = "\\.";

    private Node root;
    private Comparator packageComparator;

    public PackageTree() {
        this(null);
    }

    public PackageTree(Comparator packageComparator) {
        this.packageComparator = packageComparator;
        this.root = new Node<>(null, null, packageComparator);
    }

    public void addPackage(String element) {
        String[] packages = element.split(DELIMETER);
        Node prevNode = root;
        for (String p : packages) {
            Node node = searchNode(p, prevNode);
            prevNode = node == null ? addChild(prevNode, p) : node;
        }
    }

    private Node addChild(Node node, String value) {
        return node.addChild(value, packageComparator);
    }

    private Node searchNode(String value, Node node) {
        if (value.equals(node.getValue())) return node;
        if (node.getChilds().size() == 0) return null;
        for (Node c : node.getChilds()) {
            if (value.equals(c.getValue())) return c;
        }
        return null;
    }

    private Node searchNodeRecursive(String value, Node node) {
        if (value.equals(node.getValue())) return node;
        if (node.getChilds().size() == 0) return null;
        for (Node c : node.getChilds()) {
            Node res = searchNodeRecursive(value, c);
            if (res != null) return res;
        }
        return null;
    }

    private List> getNodesWithoutChilds(Node node) {
        List> result = new ArrayList<>();
        for (Node n : node.getChilds()) {
            if (n.getChilds().size() == 0) {
                result.add(n);
            } else {
                result.addAll(getNodesWithoutChilds(n));
            }
        }
        return result;
    }

    private String getPackage(Node node) {
        StringBuilder sb = new StringBuilder();
        if (node.getValue() != null) {
            sb.insert(0, node.getValue());
        }
        if (node.getParent() != null && node.getParent().getValue() != null) {
            sb.insert(0, ".");
            sb.insert(0, getPackage(node.getParent()));
        }
        return sb.toString();
    }

    public List toList() {
        List res = new ArrayList<>();
        for (Node node : getNodesWithoutChilds(root)) {
            res.add(getPackage(node));
        }
        return res;
    }

    public List cutOne() {
        List res = new ArrayList<>();
        for (Node node : getNodesWithoutChilds(root)) {
            int familySize = node.getParent().getChilds().size();
            String packageStr = familySize > 1 ? getPackage(node.getParent()) + ".*;" : getPackage(node);
            if (!res.contains(packageStr)) res.add(packageStr);
        }
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy