Example usage for java.util Deque pop

List of usage examples for java.util Deque pop

Introduction

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

Prototype

E pop();

Source Link

Document

Pops an element from the stack represented by this deque.

Usage

From source file:Main.java

public static void main(String[] args) {

    Deque<Integer> deque = new ArrayDeque<Integer>(8);

    deque.add(1);/*  ww  w .j  a  va  2s.  c o m*/
    deque.add(2);
    deque.add(3);
    deque.add(4);

    System.out.println(deque);

    int retval = deque.pop();
    System.out.println("Element removed is " + retval);

    System.out.println(deque);
}

From source file:Main.java

public static void main(String args[]) {
    Deque<String> stack = new ArrayDeque<String>();
    Deque<String> queue = new ArrayDeque<String>();

    stack.push("A");
    stack.push("B");
    stack.push("C");
    stack.push("D");

    while (!stack.isEmpty())
        System.out.print(stack.pop() + " ");

    queue.add("A");
    queue.add("B");
    queue.add("C");
    queue.add("D");
    while (!queue.isEmpty())
        System.out.print(queue.remove() + " ");
}

From source file:Main.java

public static void main(String[] args) {
    // Create a Deque and use it as stack
    Deque<String> deque = new ArrayDeque<>();
    deque.push("Oracle");
    deque.push("HTML");
    deque.push("CSS");
    deque.push("XML");

    System.out.println("Stack: " + deque);

    // remove all elements from the Deque
    while (deque.peek() != null) {
        System.out.println("Element at  top:  " + deque.peek());
        System.out.println("Popped: " + deque.pop());
        System.out.println("Stack: " + deque);
    }//from  w  ww. j a  v  a2 s. c o m

    System.out.println("Stack is  empty:  " + deque.isEmpty());
}

From source file:com.cinchapi.concourse.lang.Parser.java

/**
 * An the appropriate {@link AST} node to the {@code stack} based on
 * {@code operator}./*  w ww  .j a  va2 s . co  m*/
 * 
 * @param stack
 * @param operator
 */
private static void addASTNode(Deque<AST> stack, Symbol operator) {
    AST right = stack.pop();
    AST left = stack.pop();
    if (operator == ConjunctionSymbol.AND) {
        stack.push(AndTree.create(left, right));
    } else {
        stack.push(OrTree.create(left, right));
    }
}

From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.AbstractOAuth2Base.java

protected static void createOrDescend(JsonObject root, Deque<String> keyPath, String value) {
    // If there are still key-path elements remaining to traverse.
    if (keyPath.size() > 1) {
        // If there's no object already at this key-path create a new JsonObject.
        if (root.getJsonObject(keyPath.peek()) == null) {
            JsonObject newJson = new JsonObject();
            String val = keyPath.pop();
            root.put(val, newJson);
            createOrDescend(newJson, keyPath, value);
        } else { // If there's already an existing object on key-path, grab it and traverse.
            createOrDescend(root.getJsonObject(keyPath.pop()), keyPath, value);
        }//from  w w w .  j av a  2 s.  c  om
    } else { // Set the value.
        Boolean boolObj = BooleanUtils.toBooleanObject(value);
        if (boolObj != null) {
            root.put(keyPath.pop(), boolObj);
        } else if (StringUtils.isNumeric(value)) {
            root.put(keyPath.pop(), Long.parseLong(value));
        } else {
            root.put(keyPath.pop(), value);
        }
    }
}

From source file:PathUtils.java

/**
 * Calculates the relative path between a specified root directory and a target path.
 *
 * @param root   The absolute path of the root directory.
 * @param target The path to the target file or directory.
 * @return The relative path between the specified root directory and the target path.
 * @throws IllegalArgumentException <ul><li>The root file cannot be null.</li><li>The target cannot be
 *                                  null.</li><li>The root file must be a directory.</li><li>The root file must be
 *                                  absolute.</li></ul>
 *//*from w w w  .  j  av a 2s. c om*/
public static String relativize(final File root, final File target) throws IllegalArgumentException {
    if (root == null)
        throw new IllegalArgumentException("The root file cannot be null.");
    if (target == null)
        throw new IllegalArgumentException("The target cannot be null.");
    if (!root.isDirectory())
        throw new IllegalArgumentException("The root file must be a directory.");
    if (!root.isAbsolute())
        throw new IllegalArgumentException("The root file must be absolute.");
    if (!target.isAbsolute())
        return target.toString();

    if (root.equals(target))
        return ".";

    // Deconstruct hierarchies
    final Deque<File> rootHierarchy = new ArrayDeque<File>();
    for (File f = root; f != null; f = f.getParentFile())
        rootHierarchy.push(f);
    final Deque<File> targetHierarchy = new ArrayDeque<File>();
    for (File f = target; f != null; f = f.getParentFile())
        targetHierarchy.push(f);

    // Trace common root
    while (rootHierarchy.size() > 0 && targetHierarchy.size() > 0
            && rootHierarchy.peek().equals(targetHierarchy.peek())) {
        rootHierarchy.pop();
        targetHierarchy.pop();
    }
    // Create relative path
    final StringBuilder sb = new StringBuilder(rootHierarchy.size() * 3 + targetHierarchy.size() * 32);
    while (rootHierarchy.size() > 0) {
        sb.append("..");
        rootHierarchy.pop();
        if (rootHierarchy.size() > 0 || targetHierarchy.size() > 0)
            sb.append(File.separator);
    }
    while (targetHierarchy.size() > 0) {
        sb.append(targetHierarchy.pop().getName());
        if (targetHierarchy.size() > 0)
            sb.append(File.separator);
    }
    return sb.toString();
}

From source file:com.cinchapi.concourse.lang.Parser.java

/**
 * Convert a valid and well-formed list of {@link Symbol} objects into a
 * an {@link AST}.//from   w  w w . j a v a  2  s  .  com
 * <p>
 * NOTE: This method will group non-conjunctive symbols into
 * {@link Expression} objects.
 * </p>
 * 
 * @param symbols
 * @return the symbols in an AST
 */
public static AST toAbstractSyntaxTree(List<Symbol> symbols) {
    Deque<Symbol> operatorStack = new ArrayDeque<Symbol>();
    Deque<AST> operandStack = new ArrayDeque<AST>();
    symbols = groupExpressions(symbols);
    main: for (Symbol symbol : symbols) {
        if (symbol == ParenthesisSymbol.LEFT) {
            operatorStack.push(symbol);
        } else if (symbol == ParenthesisSymbol.RIGHT) {
            while (!operatorStack.isEmpty()) {
                Symbol popped = operatorStack.pop();
                if (popped == ParenthesisSymbol.LEFT) {
                    continue main;
                } else {
                    addASTNode(operandStack, popped);
                }
            }
            throw new SyntaxException(
                    MessageFormat.format("Syntax error in {0}: Mismatched parenthesis", symbols));
        } else if (symbol instanceof Expression) {
            operandStack.add(ExpressionTree.create((Expression) symbol));
        } else {
            operatorStack.push(symbol);
        }
    }
    while (!operatorStack.isEmpty()) {
        addASTNode(operandStack, operatorStack.pop());
    }
    return operandStack.pop();
}

From source file:net.myrrix.common.io.IOUtils.java

/**
 * Attempts to recursively delete a directory. This may not work across symlinks.
 *
 * @param dir directory to delete along with contents
 * @return {@code true} if all files and dirs were deleted successfully
 *///from w ww  .j a va  2s  . c o  m
public static boolean deleteRecursively(File dir) {
    if (dir == null) {
        return false;
    }
    Deque<File> stack = new ArrayDeque<File>();
    stack.push(dir);
    boolean result = true;
    while (!stack.isEmpty()) {
        File topElement = stack.peek();
        if (topElement.isDirectory()) {
            File[] directoryContents = topElement.listFiles();
            if (directoryContents != null && directoryContents.length > 0) {
                for (File fileOrSubDirectory : directoryContents) {
                    stack.push(fileOrSubDirectory);
                }
            } else {
                result = result && stack.pop().delete();
            }
        } else {
            result = result && stack.pop().delete();
        }
    }
    return result;
}

From source file:io.hawt.osgi.jmx.RBACDecorator.java

/**
 * Checks if two {@link ObjectName}s may share RBAC info - if the same configadmin PIDs are examined by Karaf
 * @param realJmxAclPids/*w  ww.  j a  v a 2s  .c  o m*/
 * @param o1
 * @param o2
 * @return
 */
public static boolean mayShareRBACInfo(List<String> realJmxAclPids, ObjectName o1, ObjectName o2) {
    if (o1 == null || o2 == null) {
        return false;
    }

    Deque<String> pids1 = new LinkedList<>();
    List<String> pidCandidates1 = iterateDownPids(nameSegments(o1));
    List<String> pidCandidates2 = iterateDownPids(nameSegments(o2));

    for (String pidCandidate1 : pidCandidates1) {
        pids1.add(getGeneralPid(realJmxAclPids, pidCandidate1));
    }
    for (String pidCandidate2 : pidCandidates2) {
        if (pids1.peek() == null || !pids1.pop().equals(getGeneralPid(realJmxAclPids, pidCandidate2))) {
            return false;
        }
    }

    return pids1.size() == 0;
}

From source file:com.cinchapi.concourse.lang.Parser.java

/**
 * Convert a valid and well-formed list of {@link Symbol} objects into a
 * Queue in postfix notation.//from  w w  w  . ja  va2s. co  m
 * <p>
 * NOTE: This method will group non-conjunctive symbols into
 * {@link Expression} objects.
 * </p>
 * 
 * @param symbols
 * @return the symbols in postfix notation
 */
public static Queue<PostfixNotationSymbol> toPostfixNotation(List<Symbol> symbols) {
    Deque<Symbol> stack = new ArrayDeque<Symbol>();
    Queue<PostfixNotationSymbol> queue = new LinkedList<PostfixNotationSymbol>();
    symbols = groupExpressions(symbols);
    for (Symbol symbol : symbols) {
        if (symbol instanceof ConjunctionSymbol) {
            while (!stack.isEmpty()) {
                Symbol top = stack.peek();
                if (symbol == ConjunctionSymbol.OR
                        && (top == ConjunctionSymbol.OR || top == ConjunctionSymbol.AND)) {
                    queue.add((PostfixNotationSymbol) stack.pop());
                } else {
                    break;
                }
            }
            stack.push(symbol);
        } else if (symbol == ParenthesisSymbol.LEFT) {
            stack.push(symbol);
        } else if (symbol == ParenthesisSymbol.RIGHT) {
            boolean foundLeftParen = false;
            while (!stack.isEmpty()) {
                Symbol top = stack.peek();
                if (top == ParenthesisSymbol.LEFT) {
                    foundLeftParen = true;
                    break;
                } else {
                    queue.add((PostfixNotationSymbol) stack.pop());
                }
            }
            if (!foundLeftParen) {
                throw new SyntaxException(
                        MessageFormat.format("Syntax error in {0}: Mismatched parenthesis", symbols));
            } else {
                stack.pop();
            }
        } else {
            queue.add((PostfixNotationSymbol) symbol);
        }
    }
    while (!stack.isEmpty()) {
        Symbol top = stack.peek();
        if (top instanceof ParenthesisSymbol) {
            throw new SyntaxException(
                    MessageFormat.format("Syntax error in {0}: Mismatched parenthesis", symbols));
        } else {
            queue.add((PostfixNotationSymbol) stack.pop());
        }
    }
    return queue;
}