58 * return 1; 59 * } 60 * {@literal @}Override 61 * public Integer reduce(Integer r1, Integer r2) { 62 * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 63 * } 64 * } 65 * </pre> 66 * 67 * @param <R> the return type of this visitor's methods. Use {@link 68 * Void} for visitors that do not need to return results. 69 * @param <P> the type of the additional parameter to this visitor's 70 * methods. Use {@code Void} for visitors that do not need an 71 * additional parameter. 72 * 73 * @author Peter von der Ahé 74 * @author Jonathan Gibbons 75 * @since 1.6 76 */ 77 public class TreeScanner<R,P> implements TreeVisitor<R,P> { 78 79 /** 80 * Scans a single node. 81 * @param tree the node to be scanned 82 * @param p a parameter value passed to the visit method 83 * @return the result value from the visit method 84 */ 85 public R scan(Tree tree, P p) { 86 return (tree == null) ? null : tree.accept(this, p); 87 } 88 89 private R scanAndReduce(Tree node, P p, R r) { 90 return reduce(scan(node, p), r); 91 } 92 93 /** 94 * Scans a sequence of nodes. 95 * @param nodes the nodes to be scanned 96 * @param p a parameter value to be passed to the visit method for each node 97 * @return the combined return value from the visit methods. | 58 * return 1; 59 * } 60 * {@literal @}Override 61 * public Integer reduce(Integer r1, Integer r2) { 62 * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 63 * } 64 * } 65 * </pre> 66 * 67 * @param <R> the return type of this visitor's methods. Use {@link 68 * Void} for visitors that do not need to return results. 69 * @param <P> the type of the additional parameter to this visitor's 70 * methods. Use {@code Void} for visitors that do not need an 71 * additional parameter. 72 * 73 * @author Peter von der Ahé 74 * @author Jonathan Gibbons 75 * @since 1.6 76 */ 77 public class TreeScanner<R,P> implements TreeVisitor<R,P> { 78 /** 79 * Constructor for subclasses to call. 80 */ 81 public TreeScanner() {} 82 83 /** 84 * Scans a single node. 85 * @param tree the node to be scanned 86 * @param p a parameter value passed to the visit method 87 * @return the result value from the visit method 88 */ 89 public R scan(Tree tree, P p) { 90 return (tree == null) ? null : tree.accept(this, p); 91 } 92 93 private R scanAndReduce(Tree node, P p, R r) { 94 return reduce(scan(node, p), r); 95 } 96 97 /** 98 * Scans a sequence of nodes. 99 * @param nodes the nodes to be scanned 100 * @param p a parameter value to be passed to the visit method for each node 101 * @return the combined return value from the visit methods. |