Example usage for java.util Queue isEmpty

List of usage examples for java.util Queue isEmpty

Introduction

In this page you can find the example usage for java.util Queue isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection contains no elements.

Usage

From source file:info.magnolia.vaadin.periscope.result.SupplierUtil.java

/**
 * Highlight (using HTML tags) all occurrences of a query string, ignoring case.
 *
 * @param text Text in which parts should be highlighted
 * @param query Parts to highlight//from   w  w  w. ja  v a 2 s . co  m
 * @return Highlighted string
 */
public static String highlight(final String text, final String query) {
    if (StringUtils.isBlank(query)) {
        return text;
    }

    final List<Integer> startIndices = allIndicesOf(text, query);
    final List<Integer> endIndices = startIndices.stream().map(i -> i + query.length()).collect(toList());

    // we run back to front to not mess up indices when inserting tags
    Collections.reverse(startIndices);
    Collections.reverse(endIndices);
    Queue<Integer> startQueue = new LinkedList<>(startIndices);
    Queue<Integer> endQueue = new LinkedList<>(endIndices);

    StringBuilder highlighted = new StringBuilder(text);
    while (!startQueue.isEmpty() || !endQueue.isEmpty()) {
        final Integer startCandidate = startQueue.peek();
        final Integer endCandidate = endQueue.peek();

        if (startCandidate != null && (endCandidate == null || startCandidate > endCandidate)) {
            highlighted.insert(startCandidate, "<strong>");
            startQueue.poll();
        } else {
            highlighted.insert(endCandidate, "</strong>");
            endQueue.poll();
        }
    }

    return highlighted.toString();
}

From source file:org.stem.db.compaction.CompactionManager.java

private static void markAllOriginalFFsAsBlank(Queue<FatFile> originalFFs, MountPoint mp) throws IOException {
    while (!originalFFs.isEmpty()) {
        FatFile ff = originalFFs.poll();
        ff.reallocate();//from  w  ww.  j  a  v a 2  s.c o m
        StorageService.instance.submitFF(ff, mp); // TODO: inside we must re-count DataTracker
    }
}

From source file:org.jbpm.services.task.jaxb.ComparePair.java

public static void compareOrig(Object origObj, Object newObj, Class objClass) {
    ComparePair compare = new ComparePair(origObj, newObj, objClass);
    Queue<ComparePair> compares = new LinkedList<ComparePair>();
    compares.add(compare);/*w  ww. j  a  v a  2  s  .  co  m*/
    while (!compares.isEmpty()) {
        compares.addAll(compares.poll().compare());
    }
}

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

private static BidiMap<State, State> checkViaDepthSearch(TransitionSystem lts1, TransitionSystem lts2) {
    BidiMap<State, State> result = new DualHashBidiMap<>();
    Set<String> alphabet = lts1.getAlphabet();
    if (!alphabet.equals(lts2.getAlphabet()))
        // Not isomorphic, there is an arc with a label not occurring in the other lts
        return result;

    Queue<Pair<State, State>> unhandled = new ArrayDeque<>();
    visit(result, unhandled, lts1.getInitialState(), lts2.getInitialState());

    while (!unhandled.isEmpty()) {
        InterrupterRegistry.throwIfInterruptRequestedForCurrentThread();

        Pair<State, State> pair = unhandled.remove();
        State state1 = pair.getFirst();
        State state2 = pair.getSecond();

        for (String label : alphabet) {
            State follow1 = follow(state1, label);
            State follow2 = follow(state2, label);

            if (!visit(result, unhandled, follow1, follow2))
                // Not isomorphic
                return new DualHashBidiMap<>();
        }/*from w  w  w. j  a  va  2 s .c  o m*/
    }

    return result;
}

From source file:com.mohawk.webcrawler.ScriptCompiler.java

/**
 *
 * @param tokens/*from   w w w.j  a v a2 s.  c  o m*/
 * @param parentScope
 */
private static void addScope(Queue<String> tokens, Queue<? super BaseToken> parentScope)
        throws CompilationException {

    while (!tokens.isEmpty()) {

        String token = tokens.poll();
        if ("end".equals(token) || "else".equals(token) || "elseif".equals(token)) {
            parentScope.add(new BaseEndScope(token));
            break;
        } else if ("if".equals(token)) {
            String expression = tokens.poll();

            If_Verb ifVerb = new If_Verb();
            ifVerb.setExpression(expression);

            parentScope.add(ifVerb);
            addScope(tokens, ifVerb.createScope());

            // check if elseif or else is defined
            LinkedList<BaseToken> ifScope = ifVerb.getScope();
            Object elseToken = ifScope.peekLast();

            if (elseToken instanceof BaseEndScope) {
                // remove elseif or else from if scope
                ifScope.pollLast();

                while (elseToken instanceof BaseEndScope) {

                    String elseStr = ((BaseEndScope) elseToken).getName();
                    if ("end".equals(elseStr))
                        break;
                    else if ("elseif".equals(elseStr)) {

                        String exp = tokens.poll();
                        ElseIf_Verb elseIfVerb = new ElseIf_Verb();
                        elseIfVerb.setExpression(exp);
                        ifVerb.addElseIf(elseIfVerb);

                        addScope(tokens, elseIfVerb.createScope());
                        elseToken = elseIfVerb.getScope().pollLast();
                    } else if ("else".equals(elseStr)) {

                        Else_Verb elseVerb = new Else_Verb();
                        ifVerb.setElse(elseVerb);

                        addScope(tokens, elseVerb.createScope());
                        elseToken = elseVerb.getScope().pollLast();
                    }
                }
            }
        } else if ("while".equals(token)) {

            String evaluation = tokens.poll();

            While_Verb whileVerb = new While_Verb();
            whileVerb.setExpression(evaluation);

            parentScope.add(whileVerb);
            addScope(tokens, whileVerb.createScope());
        } else if (LangCore.isVerb(token)) { // verb
            try {
                parentScope.add(LangCore.createVerbToken((String) token));
            } catch (Exception e) {
                e.printStackTrace();
                throw new CompilationException(e.getLocalizedMessage());
            }
        } else if (LangCore.isLiteral(token)) { // literal
            try {
                parentScope.add(new BaseLiteral(LangCore.createLiteralObject(token)));
            } catch (LanguageException e) {
                throw new CompilationException(e.getLocalizedMessage());
            }
        } else if (LangCore.isOperator(token)) { // operator
            try {
                parentScope.add(LangCore.createOperatorToken(token));
            } catch (LanguageException e) {
                throw new CompilationException(e.getLocalizedMessage());
            }
        } else // default to variable
            parentScope.add(new BaseVariable(token));
    }
}

From source file:main.java.whiteSocket.Area.java

public static boolean[][] floodBorder(Area area, boolean[][] floodArea, int x, int y) {
    /**//from www .  j a va  2  s .  com
     * paint bucket-like algorithm to fill binary map border this filled
     * area becomes the area to be filtered through the stretch() area
     * pixels to be turned Color.WHITE (unless notified otherwise by user
     * dictation)
     *
     */

    if (!floodArea[y][x]) {

        Queue<Point> queue = new LinkedList<Point>();
        queue.add(new Point(x, y));

        while (!queue.isEmpty()) {

            Point p = queue.remove();

            if (!floodArea[p.y][p.x]) {

                floodArea[p.y][p.x] = true;

                //                  System.out.println("totalErase = " + totalErase);
                if (totalErase != null)
                    totalErase[p.y][p.x] = true;

                queue.add(new Point(p.x + 1, p.y));
                queue.add(new Point(p.x - 1, p.y));
                queue.add(new Point(p.x, p.y + 1));
                queue.add(new Point(p.x, p.y - 1));

            }
        }
    }

    return floodArea;
}

From source file:com.wrmsr.wava.basic.BasicLoopInfo.java

public static Set<Name> getLoopContents(Name loop, Multimap<Name, Name> inputs,
        Multimap<Name, Name> backEdges) {
    Set<Name> seen = new HashSet<>();
    seen.add(loop);//ww w  . j a  va 2 s . c  om
    Queue<Name> queue = new LinkedList<>();
    inputs.get(loop).stream().filter(n -> !n.equals(loop) && backEdges.containsEntry(loop, n))
            .forEach(queue::add);
    queue.forEach(seen::add);
    while (!queue.isEmpty()) {
        Name cur = queue.poll();
        inputs.get(cur).stream().filter(input -> !seen.contains(input)).forEach(input -> {
            seen.add(input);
            queue.add(input);
        });
    }
    return seen;
}

From source file:org.xwiki.xdomviz.Main.java

