51 * the last child scanned. 52 * </ul> 53 * 54 * <p>Here is an example to count the number of erroneous nodes in a tree: 55 * <pre> 56 * class CountErrors extends DocTreeScanner<Integer,Void> { 57 * {@literal @}Override 58 * public Integer visitErroneous(ErroneousTree node, Void p) { 59 * return 1; 60 * } 61 * {@literal @}Override 62 * public Integer reduce(Integer r1, Integer r2) { 63 * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 64 * } 65 * } 66 * </pre> 67 * 68 * @since 1.8 69 */ 70 public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> { 71 72 /** 73 * Scans a single node. 74 * @param node the node to be scanned 75 * @param p a parameter value passed to the visit method 76 * @return the result value from the visit method 77 */ 78 public R scan(DocTree node, P p) { 79 return (node == null) ? null : node.accept(this, p); 80 } 81 82 private R scanAndReduce(DocTree node, P p, R r) { 83 return reduce(scan(node, p), r); 84 } 85 86 /** 87 * Scans a sequence of nodes. 88 * @param nodes the nodes to be scanned 89 * @param p a parameter value to be passed to the visit method for each node 90 * @return the combined return value from the visit methods. | 51 * the last child scanned. 52 * </ul> 53 * 54 * <p>Here is an example to count the number of erroneous nodes in a tree: 55 * <pre> 56 * class CountErrors extends DocTreeScanner<Integer,Void> { 57 * {@literal @}Override 58 * public Integer visitErroneous(ErroneousTree node, Void p) { 59 * return 1; 60 * } 61 * {@literal @}Override 62 * public Integer reduce(Integer r1, Integer r2) { 63 * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 64 * } 65 * } 66 * </pre> 67 * 68 * @since 1.8 69 */ 70 public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> { 71 /** 72 * Constructs a {@code DocTreeScanner}. 73 */ 74 public DocTreeScanner() {} 75 76 /** 77 * Scans a single node. 78 * @param node the node to be scanned 79 * @param p a parameter value passed to the visit method 80 * @return the result value from the visit method 81 */ 82 public R scan(DocTree node, P p) { 83 return (node == null) ? null : node.accept(this, p); 84 } 85 86 private R scanAndReduce(DocTree node, P p, R r) { 87 return reduce(scan(node, p), r); 88 } 89 90 /** 91 * Scans a sequence of nodes. 92 * @param nodes the nodes to be scanned 93 * @param p a parameter value to be passed to the visit method for each node 94 * @return the combined return value from the visit methods. |