Example usage for java.util Deque addLast

List of usage examples for java.util Deque addLast

Introduction

In this page you can find the example usage for java.util Deque addLast.

Prototype

void addLast(E e);

Source Link

Document

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

Usage

From source file:org.nuxeo.ecm.core.storage.ExpressionEvaluator.java

private static Set<String> parseFullText(String string, int phraseSize) {
    if (string == null) {
        return Collections.emptySet();
    }/*from w  w w.j  a v  a  2s . com*/
    Set<String> set = new HashSet<String>();
    Deque<String> phraseWords = new LinkedList<>();
    for (String word : WORD_PATTERN.split(string)) {
        word = parseWord(word);
        if (word != null) {
            word = word.toLowerCase();
            set.add(word);
            if (phraseSize > 1) {
                phraseWords.addLast(word);
                if (phraseWords.size() > 1) {
                    if (phraseWords.size() > phraseSize) {
                        phraseWords.removeFirst();
                    }
                    addPhraseWords(set, phraseWords);
                }
            }
        }
    }
    while (phraseWords.size() > 2) {
        phraseWords.removeFirst();
        addPhraseWords(set, phraseWords);
    }
    return set;
}

From source file:org.apache.oozie.workflow.lite.LiteWorkflowValidator.java

private void checkCycle(Deque<String> path, String nodeName) throws WorkflowException {
    if (path.contains(nodeName)) {
        path.addLast(nodeName);
        throw new WorkflowException(ErrorCode.E0707, nodeName, Joiner.on("->").join(path));
    }/*from w ww .  j  a  v a2  s .  com*/
}

From source file:net.sf.jasperreports.engine.json.expression.member.evaluation.AbstractMemberExpressionEvaluator.java

protected void addChildrenToStack(JRJsonNode stackNode, Deque<JRJsonNode> stack) {
    JsonNode stackDataNode = stackNode.getDataNode();

    if (stackDataNode.isObject()) {
        Iterator<Map.Entry<String, JsonNode>> it = stackDataNode.fields();

        while (it.hasNext()) {
            JsonNode current = it.next().getValue();
            stack.addLast(stackNode.createChild(current));
        }//from w w  w .  j  a v  a 2s.  c  o  m
    }
    // if array => push all children
    else if (stackDataNode.isArray()) {
        for (JsonNode deeper : stackDataNode) {
            stack.addLast(stackNode.createChild(deeper));
        }
    }
}

From source file:org.lilyproject.indexer.engine.ValueEvaluator.java

private List<String> extractContent(String table, List<IndexValue> indexValues, LRepository repository) {
    // At this point we can be sure the value will be a blob, this is
    // validated during
    // the construction of the indexer conf.

    List<String> result = new ArrayList<String>(indexValues.size());

    Deque<Integer> indexes = new ArrayDeque<Integer>();

    for (IndexValue indexValue : indexValues) {
        indexes.clear();/*  w w w  .  ja  va2s  .c  o m*/

        if (indexValue.listIndex != null) {
            indexes.addLast(indexValue.listIndex);
        }

        extractContent(table, indexValue.value, indexes, indexValue.record, indexValue.fieldType, result,
                repository);
    }

    return result.isEmpty() ? null : result;
}

From source file:ro.hasna.ts.math.ml.distance.DynamicTimeWarpingDistance.java

/**
 * <p>/*from w  w w. j av  a 2  s  .c  o m*/
 * Reference:
 * Daniel Lemire (2008)
 * <i>Faster Sequential Search with a Two-Pass Dynamic-Time-Warping Lower Bound</i>
 * </p>
 *
 * @param v      the vector
 * @param n      the length of the vector
 * @param radius the Sakoe-Chiba band radius
 * @return time series envelope
 */
protected Envelope computeLemireEnvelope(double[] v, int n, int radius) {
    int w = 2 * radius + 1;
    double[] upper = new double[n];
    double[] lower = new double[n];
    int i;

    Deque<Integer> upperList = new LinkedList<>();
    Deque<Integer> lowerList = new LinkedList<>();
    upperList.addLast(0);
    lowerList.addLast(0);
    for (i = 1; i < n; i++) {
        if (i >= w) {
            upper[i - w] = v[upperList.getFirst()];
            lower[i - w] = v[lowerList.getFirst()];
        }
        if (v[i] > v[i - 1]) {
            upperList.removeLast();
            while (!upperList.isEmpty() && v[i] > v[upperList.getLast()]) {
                upperList.removeLast();
            }
        } else {
            lowerList.removeLast();
            while (!lowerList.isEmpty() && v[i] < v[lowerList.getLast()]) {
                lowerList.removeLast();
            }
        }
        upperList.addLast(i);
        lowerList.addLast(i);
        if (i == 2 * w + upperList.getFirst()) {
            upperList.removeFirst();
        } else if (i == 2 * w + lowerList.getFirst()) {
            lowerList.removeFirst();
        }
    }

    for (i = n; i < n + w; i++) {
        upper[i - w] = v[upperList.getFirst()];
        lower[i - w] = v[lowerList.getFirst()];
        if (i - upperList.getFirst() >= 2 * w) {
            upperList.removeFirst();
        }
        if (i - lowerList.getFirst() >= 2 * w) {
            lowerList.removeFirst();
        }
    }

    return new Envelope(lower, upper);
}

