Example usage for java.util Deque peek

List of usage examples for java.util Deque peek

Introduction

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

Prototype

E peek();

Source Link

Document

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

Usage

From source file:org.apache.pig.builtin.Utf8StorageConverter.java

private Tuple consumeTuple(PushbackInputStream in, ResourceFieldSchema fieldSchema) throws IOException {
    if (fieldSchema == null) {
        throw new IOException("Schema is null");
    }//from   w  w w .  j a  v  a  2  s. c om
    int buf;
    ByteArrayOutputStream mOut;

    while ((buf = in.read()) != '(' || buf == '}') {
        if (buf == -1) {
            throw new IOException("Unexpect end of tuple");
        }
        if (buf == '}') {
            in.unread(buf);
            return null;
        }
    }
    Tuple t = TupleFactory.getInstance().newTuple();
    if (fieldSchema.getSchema() != null && fieldSchema.getSchema().getFields().length != 0) {
        ResourceFieldSchema[] fss = fieldSchema.getSchema().getFields();
        // Interpret item inside tuple one by one based on the inner schema
        for (int i = 0; i < fss.length; i++) {
            Object field;
            ResourceFieldSchema fs = fss[i];
            int delimit = ',';
            if (i == fss.length - 1)
                delimit = ')';

            if (DataType.isComplex(fs.getType())) {
                field = consumeComplexType(in, fs);
                while ((buf = in.read()) != delimit) {
                    if (buf == -1) {
                        throw new IOException("Unexpect end of tuple");
                    }
                }
            } else {
                mOut = new ByteArrayOutputStream(BUFFER_SIZE);
                while ((buf = in.read()) != delimit) {
                    if (buf == -1) {
                        throw new IOException("Unexpect end of tuple");
                    }
                    if (buf == delimit)
                        break;
                    mOut.write(buf);
                }
                field = parseSimpleType(mOut.toByteArray(), fs);
            }
            t.append(field);
        }
    } else {
        // No inner schema, treat everything inside tuple as bytearray
        Deque<Character> level = new LinkedList<Character>(); // keep track of nested tuple/bag/map. We do not interpret, save them as bytearray
        mOut = new ByteArrayOutputStream(BUFFER_SIZE);
        while (true) {
            buf = in.read();
            if (buf == -1) {
                throw new IOException("Unexpect end of tuple");
            }
            if (buf == '[' || buf == '{' || buf == '(') {
                level.push((char) buf);
                mOut.write(buf);
            } else if (buf == ')' && level.isEmpty()) // End of tuple
            {
                DataByteArray value = new DataByteArray(mOut.toByteArray());
                t.append(value);
                break;
            } else if (buf == ',' && level.isEmpty()) {
                DataByteArray value = new DataByteArray(mOut.toByteArray());
                t.append(value);
                mOut.reset();
            } else if (buf == ']' || buf == '}' || buf == ')') {
                if (level.peek() == findStartChar((char) buf))
                    level.pop();
                else
                    throw new IOException("Malformed tuple");
                mOut.write(buf);
            } else
                mOut.write(buf);
        }
    }
    return t;
}

From source file:org.apache.pig.builtin.Utf8StorageConverter.java

private Map<String, Object> consumeMap(PushbackInputStream in, ResourceFieldSchema fieldSchema)
        throws IOException {
    int buf;//w  ww. j a  va2s  .  c o m
    boolean emptyMap = true;

    while ((buf = in.read()) != '[') {
        if (buf == -1) {
            throw new IOException("Unexpect end of map");
        }
    }
    HashMap<String, Object> m = new HashMap<String, Object>();
    ByteArrayOutputStream mOut = new ByteArrayOutputStream(BUFFER_SIZE);
    while (true) {
        // Read key (assume key can not contains special character such as #, (, [, {, }, ], )
        while ((buf = in.read()) != '#') {
            // end of map
            if (emptyMap && buf == ']') {
                return m;
            }
            if (buf == -1) {
                throw new IOException("Unexpect end of map");
            }
            emptyMap = false;
            mOut.write(buf);
        }
        String key = bytesToCharArray(mOut.toByteArray());
        if (key.length() == 0)
            throw new IOException("Map key can not be null");

        // Read value
        mOut.reset();
        Deque<Character> level = new LinkedList<Character>(); // keep track of nested tuple/bag/map. We do not interpret, save them as bytearray
        while (true) {
            buf = in.read();
            if (buf == -1) {
                throw new IOException("Unexpect end of map");
            }
            if (buf == '[' || buf == '{' || buf == '(') {
                level.push((char) buf);
            } else if (buf == ']' && level.isEmpty()) // End of map
                break;
            else if (buf == ']' || buf == '}' || buf == ')') {
                if (level.isEmpty())
                    throw new IOException("Malformed map");

                if (level.peek() == findStartChar((char) buf))
                    level.pop();
            } else if (buf == ',' && level.isEmpty()) { // Current map item complete
                break;
            }
            mOut.write(buf);
        }
        Object value = null;
        if (fieldSchema != null && fieldSchema.getSchema() != null && mOut.size() > 0) {
            value = bytesToObject(mOut.toByteArray(), fieldSchema.getSchema().getFields()[0]);
        } else if (mOut.size() > 0) { // untyped map
            value = new DataByteArray(mOut.toByteArray());
        }
        m.put(key, value);
        mOut.reset();
        if (buf == ']')
            break;
    }
    return m;
}

From source file:org.jahia.utils.maven.plugin.support.MavenAetherHelperUtils.java

static String getTrailPadding(Deque<String> dependencyTrail) {
    StringBuffer padding = new StringBuffer();
    for (int i = 0; i < dependencyTrail.size(); i++) {
        padding.append("  ");
    }//from  w  w w. j a  va  2 s  . com
    padding.append(dependencyTrail.peek());
    padding.append(" ");
    return padding.toString();
}

From source file:org.nuxeo.ecm.platform.routing.core.impl.GraphRouteImpl.java

/**
 * Finds which transitions are re-looping (feedback arc set).
 *//*from   www.  j a  v  a2s . c  o m*/
protected void computeLoopTransitions(String startNodeId) throws DocumentRouteException {
    if (startNodeId == null) {
        // incomplete graph
        return;
    }
    /*
     * Depth-first search. In the todo stack, each element records a list of the siblings left to visit at that
     * depth. After visiting the last sibling, we go back to the parent and at this point mark it as visited in
     * post-traversal order.
     */
    List<String> postOrder = new LinkedList<String>();
    Deque<Deque<String>> stack = new LinkedList<Deque<String>>();
    Deque<String> first = new LinkedList<String>();
    first.add(startNodeId);
    stack.push(first);
    Set<String> done = new HashSet<String>();
    for (;;) {
        // find next sibling
        String nodeId = stack.peek().peek();
        if (nodeId == null) {
            // last sibling done
            // go back up one level and mark post-traversal order
            stack.pop(); // pop empty children
            if (stack.isEmpty()) {
                // we are done
                break;
            }
            nodeId = stack.peek().pop(); // pop parent
            postOrder.add(nodeId); // mark post-traversal order
        } else if (done.add(nodeId)) {
            // traverse the next sibling
            Deque<String> children = new LinkedList<String>();
            for (Transition t : getNode(nodeId).getOutputTransitions()) {
                children.add(t.target);
            }
            // add children to stack and recurse
            stack.push(children);
        } else {
            // already traversed
            stack.peek().pop(); // skip it
        }
    }

    // reverse the post-order to find the topological ordering
    Collections.reverse(postOrder);
    Map<String, Integer> ordering = new HashMap<String, Integer>();
    int i = 1;
    for (String nodeId : postOrder) {
        ordering.put(nodeId, Integer.valueOf(i++));
    }

    // walk the graph and all transitions again
    // and mark as looping the transitions pointing to a node
    // with a smaller order that the source
    done.clear();
    Deque<String> todo = new LinkedList<String>();
    todo.add(startNodeId);
    while (!todo.isEmpty()) {
        String nodeId = todo.pop();
        if (done.add(nodeId)) {
            int source = ordering.get(nodeId).intValue();
            for (Transition t : getNode(nodeId).getOutputTransitions()) {
                todo.push(t.target);
                // compare orders to detected feeback arcs
                int target = ordering.get(t.target).intValue();
                if (target <= source) {
                    t.loop = true;
                }
            }
        }
    }
}