/**
 * <p>//  w w w .  j a v  a 2s.  com
 * This method performs a normalization of the tree by removing all the multi-edges (due to a node having multiple
 * parents). This happens often with SpaceBlocks, which are reused all over the XDOM. The algorithm performs a
 * breadth first visit. For each visited node, it checks how many times a child occurs in its children list. If a
 * child occurs more than once, then we create new nodes, one for each occurrence.
 * </p>
 * <p>
 * The node tree corresponding to the XDOM of "this is a test" is:
 * </p>
 * <ul>
 * <li>XDOM -> P</li>
 * <li>P -> "This"</li>
 * <li>P -> S (3 edges, each one representing a space)</li>
 * <li>P -> "is"</li>
 * <li>P -> "a"</li>
 * <li>P -> "test"</li>
 * </ul>
 * <p>
 * The normalized tree will be:
 * </p>
 * <ul>
 * <li>XDOM -> P</li>
 * <li>P -> "This"</li>
 * <li>P -> S</li>
 * <li>P -> "is"</li>
 * <li>P -> S</li>
 * <li>P -> "a"</li>
 * <li>P -> S</li>
 * <li>P -> "test"</li>
 * </ul>
 * <p>
 * In a normalized tree, each node has one and only one parent.
 * </p>
 * 
 * @param root The root node of the tree.
 * @return The root node of the normalized tree.
 */
private static Node normalize(Node root) {
    // Breadth first visit of the XDOM to assign simple ids to nodes.
    Queue<Node> nodesQueue = new ArrayDeque<Node>();
    nodesQueue.add(root);
    while (!nodesQueue.isEmpty()) {
        Node node = nodesQueue.poll();

        // This map contains, for the current node, where are the occurrences in the children list of each child.
        Map<Node, List<Integer>> nodeToIndexesMap = new HashMap<Node, List<Integer>>();

        int i = 0;
        // For each child calculate store the its position in the indexes list.
        for (Node child : node.getChildren()) {
            List<Integer> indexes = nodeToIndexesMap.get(child);
            if (indexes == null) {
                indexes = new ArrayList<Integer>();
                nodeToIndexesMap.put(child, indexes);
            }
            indexes.add(i);
            i++;
        }

        for (Node child : nodeToIndexesMap.keySet()) {
            List<Integer> indexes = nodeToIndexesMap.get(child);
            // If the indexes size is > 1 then a child occurs multiple times in a
            if (indexes.size() > 1) {
                for (Integer index : indexes) {
                    Node newNode = new Node(child.getBlock());
                    newNode.getParents().add(node);
                    newNode.getChildren().addAll(child.getChildren());
                    node.getChildren().set(index, newNode);
                }
            }
        }

        for (Node child : node.getChildren()) {
            nodesQueue.add(child);
        }
    }

    return root;
}

From source file:org.xwiki.xdomviz.Main.java

/**
 * <p>/*from   w ww. j a  va  2s .  com*/
 * This method creates an isomorphic tree using node structures instead of blocks. This is necessary because a
 * single XDOM's block can be a child of multiple parents but the getParent() method is able to return only a single
 * parent. Using this alternative representation, full parent information is correctly stored in each node.
 * </p>
 * <p>
 * The node tree representation allows also the manipulation of the tree structure because all the attributes of a
 * node are mutable.
 * </p>
 * 
 * @param xdom
 * @return The root node of the new tree.
 */
private static Node createNodeTree(XDOM xdom) {
    // The list of the nodes created from the visited XDOM's blocks.
    List<Node> nodes = new ArrayList<Node>();

    // Breadth first visit of the XDOM.
    Queue<Block> blocksQueue = new ArrayDeque<Block>();
    blocksQueue.add(xdom.getRoot());
    while (!blocksQueue.isEmpty()) {
        Block block = blocksQueue.poll();

        // If there isn't a node corresponding to this block, create it!
        Node parentNode = findNode(nodes, block);
        if (parentNode == null) {
            parentNode = new Node(block);
            nodes.add(parentNode);
        }

        for (Block child : block.getChildren()) {
            blocksQueue.add(child);

            // If there isn't a node corresponding to this child-block, create it!
            Node childNode = findNode(nodes, child);
            if (childNode == null) {
                childNode = new Node(child);
                nodes.add(childNode);
            }

            // Link parent and child child.
            parentNode.getChildren().add(childNode);
            childNode.getParents().add(parentNode);
        }
    }

    return findNode(nodes, xdom.getRoot());
}

From source file:org.jspringbot.keyword.expression.ELUtils.java

public static Object doCase(Object... args) {
    Object defaultValue = null;//from   w w  w .jav a 2s. c  o m

    Queue<Object> arguments = new LinkedList<Object>();
    arguments.addAll(Arrays.asList(args));

    while (!arguments.isEmpty()) {
        if (arguments.size() > 1) {
            boolean condition = (Boolean) arguments.remove();
            Object value = arguments.remove();
            if (condition) {
                return value;
            }
        } else {
            // default
            return arguments.remove();
        }
    }

    return defaultValue;
}