ConstRewriter implements TreeRewriter<Op<T>> { @Override public int rewrite(final TreeNode<Op<T>> node, final int limit) { requireNonNull(node); int rewritten = 0; int res; Optional<TreeNode<Op<T>>> result; do { result = results(node).findFirst(); res = result.map(this::rewriting).orElse(0); rewritten += res; } while (result.isPresent() && rewritten < limit); return rewritten; } @SuppressWarnings("unchecked") private  ConstRewriter(final Class<? extends T> type); Class<T> type(); @Override int rewrite(final TreeNode<Op<T>> node, final int limit); @Override String toString(); static ConstRewriter<T> ofType(final Class<? extends T> type); static final ConstRewriter<Double> DOUBLE; }
@Test(dataProvider = "expressions") public void rewrite(final String expr, final double value) { final TreeNode<Op<Double>> tree = MathExpr.parse(expr).toTree(); ConstRewriter.ofType(Double.class).rewrite(tree); Assert.assertEquals(tree.value(), Const.of(value)); }