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

com.buschmais.jqassistant.plugin.yaml.impl.scanner.ProcessingContext Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
package com.buschmais.jqassistant.plugin.yaml.impl.scanner;

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.buschmais.jqassistant.plugin.yaml.api.model.YAMLDescriptor;
import com.buschmais.jqassistant.plugin.yaml.api.model.YAMLKeyDescriptor;

class ProcessingContext {
    private ArrayDeque stackedContext = new ArrayDeque<>();
    private LinkedList context = new LinkedList<>();

    public void push(YAMLDescriptor newContext) {
        stackedContext.push(newContext);
    }

    @SuppressWarnings("unchecked")
    public  T peek() {
        return (T)stackedContext.peek();
    }

    @SuppressWarnings("unchecked")
    public  T pop() {
        return (T)stackedContext.pop();
    }

    public void pushContextEvent(YAMLEmitter.ParseContext event) {
        context.push(event);
    }

    public YAMLEmitter.ParseContext getContext() {
        return context.peek();
    }

    public boolean isContext(YAMLEmitter.ParseContext... eventChain) {
        int pathLength = eventChain.length;
        int currentContextDepth = context.size();

        boolean result = true;

        if (pathLength <= currentContextDepth) {
            List tail = context.subList(0, pathLength);

            for (int i = pathLength - 1; i >= 0; i--) {
                if (eventChain[i] != tail.get(pathLength - 1 - i)) {
                    result = false;
                    break;
                }
            }
        } else {
            result = false;
        }

        return result;
    }

    public void popContextEvent(int elements) {
        for (int i = 0; i < elements; i++) {
            context.pop();
        }
    }

    public String buildNextFQN(String lastElement) {
        StringBuilder builder = new StringBuilder();

        Iterator descItr = stackedContext.descendingIterator();

        while (descItr.hasNext()) {
            YAMLDescriptor yamld = descItr.next();

            if (YAMLKeyDescriptor.class.isAssignableFrom(yamld.getClass())) {
                YAMLKeyDescriptor keyDescriptor = (YAMLKeyDescriptor) yamld;
                builder.append(keyDescriptor.getName()).append('.');
            }
        }

        String name = builder.append(lastElement).toString();

        return name;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy