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

ru.yandex.qatools.camelot.config.PluginTreeRouteBuilder 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 PluginTreeRouteBuilder {

    private final Deque states = new LinkedList<>();

    public PluginTreeRouteBuilder(PluginTree pluginTree) {
        states.addLast(new State(pluginTree));
    }

    public List> build() {
        final List> routes = new ArrayList<>();
        while (!states.isEmpty()) {
            if (hasNextChild()) {
                final State state = state();
                final PluginTree nextChild = nextChild(state);
                states.addLast(new State(nextChild));
            } else {
                if (state().node.getChildren().isEmpty()) {
                    final List route = new ArrayList<>();
                    routes.add(route);
                    for (State state : states) {
                        route.add(state.node);
                    }
                }
                states.pollLast();
            }
        }
        return routes;
    }

    private PluginTree nextChild(State state) {
        return state.node.getChildren().get(state.childIndex++);
    }

    private State state() {
        return states.getFirst();
    }

    private boolean hasNextChild() {
        return !states.isEmpty() && state().childIndex < state().node.getChildren().size();
    }

    private static class State {
        private PluginTree node;
        private int childIndex = 0;

        private State(PluginTree node) {
            this.node = node;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy