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

graphql.util.Traverser Maven / Gradle / Ivy

The newest version!
package graphql.util;

import graphql.Internal;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.Assert.assertTrue;
import static graphql.util.TraversalControl.CONTINUE;
import static graphql.util.TraversalControl.QUIT;

@Internal
public class Traverser {

    private final TraverserState traverserState;
    private final Function>> getChildren;
    private final Object initialAccumulate;
    private final Map, Object> rootVars = new ConcurrentHashMap<>();

    private static final List CONTINUE_OR_QUIT = Arrays.asList(CONTINUE, QUIT);

    private Traverser(TraverserState traverserState, Function>> getChildren, Object initialAccumulate) {
        this.traverserState = assertNotNull(traverserState);
        this.getChildren = assertNotNull(getChildren);
        this.initialAccumulate = initialAccumulate;
    }

    private static  Function>> wrapListFunction(Function> listFn) {
        return node -> {
            List childs = listFn.apply(node);
            return Collections.singletonMap(null, childs);
        };
    }

    public Traverser rootVars(Map, Object> rootVars) {
        this.rootVars.putAll(assertNotNull(rootVars));
        return this;
    }

    public Traverser rootVar(Class key, Object value) {
        rootVars.put(key, value);
        return this;
    }

    public static  Traverser depthFirst(Function> getChildren) {
        return depthFirst(getChildren, null, null);
    }

    public static  Traverser depthFirst(Function> getChildren, Object sharedContextData) {
        return depthFirst(getChildren, sharedContextData, null);
    }

    public static  Traverser depthFirst(Function> getChildren, Object sharedContextData, Object initialAccumulate) {
        Function>> mapFunction = wrapListFunction(getChildren);
        return new Traverser<>(TraverserState.newStackState(sharedContextData), mapFunction, initialAccumulate);
    }

    public static  Traverser depthFirstWithNamedChildren(Function>> getNamedChildren, Object sharedContextData, Object initialAccumulate) {
        return new Traverser<>(TraverserState.newStackState(sharedContextData), getNamedChildren, initialAccumulate);
    }

    public static  Traverser breadthFirst(Function> getChildren) {
        return breadthFirst(getChildren, null, null);
    }

    public static  Traverser breadthFirst(Function> getChildren, Object sharedContextData) {
        return breadthFirst(getChildren, sharedContextData, null);
    }

    public static  Traverser breadthFirst(Function> getChildren, Object sharedContextData, Object initialAccumulate) {
        Function>> mapFunction = wrapListFunction(getChildren);
        return new Traverser<>(TraverserState.newQueueState(sharedContextData), mapFunction, initialAccumulate);
    }

    public static  Traverser breadthFirstWithNamedChildren(Function>> getNamedChildren, Object sharedContextData, Object initialAccumulate) {
        return new Traverser<>(TraverserState.newQueueState(sharedContextData), getNamedChildren, initialAccumulate);
    }

    public TraverserResult traverse(T root, TraverserVisitor visitor) {
        return traverse(Collections.singleton(root), visitor);
    }

    public TraverserResult traverse(Collection roots, TraverserVisitor visitor) {
        assertNotNull(roots);
        assertNotNull(visitor);


        // "artificial" parent context for all roots with rootVars
        DefaultTraverserContext rootContext = traverserState.newRootContext(rootVars);
        traverserState.addNewContexts(roots, rootContext);

        DefaultTraverserContext currentContext;
        Object currentAccValue = initialAccumulate;
        traverseLoop:
        while (!traverserState.isEmpty()) {
            Object top = traverserState.pop();

            if (top instanceof TraverserState.EndList) {
                Map>> childrenContextMap = ((TraverserState.EndList) top).childrenContextMap;
                // end-of-list marker, we are done recursing children,
                // mark the current node as fully visited
                currentContext = (DefaultTraverserContext) traverserState.pop();
                currentContext.setCurAccValue(currentAccValue);
                currentContext.setChildrenContexts(childrenContextMap);
                currentContext.setPhase(TraverserContext.Phase.LEAVE);
                TraversalControl traversalControl = visitor.leave(currentContext);
                currentAccValue = currentContext.getNewAccumulate();
                assertNotNull(traversalControl, () -> "result of leave must not be null");
                assertTrue(CONTINUE_OR_QUIT.contains(traversalControl), () -> "result can only return CONTINUE or QUIT");

                switch (traversalControl) {
                    case QUIT:
                        break traverseLoop;
                    case CONTINUE:
                        continue;
                    default:
                        assertShouldNeverHappen();
                }
            }

            currentContext = (DefaultTraverserContext) top;

            if (currentContext.isVisited()) {
                currentContext.setCurAccValue(currentAccValue);
                currentContext.setPhase(TraverserContext.Phase.BACKREF);
                TraversalControl traversalControl = visitor.backRef(currentContext);
                currentAccValue = currentContext.getNewAccumulate();
                assertNotNull(traversalControl, () -> "result of backRef must not be null");
                assertTrue(CONTINUE_OR_QUIT.contains(traversalControl), () -> "backRef can only return CONTINUE or QUIT");
                if (traversalControl == QUIT) {
                    break traverseLoop;
                }
            } else {
                currentContext.setCurAccValue(currentAccValue);
                Object nodeBeforeEnter = currentContext.thisNode();
                currentContext.setPhase(TraverserContext.Phase.ENTER);
                TraversalControl traversalControl = visitor.enter(currentContext);
                currentAccValue = currentContext.getNewAccumulate();
                assertNotNull(traversalControl, () -> "result of enter must not be null");
                this.traverserState.addVisited((T) nodeBeforeEnter);
                switch (traversalControl) {
                    case QUIT:
                        break traverseLoop;
                    case ABORT:
                        continue;
                    case CONTINUE:
                        traverserState.pushAll(currentContext, getChildren);
                        continue;
                    default:
                        assertShouldNeverHappen();
                }
            }
        }

        TraverserResult traverserResult = new TraverserResult(currentAccValue);
        return traverserResult;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy