### Question:
Optimizer { static Matcher mergeNext(Matcher matcher) { if (matcher instanceof SeqMatcher) { List<Matcher> matchers = matcher.<SeqMatcher>as().matchers(); List<Matcher> ms = new ArrayList<>(); for (int i = 0; i < matchers.size(); ++i) { Matcher m = matchers.get(i); if (m instanceof GreedyMatcher) { List<Matcher> after = matchers.subList(i + 1, matchers.size()); ms.add(m.<GreedyMatcher>as().mergeNext(SeqMatcher.create(after))); break; } else { ms.add(m); } } return SeqMatcher.create(ms); } return matcher; } private  Optimizer();   }### Answer:
@Test public void zeroOrMoreMergeNext() { Matcher input = SeqMatcher.create( AnyMatcher.INSTANCE, new ZeroOrMoreMatcher(AnyMatcher.INSTANCE, AnyMatcher.INSTANCE), AnyMatcher.INSTANCE ); Matcher expected = SeqMatcher.create( AnyMatcher.INSTANCE, new ZeroOrMoreMatcher( AnyMatcher.INSTANCE, SeqMatcher.create(AnyMatcher.INSTANCE, AnyMatcher.INSTANCE)) ); Assertions.assertEquals(expected, Optimizer.mergeNext(input)); }
@Test public void orMergeNext() { Matcher input = SeqMatcher.create( AnyMatcher.INSTANCE, OrMatcher.create(AnyMatcher.INSTANCE, AnyMatcher.INSTANCE), AnyMatcher.INSTANCE ); Matcher expected = SeqMatcher.create( AnyMatcher.INSTANCE, (new OrMatcher(AnyMatcher.INSTANCE, AnyMatcher.INSTANCE)).mergeNext(AnyMatcher.INSTANCE) ); Assertions.assertEquals(expected, Optimizer.mergeNext(input)); }