Java Code Examples for java.util.Deque#iterator()

The following examples show how to use java.util.Deque#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HT_IO.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Read a node from a file on disk
 *
 * @param queue
 *            NON-EMPTY queue of node sequence numbers to read from
 * @return any node from the queue, returning cached nodes first, else resorting
 *         to reading on disk.
 * @throws ClosedChannelException
 *             Usually happens because the file was closed while we were
 *             reading. Instead of using a big reader-writer lock, we'll just
 *             catch this exception.
 */
public @NonNull HTNode readNode(Deque<Integer> queue) throws ClosedChannelException {
    /*
     * Use an iterator in order to remove efficiently from the queue position
     */
    Iterator<Integer> iterator = queue.iterator();
    while (iterator.hasNext()) {
        Integer seqNumber = iterator.next();
        CacheKey key = new CacheKey(this, seqNumber);
        HTNode node = NODE_CACHE.getIfPresent(key);
        if (node != null) {
            iterator.remove();
            return node;
        }
    }
    // need to go to disk
    return readNode(queue.pop());
}
 
Example 2
Source File: Rule.java    From spork with Apache License 2.0 6 votes vote down vote up
protected boolean check(List<Operator> planOps) throws FrontendException {
    List<Operator> patternOps = pattern.getSinks();
    if (planOps.size() != patternOps.size()) {
        return false;
    }
    
    for(int i=0; i<planOps.size(); i++) {
        Deque<Operator> s = new LinkedList<Operator>();
        if (!check(planOps.get(i), patternOps.get(i), s)) {
            return false;
        }
        Iterator<Operator> iter = s.iterator();
        while(iter.hasNext()) {
            add(iter.next());
        }
    }
    
    if (size() == pattern.size()) {
        return true;
    }
    
    return false;
}
 
Example 3
Source File: ProtoTruthMessageDifferencer.java    From curiostack with MIT License 5 votes vote down vote up
@NullableDecl
private RepeatedField.PairResult findMatchingPairResult(
    Deque<Integer> actualIndices,
    List<?> actualValues,
    int expectedIndex,
    Object expectedValue,
    boolean excludeNonRecursive,
    FieldDescriptor fieldDescriptor,
    FluentEqualityConfig config) {
  Iterator<Integer> actualIndexIter = actualIndices.iterator();
  while (actualIndexIter.hasNext()) {
    int actualIndex = actualIndexIter.next();
    RepeatedField.PairResult pairResult =
        compareRepeatedFieldElementPair(
            actualValues.get(actualIndex),
            expectedValue,
            excludeNonRecursive,
            fieldDescriptor,
            actualIndex,
            expectedIndex,
            config);
    if (pairResult.isMatched()) {
      actualIndexIter.remove();
      return pairResult;
    }
  }

  return null;
}
 
Example 4
Source File: Doc.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
* Determines whether the documents in {@code agenda} fit in {@code width} columns, up to the next
* newline.
*/
private static boolean agendaFits(Deque<Agendum> agenda, int width) {
  FitsState state = new FitsState(width);
  Iterator<Agendum> iterator = agenda.iterator();
  while (iterator.hasNext() && !state.done && state.width >= 0) {
    Agendum agendum = iterator.next();
    agendum.doc.fits(agendum.mode, state);
  }
  return state.width >= 0;
}
 
Example 5
Source File: ASTPath.java    From annotation-tools with MIT License 5 votes vote down vote up
@Override
public int compareTo(ASTPath o) {
  // hacky fix: remove {Method,Class}.body for comparison
  ImmutableStack<ASTEntry> s0 = canonical(this);
  ImmutableStack<ASTEntry> s1 = canonical(o);
  Deque<ASTEntry> d0 = new LinkedList<ASTEntry>();
  Deque<ASTEntry> d1 = new LinkedList<ASTEntry>();
  int c = 0;
  while (!s0.isEmpty()) {
    d0.push(s0.peek());
    s0 = s0.pop();
  }
  while (!s1.isEmpty()) {
    d1.push(s1.peek());
    s1 = s1.pop();
  }
  int n0 = d0.size();
  int n1 = d1.size();
  c = Integer.compare(n0, n1);
  if (c == 0) {
    Iterator<ASTEntry> i0 = d0.iterator();
    Iterator<ASTEntry> i1 = d1.iterator();
    while (i0.hasNext()) {
      c = i0.next().compareTo(i1.next());
      if (c != 0) { return c; }
    }
  }
  return c;
}
 
Example 6
Source File: Path.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
private String assembleSegments(final Deque<String> segments, final String file) {
	final StringBuilder builder=new StringBuilder();
	for(final Iterator<String> it=segments.iterator();it.hasNext();) {
		final String segment=it.next();
		builder.append(segment);
		if(it.hasNext() || !isDotSegment(segment) || file!=null) {
			builder.append(SLASH);
		}
	}
	if(file!=null) {
		builder.append(file);
	}
	return builder.toString();
}
 
Example 7
Source File: BaseGenericObjectPool.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Create an EvictionIterator for the provided idle instance deque.
 * @param idleObjects underlying deque
 */
EvictionIterator(final Deque<PooledObject<T>> idleObjects) {
    this.idleObjects = idleObjects;

    if (getLifo()) {
        idleObjectIterator = idleObjects.descendingIterator();
    } else {
        idleObjectIterator = idleObjects.iterator();
    }
}
 
Example 8
Source File: ChorusLine.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void run(Deque<Integer> deq) {
    while (deq.size() > 1) {
        Iterator<Integer> it = deq.iterator();
        it.next(); it.remove();
        it = deq.descendingIterator();
        it.next(); it.remove();
    }
    System.out.println(deq);
}
 
Example 9
Source File: Structurizer.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static void removeCommentsAndWhitespace(Deque<Term> terms) {
  Iterator<Term> it = terms.iterator();
  while (it.hasNext()) {
    Term n = it.next();
    if (n.isComment() || n.isWhitespace()) {
      it.remove();
    }
  }
}
 
Example 10
Source File: ASTWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : c.getDeclaredFields()) {
            try {
                f.setAccessible(true);
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}
 
Example 11
Source File: ASTWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : accessibleFields.get(c)) {
            try {
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}
 
Example 12
Source File: ComponentUtils.java    From litho with Apache License 2.0 4 votes vote down vote up
/**
 * @return String representation of the tree with the root at the passed node For example:
 *     PlaygroundComponent |-Text[trans.key="text_transition_key";] |-Row | +-Text
 *     +-Text[manual.key="text2";]
 */
static String treeToString(@Nullable InternalNode root) {
  if (root == null) {
    return "null";
  }

  final StringBuilder builder = new StringBuilder();
  final Deque<InternalNode> stack = new LinkedList<>();
  stack.addLast(null);
  stack.addLast(root);
  int level = 0;
  while (!stack.isEmpty()) {
    final InternalNode node = stack.removeLast();
    if (node == null) {
      level--;
      continue;
    }

    final Component component = node.getTailComponent();
    if (component == null) {
      continue;
    }

    if (node != root) {
      builder.append('\n');
      boolean isLast;
      final Iterator<InternalNode> iterator = stack.iterator();
      iterator.next();
      iterator.next();
      for (int index = 0; index < level - 1; index++) {
        isLast = iterator.next() == null;
        if (!isLast) {
          while (iterator.next() != null) ;
        }
        builder.append(isLast ? ' ' : "\u2502").append(' ');
      }
      builder.append(stack.getLast() == null ? "\u2514\u2574" : "\u251C\u2574");
    }

    builder.append(component.getSimpleName());

    if (component.hasManualKey() || node.hasTransitionKey() || node.getTestKey() != null) {
      builder.append('[');
      if (component.hasManualKey()) {
        builder.append("manual.key=\"").append(component.getKey()).append("\";");
      }
      if (node.hasTransitionKey()) {
        builder.append("trans.key=\"").append(node.getTransitionKey()).append("\";");
      }
      if (node.getTestKey() != null) {
        builder.append("test.key=\"").append(node.getTestKey()).append("\";");
      }
      builder.append(']');
    }

    if (node.getChildCount() == 0) {
      continue;
    }

    stack.addLast(null);
    for (int index = node.getChildCount() - 1; index >= 0; index--) {
      stack.addLast(node.getChildAt(index));
    }
    level++;
  }

  return builder.toString();
}
 
Example 13
Source File: ASTWriter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : c.getDeclaredFields()) {
            try {
                f.setAccessible(true);
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}
 
Example 14
Source File: ASTWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : c.getDeclaredFields()) {
            try {
                f.setAccessible(true);
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}
 
Example 15
Source File: DefaultHandlerContext.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
public DefaultHandlerContext(Deque<Handler> handlers) {
    this.handlers = handlers;
    this.cursor = handlers.iterator();
}
 
Example 16
Source File: FineGrainedWatermarkTracker.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * A helper method to garbage collect acknowledged watermarks
 * @return number of elements collected
 */
@VisibleForTesting
synchronized int sweep() {
  long startTime = System.nanoTime();
  int swept = 0;
  for (Map.Entry<String, Deque<AcknowledgableWatermark>> entry : _watermarksMap.entrySet()) {
    Deque<AcknowledgableWatermark> watermarks = entry.getValue();
    /**
     * Keep popping acked elements from the front as long as their next element is also acked.
     * So: Acked_A -> Acked_B -> Not-Acked_C -> ...  becomes
     *     Acked_B -> Not-Acked_C -> ...
     *
     * We keep the acked element around because that represents the highest contiguous acked watermark.
     */
    boolean continueIteration = true;
    while (continueIteration) {
      Iterator<AcknowledgableWatermark> iter = watermarks.iterator();
      if (!iter.hasNext()) {  // null
        continueIteration = false;
        continue;
      }

      AcknowledgableWatermark first = iter.next();
      if (first.isAcked()) {
        if (!iter.hasNext()) { // Acked_A -> null
          continueIteration = false;
          continue;
        }
        AcknowledgableWatermark second = iter.next();
        if ((second != null) && second.isAcked()) { // Acked_A -> Acked_B -> ...
          watermarks.pop();
          swept++;
        } else { // Acked_A -> Not_Acked_B
          continueIteration = false;
        }
      } else { // Not_Acked_A -> ..
        continueIteration = false;
      }
    }
  }
  long duration = (System.nanoTime() - startTime)/ MILLIS_TO_NANOS;
  log.debug("Swept {} watermarks in {} millis", swept, duration);
  _watermarksSwept.mark(swept);
  return swept;
}
 
Example 17
Source File: ASTWriter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : c.getDeclaredFields()) {
            try {
                f.setAccessible(true);
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}
 
Example 18
Source File: ASTWriter.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : c.getDeclaredFields()) {
            try {
                f.setAccessible(true);
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}
 
Example 19
Source File: DefaultHandlerContext.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
public DefaultHandlerContext(Deque<Handler> handlers) {
    this.handlers = handlers;
    this.cursor = handlers.iterator();
}
 
Example 20
Source File: ASTWriter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void enqueueChildren(final Node node, final Class<?> nodeClass, final List<Field> children) {
    final Deque<Class<?>> stack = new ArrayDeque<>();

    /**
     * Here is some ugliness that can be overcome by proper ChildNode annotations
     * with proper orders. Right now we basically sort all classes up to Node
     * with super class first, as this often is the natural order, e.g. base
     * before index for an IndexNode.
     *
     * Also there are special cases as this is not true for UnaryNodes(lhs) and
     * BinaryNodes extends UnaryNode (with lhs), and TernaryNodes.
     *
     * TODO - generalize traversal with an order built on annotations and this
     * will go away.
     */
    Class<?> clazz = nodeClass;
    do {
        stack.push(clazz);
        clazz = clazz.getSuperclass();
    } while (clazz != null);

    if (node instanceof TernaryNode) {
        // HACK juggle "third"
        stack.push(stack.removeLast());
    }
    // HACK change operator order for BinaryNodes to get lhs first.
    final Iterator<Class<?>> iter = node instanceof BinaryNode ? stack.descendingIterator() : stack.iterator();

    while (iter.hasNext()) {
        final Class<?> c = iter.next();
        for (final Field f : c.getDeclaredFields()) {
            try {
                f.setAccessible(true);
                final Object child = f.get(node);
                if (child == null) {
                    continue;
                }

                if (child instanceof Node) {
                    children.add(f);
                } else if (child instanceof Collection) {
                    if (!((Collection<?>)child).isEmpty()) {
                        children.add(f);
                    }
                }
            } catch (final IllegalArgumentException | IllegalAccessException e) {
                return;
            }
        }
    }
}