Search in sources :

Example 6 with Stack

use of java.util.Stack in project groovy by apache.

the class GPathResult method depthFirst.

/**
     * Provides an Iterator over all the nodes of this GPathResult using a depth-first traversal.
     *
     * @return the <code>Iterator</code> of (depth-first) ordered GPathResults
     */
public Iterator depthFirst() {
    return new Iterator() {

        private final List list = new LinkedList();

        private final Stack stack = new Stack();

        private Iterator iter = iterator();

        private GPathResult next = getNextByDepth();

        public boolean hasNext() {
            return this.next != null;
        }

        public Object next() {
            try {
                return this.next;
            } finally {
                this.next = getNextByDepth();
            }
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        private GPathResult getNextByDepth() {
            while (this.iter.hasNext()) {
                final GPathResult node = (GPathResult) this.iter.next();
                this.list.add(node);
                this.stack.push(this.iter);
                this.iter = node.children().iterator();
            }
            if (this.list.isEmpty()) {
                return null;
            } else {
                GPathResult result = (GPathResult) this.list.get(0);
                this.list.remove(0);
                this.iter = (Iterator) this.stack.pop();
                return result;
            }
        }
    };
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 7 with Stack

use of java.util.Stack in project hadoop by apache.

the class NodeInfo method parseConf.

private static List<NodeInfo> parseConf(InputStream in) throws XMLStreamException {
    QName configuration = new QName("configuration");
    QName property = new QName("property");
    List<NodeInfo> nodes = new ArrayList<NodeInfo>();
    Stack<NodeInfo> parsed = new Stack<NodeInfo>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(in);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement currentElement = event.asStartElement();
            NodeInfo currentNode = new NodeInfo(currentElement);
            if (parsed.isEmpty()) {
                if (!currentElement.getName().equals(configuration)) {
                    return null;
                }
            } else {
                NodeInfo parentNode = parsed.peek();
                QName parentName = parentNode.getStartElement().getName();
                if (parentName.equals(configuration) && currentNode.getStartElement().getName().equals(property)) {
                    @SuppressWarnings("unchecked") Iterator<Attribute> it = currentElement.getAttributes();
                    while (it.hasNext()) {
                        currentNode.addAttribute(it.next());
                    }
                } else if (parentName.equals(property)) {
                    parentNode.addElement(currentElement);
                }
            }
            parsed.push(currentNode);
        } else if (event.isEndElement()) {
            NodeInfo node = parsed.pop();
            if (parsed.size() == 1) {
                nodes.add(node);
            }
        } else if (event.isCharacters()) {
            if (2 < parsed.size()) {
                NodeInfo parentNode = parsed.pop();
                StartElement parentElement = parentNode.getStartElement();
                NodeInfo grandparentNode = parsed.peek();
                if (grandparentNode.getElement(parentElement) == null) {
                    grandparentNode.setElement(parentElement, event.asCharacters());
                }
                parsed.push(parentNode);
            }
        }
    }
    return nodes;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Stack(java.util.Stack) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 8 with Stack

use of java.util.Stack in project groovy by apache.

the class Node method breadthFirstRest.

private void breadthFirstRest(boolean preorder, int level, Closure c) {
    Stack<Tuple2<Object, Integer>> stack = new Stack<Tuple2<Object, Integer>>();
    List nextLevelChildren = preorder ? getDirectChildren() : DefaultGroovyMethods.reverse(getDirectChildren());
    while (!nextLevelChildren.isEmpty()) {
        List working = new NodeList(nextLevelChildren);
        nextLevelChildren = new NodeList();
        for (Object child : working) {
            if (preorder) {
                callClosureForNode(c, child, level);
            } else {
                stack.push(new Tuple2<Object, Integer>(child, level));
            }
            if (child instanceof Node) {
                Node childNode = (Node) child;
                List children = childNode.getDirectChildren();
                if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String)))
                    nextLevelChildren.addAll(preorder ? children : DefaultGroovyMethods.reverse(children));
            }
        }
        level++;
    }
    while (!stack.isEmpty()) {
        Tuple2<Object, Integer> next = stack.pop();
        callClosureForNode(c, next.getFirst(), next.getSecond());
    }
}
Also used : Tuple2(groovy.lang.Tuple2) ArrayList(java.util.ArrayList) List(java.util.List) Stack(java.util.Stack)

Example 9 with Stack

use of java.util.Stack in project hadoop by apache.

the class RollingWindowManager method snapshot.

/**
   * Take a snapshot of current top users in the past period.
   *
   * @param time the current time
   * @return a TopWindow describing the top users for each metric in the 
   * window.
   */
public TopWindow snapshot(long time) {
    TopWindow window = new TopWindow(windowLenMs);
    Set<String> metricNames = metricMap.keySet();
    LOG.debug("iterating in reported metrics, size={} values={}", metricNames.size(), metricNames);
    for (Map.Entry<String, RollingWindowMap> entry : metricMap.entrySet()) {
        String metricName = entry.getKey();
        RollingWindowMap rollingWindows = entry.getValue();
        TopN topN = getTopUsersForMetric(time, metricName, rollingWindows);
        final int size = topN.size();
        if (size == 0) {
            continue;
        }
        Op op = new Op(metricName, topN.getTotal());
        window.addOp(op);
        // Reverse the users from the TopUsers using a stack, 
        // since we'd like them sorted in descending rather than ascending order
        Stack<NameValuePair> reverse = new Stack<NameValuePair>();
        for (int i = 0; i < size; i++) {
            reverse.push(topN.poll());
        }
        for (int i = 0; i < size; i++) {
            NameValuePair userEntry = reverse.pop();
            User user = new User(userEntry.getName(), userEntry.getValue());
            op.addUser(user);
        }
    }
    return window;
}
Also used : NameValuePair(org.apache.hadoop.metrics2.util.Metrics2Util.NameValuePair) Stack(java.util.Stack) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) TopN(org.apache.hadoop.metrics2.util.Metrics2Util.TopN)

Example 10 with Stack

use of java.util.Stack in project hbase by apache.

the class ParseFilter method parseFilterString.

/**
   * Parses the filterString and constructs a filter using it
   * <p>
   * @param filterStringAsByteArray filter string given by the user
   * @return filter object we constructed
   */
public Filter parseFilterString(byte[] filterStringAsByteArray) throws CharacterCodingException {
    // stack for the operators and parenthesis
    Stack<ByteBuffer> operatorStack = new Stack<>();
    // stack for the filter objects
    Stack<Filter> filterStack = new Stack<>();
    Filter filter = null;
    for (int i = 0; i < filterStringAsByteArray.length; i++) {
        if (filterStringAsByteArray[i] == ParseConstants.LPAREN) {
            // LPAREN found
            operatorStack.push(ParseConstants.LPAREN_BUFFER);
        } else if (filterStringAsByteArray[i] == ParseConstants.WHITESPACE || filterStringAsByteArray[i] == ParseConstants.TAB) {
            // WHITESPACE or TAB found
            continue;
        } else if (checkForOr(filterStringAsByteArray, i)) {
            // OR found
            i += ParseConstants.OR_ARRAY.length - 1;
            reduce(operatorStack, filterStack, ParseConstants.OR_BUFFER);
            operatorStack.push(ParseConstants.OR_BUFFER);
        } else if (checkForAnd(filterStringAsByteArray, i)) {
            // AND found
            i += ParseConstants.AND_ARRAY.length - 1;
            reduce(operatorStack, filterStack, ParseConstants.AND_BUFFER);
            operatorStack.push(ParseConstants.AND_BUFFER);
        } else if (checkForSkip(filterStringAsByteArray, i)) {
            // SKIP found
            i += ParseConstants.SKIP_ARRAY.length - 1;
            reduce(operatorStack, filterStack, ParseConstants.SKIP_BUFFER);
            operatorStack.push(ParseConstants.SKIP_BUFFER);
        } else if (checkForWhile(filterStringAsByteArray, i)) {
            // WHILE found
            i += ParseConstants.WHILE_ARRAY.length - 1;
            reduce(operatorStack, filterStack, ParseConstants.WHILE_BUFFER);
            operatorStack.push(ParseConstants.WHILE_BUFFER);
        } else if (filterStringAsByteArray[i] == ParseConstants.RPAREN) {
            // RPAREN found
            if (operatorStack.empty()) {
                throw new IllegalArgumentException("Mismatched parenthesis");
            }
            ByteBuffer argumentOnTopOfStack = operatorStack.peek();
            if (argumentOnTopOfStack.equals(ParseConstants.LPAREN_BUFFER)) {
                operatorStack.pop();
                continue;
            }
            while (!(argumentOnTopOfStack.equals(ParseConstants.LPAREN_BUFFER))) {
                filterStack.push(popArguments(operatorStack, filterStack));
                if (operatorStack.empty()) {
                    throw new IllegalArgumentException("Mismatched parenthesis");
                }
                argumentOnTopOfStack = operatorStack.pop();
            }
        } else {
            // SimpleFilterExpression found
            byte[] filterSimpleExpression = extractFilterSimpleExpression(filterStringAsByteArray, i);
            i += (filterSimpleExpression.length - 1);
            filter = parseSimpleFilterExpression(filterSimpleExpression);
            filterStack.push(filter);
        }
    }
    // Finished parsing filterString
    while (!operatorStack.empty()) {
        filterStack.push(popArguments(operatorStack, filterStack));
    }
    if (filterStack.empty()) {
        throw new IllegalArgumentException("Incorrect Filter String");
    }
    filter = filterStack.pop();
    if (!filterStack.empty()) {
        throw new IllegalArgumentException("Incorrect Filter String");
    }
    return filter;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Stack(java.util.Stack)

Aggregations

Stack (java.util.Stack)245 HashSet (java.util.HashSet)42 ArrayList (java.util.ArrayList)37 File (java.io.File)23 IOException (java.io.IOException)22 View (android.view.View)18 Test (org.junit.Test)18 ViewGroup (android.view.ViewGroup)14 HashMap (java.util.HashMap)14 Set (java.util.Set)12 LinkedList (java.util.LinkedList)11 List (java.util.List)11 ImageView (android.widget.ImageView)10 Map (java.util.Map)10 Document (org.w3c.dom.Document)10 TestInputHandler (org.apache.maven.plugins.repository.testutil.TestInputHandler)9 PostfixMathCommandI (org.nfunk.jep.function.PostfixMathCommandI)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 NotificationPanelView (com.android.systemui.statusbar.phone.NotificationPanelView)7 TreeSet (java.util.TreeSet)7