From source file:org.lilyproject.indexer.engine.ValueEvaluator.java

private void extractContent(String table, Object value, Deque<Integer> indexes, Record record,
        FieldType fieldType, List<String> result, LRepository repository) {

    if (value instanceof List) { // this covers both LIST and PATH types
        List values = (List) value;
        for (int i = 0; i < values.size(); i++) {
            indexes.addLast(i);
            extractContent(table, values.get(i), indexes, record, fieldType, result, repository);
            indexes.removeLast();//w  w w. j a  va 2 s.  c  om
        }
    } else {
        extractContent(table, value, record, fieldType, Ints.toArray(indexes), result, repository);
    }
}

From source file:com.roche.sequencing.bioinformatics.common.utils.FileUtil.java

/**
 * taken from here http://codereview.stackexchange.com/questions/47923/simplifying-a-path -Kurt Heilman
 * //  w ww.j a v a 2  s . c om
 * @param path
 * @return
 */
public static String simplifyPath(String path) {
    String simplifiedPath = null;
    Deque<String> pathDeterminer = new ArrayDeque<String>();
    path = path.replaceAll(Pattern.quote("\\"), "/");
    path = path.replaceAll(Pattern.quote("\\\\"), "/");
    String[] pathSplitter = path.split("/");
    StringBuilder absolutePath = new StringBuilder();
    for (String term : pathSplitter) {
        if (term == null || term.length() == 0 || term.equals(".")) {
            /* ignore these guys */
        } else if (term.equals("..")) {
            if (pathDeterminer.size() > 0) {
                pathDeterminer.removeLast();
            }
        } else {
            pathDeterminer.addLast(term);
        }
    }
    if (pathDeterminer.isEmpty()) {
        simplifiedPath = "/";
    } else {
        while (!pathDeterminer.isEmpty()) {
            absolutePath.insert(0, pathDeterminer.removeLast());
            absolutePath.insert(0, "/");
        }
        simplifiedPath = absolutePath.toString();
    }
    return simplifiedPath;
}

From source file:org.apache.pig.newplan.BaseOperatorPlan.java

/**
 * Move everything below a given operator to the new operator plan.  The specified operator will
 * be moved and will be the root of the new operator plan
 * @param root Operator to move everything after
 * @param newPlan new operator plan to move things into
 * @throws PlanException /*  www.ja  va2 s.  co  m*/
 */
public void moveTree(Operator root, BaseOperatorPlan newPlan) throws FrontendException {
    Deque<Operator> queue = new ArrayDeque<Operator>();
    newPlan.add(root);
    root.setPlan(newPlan);
    queue.addLast(root);
    while (!queue.isEmpty()) {
        Operator node = queue.poll();
        if (getSuccessors(node) != null) {
            for (Operator succ : getSuccessors(node)) {
                if (!queue.contains(succ)) {
                    queue.addLast(succ);
                    newPlan.add(succ);
                    succ.setPlan(newPlan);
                    newPlan.connect(node, succ);
                }
            }
        }
    }

    trimBelow(root);
}

From source file:org.apache.oozie.workflow.lite.LiteWorkflowValidator.java

/**
 * This method recursively validates two things:
 * - fork/join methods are properly paired
 * - there are no multiple "okTo" paths to a given node
 *
 * Important: this method assumes that the workflow is not acyclic - therefore this must run after performBasicValidation()
 *
 * @param app The WorkflowApp/*from  w w  w .ja  va 2  s  .c  om*/
 * @param node Current node we're checking
 * @param currentFork Current fork node (null if we are not under a fork path)
 * @param topDecisionParent The top (eldest) decision node along the path to this node, or null if there isn't one
 * @param okPath false if node (or an ancestor of node) was gotten to via an "error to" transition or via a join node that has
 * already been visited at least once before
 * @param forkJoins Map that contains a mapping of fork-join node pairs.
 * @param nodeAndDecisionParents Map that contains a mapping of nodes and their eldest decision node
 * @throws WorkflowException If there is any of the constraints described above is violated
 */
