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

ru.yandex.qatools.camelot.config.PluginTree Maven / Gradle / Ivy

There is a newer version: 2.5.4
Show newest version
package ru.yandex.qatools.camelot.config;

import java.util.*;

/**
 * @author Ilya Sadykov (mailto: [email protected])
 */
public class PluginTree implements Iterable {

    final private Plugin plugin;
    final private List children = new ArrayList<>();
    private List> routes;

    public PluginTree() {
        this.plugin = null;
    }

    public PluginTree(Plugin plugin) {
        this.plugin = plugin;
    }

    public Plugin getPlugin() {
        return plugin;
    }

    public List getChildren() {
        return children;
    }

    public PluginTree getChild(String id) {
        for (PluginTree child : children) {
            if (child.getPlugin() != null && child.getPlugin().getId().equals(id)) {
                return child;
            }
        }
        return null;
    }

    public List> getRoutes() {
        if (routes == null) {
            routes = new PluginTreeRouteBuilder(this).build();
        }
        return routes;
    }

    public static PluginTree buildTree(Map pluginMap) {
        PluginTree mainRoot = new PluginTree();
        final Map> consumers = findConsumers(pluginMap);
        buildLeaf(mainRoot, consumers, null);
        return mainRoot;
    }

    private static void buildLeaf(PluginTree branch, Map> consumers, String pluginId) {
        if (!consumers.containsKey(pluginId)) {
            return; // leaf
        }
        for (Plugin plugin : consumers.get(pluginId)) {
            PluginTree child = new PluginTree(plugin);
            branch.children.add(child);
            buildLeaf(child, consumers, child.getPlugin().getId());
        }
    }

    private static Map> findConsumers(Map pluginMap) {
        Map> result = new HashMap<>();
        for (final Plugin plugin : pluginMap.values()) {
            final String from = plugin.getSource();
            if (!result.containsKey(from)) {
                result.put(from, new ArrayList());
            }
            result.get(from).add(plugin);
        }
        return result;
    }

    @Override
    public Iterator iterator() {
        return new PluginTreeIterator(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy