/** * Provides a collection of all the nodes in the tree * using a depth-first traversal. * * @param preorder if false, a postorder depth-first traversal will be performed * @return the list of (depth-first) ordered nodes * @since 2.5.0 */ public List depthFirst(boolean preorder) { List answer = new NodeList(); if (preorder) answer.add(this); answer.addAll(depthFirstRest(preorder)); if (!preorder) answer.add(this); return answer; }
private List depthFirstRest(boolean preorder) { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.depthFirstRest(preorder); if (preorder) answer.add(childNode); if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String))) answer.addAll(children); if (!preorder) answer.add(childNode); } else if (child instanceof String) { answer.add(child); } } return answer; }
/** * Provides a collection of all the nodes in the tree * using a depth-first traversal. * A boolean 'preorder' options is supported. * * @param options map containing options * @param c the closure to run for each node (a one or two parameter can be used; if one parameter is given the * closure will be passed the node, for a two param closure the second parameter will be the level). * @since 2.5.0 */ public void depthFirst(Map<String, Object> options, Closure c) { boolean preorder = Boolean.valueOf(options.get("preorder").toString()); if (preorder) callClosureForNode(c, this, 1); depthFirstRest(preorder, 2, c); if (!preorder) callClosureForNode(c, this, 1); }
private void depthFirstRest(boolean preorder, int level, Closure c) { for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; if (preorder) callClosureForNode(c, childNode, level); childNode.depthFirstRest(preorder, level + 1, c); if (!preorder) callClosureForNode(c, childNode, level); } } }
/** * Provide a collection of all the nodes in the tree * using a depth first traversal. * * @return the list of (depth-first) ordered nodes */ public List depthFirst() { List answer = new NodeList(); answer.add(this); answer.addAll(depthFirstRest()); return answer; }
/** * Provide a collection of all the nodes in the tree * using a depth first traversal. * * @return the list of (depth-first) ordered nodes */ public List depthFirst() { List answer = new NodeList(); answer.add(this); answer.addAll(depthFirstRest()); return answer; }
/** * Provide a collection of all the nodes in the tree * using a depth first traversal. * * @return the list of (depth-first) ordered nodes */ public List depthFirst() { List answer = new NodeList(); answer.add(this); answer.addAll(depthFirstRest()); return answer; }
/** * Provides a collection of all the nodes in the tree * using a depth first traversal. * * @return the list of (depth-first) ordered nodes */ public List depthFirst() { List answer = new NodeList(); answer.add(this); answer.addAll(depthFirstRest()); return answer; }
private List depthFirstRest() { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext();) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.depthFirstRest(); answer.add(childNode); answer.addAll(children); } } return answer; }
private List depthFirstRest() { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext();) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.depthFirstRest(); answer.add(childNode); answer.addAll(children); } } return answer; }
private List depthFirstRest() { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext();) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.depthFirstRest(); answer.add(childNode); answer.addAll(children); } } return answer; }
private List depthFirstRest() { List answer = new NodeList(); for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { Object child = iter.next(); if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.depthFirstRest(); answer.add(childNode); if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String))) answer.addAll(children); } else if (child instanceof String) { answer.add(child); } } return answer; }