public static Matcher < MultiResult > sequence ( final Matcher < ? > ... matchers ) { checkNotEmpty ( matchers ) ; return new Matcher < MultiResult > ( ) { @ Override public MultiResult matches ( String input , boolean isEof ) { int matchCount = 0 ; Result [ ] results = new Result [ matchers . length ] ; Arrays . fill ( results , failure ( input , false ) ) ; int beginIndex = 0 ; for ( int i = 0 ; i < matchers . length ; i ++ ) { Result result = matchers [ i ] . matches ( input . substring ( beginIndex ) , isEof ) ; if ( result . isSuccessful ( ) ) { beginIndex += result . end ( ) ; results [ i ] = result ; if ( ++ matchCount == matchers . length ) { String group = result . group ( ) ; Result finalResult = new SimpleResult ( true , input , input . substring ( 0 , beginIndex - group . length ( ) ) , group , result . canStopMatching ( ) ) ; return new MultiResultImpl ( finalResult , Arrays . asList ( results ) ) ; } } else { break ; } } final List < Result > resultList = Arrays . asList ( results ) ; boolean canStopMatching = MultiResultImpl . canStopMatching ( resultList ) ; return new MultiResultImpl ( failure ( input , canStopMatching ) , resultList ) ; } @ Override public String toString ( ) { return String . format ( "sequence(%s)" , MultiMatcher . matchersToString ( matchers ) ) ; } } ; }
Matches the given matchers one by one . Every successful matches updates the internal buffer . The consequent match operation is performed after the previous match has succeeded .