private void validateForkJoin(LiteWorkflowApp app, NodeDef node, NodeDef currentFork, String topDecisionParent,
        boolean okPath, Deque<String> path, Map<String, String> forkJoins,
        Map<String, Optional<String>> nodeAndDecisionParents) throws WorkflowException {
    final String nodeName = node.getName();

    path.addLast(nodeName);

    /* If we're walking an "okTo" path and the nodes are not Kill/Join/End, we have to make sure that only a single
     * "okTo" path exists to the current node.
     *
     * The "topDecisionParent" represents the eldest decision in the chain that we've gone through. For example, let's assume
     * that D1, D2, D3 are decision nodes and A is an action node.
     *
     * D1-->D2-->D3---> ... (rest of the WF)
     *  |   |    |
     *  |   |    |
     *  |   |    +----> +---+
     *  |   +---------> | A |
     *  +-------------> +---+
     *
     * In this case, there are three "okTo" paths to "A" but it's still a valid workflow because the eldest decision node
     * is D1 and during every run, there is only one possible execution path that leads to A (D1->A, D1->D2->A or
     * (D1->D2->D3->A). In the code, if we encounter a decision node and we already have one, we don't update it. If it's null
     * then we set it to the current decision node we're under.
     *
     * If the "current" and "top" parents are null, it means that we reached the node from two separate "okTo" paths, which is
     * not acceptable.
     *
     * Also, if we have two distinct top decision parents it means that the node is reachable from two decision paths which
     * are not "chained" (like in the example).
     *
     * It's worth noting that the last two examples can only occur in case of fork-join when we start to execute at least
     * two separate paths in parallel. Without fork-join, multiple parents or two null parents would mean that there is a loop
     * in the workflow but that should not happen since it has been validated.
     */
    if (okPath && !(node instanceof KillNodeDef) && !(node instanceof JoinNodeDef)
            && !(node instanceof EndNodeDef)) {
        // using Optional here so we can distinguish between "non-visited" and "visited - no parent" state.
        Optional<String> decisionParentOpt = nodeAndDecisionParents.get(nodeName);
        if (decisionParentOpt == null) {
            nodeAndDecisionParents.put(node.getName(), Optional.fromNullable(topDecisionParent));
        } else {
            String decisionParent = decisionParentOpt.isPresent() ? decisionParentOpt.get() : null;

            if ((decisionParent == null && topDecisionParent == null)
                    || !Objects.equal(decisionParent, topDecisionParent)) {
                throw new WorkflowException(ErrorCode.E0743, nodeName);
            }
        }
    }

    /* Fork-Join validation logic:
     *
     * At each Fork node, we recurse to every possible paths, changing the "currentFork" variable to the Fork node. We stop
     * walking as soon as we encounter a Join node. At the Join node, we update the forkJoin mapping, which maintains
     * the relationship between every fork-join pair (actually it's join->fork mapping). We check whether the join->fork
     * mapping already contains another Fork node, which means that the Join is reachable from at least two distinct
     * Fork nodes, so we terminate the validation.
     *
     * From the Join node, we don't recurse further. Therefore, all recursive calls return back to the point where we called
     * validateForkJoin() from the Fork node in question.
     *
     * At this point, we have to check how many different Join nodes we've found at each different paths. We collect them to
     * a set, then we make sure that we have only a single Join node for all Fork paths. Otherwise the workflow is broken.
     *
     * If we have only a single Join, then we get the transition node from the Join and go on with the recursive validation -
     * this time we use the original "currentFork" variable that we have on the stack. With this approach, nested
     * Fork-Joins are handled correctly.
     */
    if (node instanceof ForkNodeDef) {
        final List<String> transitions = node.getTransitions();

        checkForkTransitions(app, transitions, node);

        for (String t : transitions) {
            NodeDef transition = app.getNode(t);
            validateForkJoin(app, transition, node, topDecisionParent, okPath, path, forkJoins,
                    nodeAndDecisionParents);
        }

        // get the Join node for this ForkNode & validate it (we must have only one)
        Set<String> joins = new HashSet<String>();
        collectJoins(app, forkJoins, nodeName, joins);
        checkJoins(joins, nodeName);

        List<String> joinTransitions = app.getNode(joins.iterator().next()).getTransitions();
        NodeDef next = app.getNode(joinTransitions.get(0));

        validateForkJoin(app, next, currentFork, topDecisionParent, okPath, path, forkJoins,
                nodeAndDecisionParents);
    } else if (node instanceof JoinNodeDef) {
        if (currentFork == null) {
            throw new WorkflowException(ErrorCode.E0742, node.getName());
        }

        // join --> fork mapping
        String forkNode = forkJoins.get(nodeName);
        if (forkNode == null) {
            forkJoins.put(nodeName, currentFork.getName());
        } else if (!forkNode.equals(currentFork.getName())) {
            throw new WorkflowException(ErrorCode.E0758, node.getName(), forkNode + "," + currentFork);
        }
    } else if (node instanceof DecisionNodeDef) {
        List<String> transitions = node.getTransitions();

        // see explanation above - if we already have a topDecisionParent, we don't update it
        String parentDecisionNode = topDecisionParent;
        if (parentDecisionNode == null) {
            parentDecisionNode = nodeName;
        }

        for (String t : transitions) {
            NodeDef transition = app.getNode(t);
            validateForkJoin(app, transition, currentFork, parentDecisionNode, okPath, path, forkJoins,
                    nodeAndDecisionParents);
        }
    } else if (node instanceof KillNodeDef) {
        // no op
    } else if (node instanceof EndNodeDef) {
        // We can't end the WF if we're on a Fork path. From the "path" deque, we remove the last node (which
        // is the current "End") and look at last node again so we know where we came from
        if (currentFork != null) {
            path.removeLast();
            String previous = path.peekLast();
            throw new WorkflowException(ErrorCode.E0737, previous, node.getName());
        }
    } else if (node instanceof ActionNodeDef) {
        String transition = node.getTransitions().get(0); // "ok to" transition
        NodeDef okNode = app.getNode(transition);
        validateForkJoin(app, okNode, currentFork, topDecisionParent, true, path, forkJoins,
                nodeAndDecisionParents);

        transition = node.getTransitions().get(1); // "error to" transition
        NodeDef errorNode = app.getNode(transition);
        validateForkJoin(app, errorNode, currentFork, topDecisionParent, false, path, forkJoins,
                nodeAndDecisionParents);
    } else if (node instanceof StartNodeDef) {
        String transition = node.getTransitions().get(0); // start always has only 1 transition
        NodeDef tranNode = app.getNode(transition);
        validateForkJoin(app, tranNode, currentFork, topDecisionParent, okPath, path, forkJoins,
                nodeAndDecisionParents);
    } else {
        throw new WorkflowException(ErrorCode.E0740, node.getClass());
    }

    path.remove(nodeName);
}

From source file:uniol.apt.analysis.isomorphism.IsomorphismLogicComplex.java

/**
 * Isomorphism-check for current state/* www . j a  v a2 s .  co m*/
 *
 * @param state current state
 * @return true if node-pair m, n in state s is isomorphic.
 */
private boolean doMatch(ExtendedState state) {
    // Statal-tree, that stores all states
    Deque<ExtendedState> states = new LinkedList<>();

    // Add current state to list of states
    states.addLast(state);
    int depth = 0;

    while (!states.isEmpty()) {
        ExtendedState s = states.getLast();
        depth = s.depth;

        if (depth > 0) {
            core1.put(s.n, s.m);
            core2.put(s.m, s.n);
        }
        if (depth == numNodes) {
            break;
        }
        if (!s.active) {
            computeTerminalSets(depth, s);
        }

        Pair<State, State> node = computeP(depth, s);

        State n, m;
        //boolean that is true, if (m, n) is an isomorphic pair
        boolean goodState = false;
        s.active = true;
        for (; node != null; node = computeP(depth, s)) {
            InterrupterRegistry.throwIfInterruptRequestedForCurrentThread();
            n = node.getFirst();
            m = node.getSecond();

            //if (m,n) is an isomorphic pair
            if (isFeasible(n, m)) {
                goodState = true;

                //increment depth because of new isomorphic pair and
                //set new pair (n,m) as current pair in new state
                ExtendedState news = new ExtendedState(s, depth + 1, n, m);

                if (!in1.containsKey(n)) {
                    in1.put(n, depth + 1);
                    news.numin1++;
                }
                if (!out1.containsKey(n)) {
                    out1.put(n, depth + 1);
                    news.numout1++;
                }
                if (!in2.containsKey(m)) {
                    in2.put(m, depth + 1);
                    news.numin2++;
                }
                if (!out2.containsKey(m)) {
                    out2.put(m, depth + 1);
                    news.numout2++;
                }

                //Add new state to state-tree
                states.addLast(news);

                break;
            }
        }

        //Discard current state if no isomorphic pair has been found
        if (!goodState) {
            rollback(depth, s.n, s.m);
            ExtendedState last = states.removeLast();
            assert last == s;
        }

    }

    //Set final state, to get pairs of isomorphic nodes.
    this.finalState = states;
    for (ExtendedState ext : states) {
        if (ext.depth > 0)
            isomorphism.put(ext.n, ext.m);
    }

    return (depth == numNodes);
}