55import org .hamcrest .TypeSafeDiagnosingMatcher ;
66
77import java .util .ArrayList ;
8+ import java .util .Iterator ;
89import java .util .List ;
910
1011/**
@@ -19,32 +20,13 @@ public HasSubsequence(List<Matcher<? super T>> matchers) {
1920 }
2021
2122 @ Override
22- public boolean matchesSafely (Iterable <? extends T > subsequenceToMatch ,
23- Description mismatchDescription )
24- {
25- List <T > subsequenceToMatchList = new ArrayList <>();
26- for (T e : subsequenceToMatch ) {
27- subsequenceToMatchList .add (e );
28- }
23+ public boolean matchesSafely (Iterable <? extends T > actual ,
24+ Description mismatchDescription ) {
25+ final Cursor <T > cursor = new Cursor <>(actual .iterator (), matchers .iterator ());
2926
30- for (int i = 0 ; i <= subsequenceToMatchList .size () - matchers .size (); i ++) {
31- boolean allMatchersMatched = true ;
32- for (int j = 0 ; j < matchers .size (); j ++) {
33- Matcher <? super T > matcher = matchers .get (j );
34- if (!matcher .matches (subsequenceToMatchList .get (i + j ))) {
35- allMatchersMatched = false ;
36- break ;
37- }
38- }
39- if (allMatchersMatched ) {
40- return true ;
41- }
42- }
43-
44- mismatchDescription
45- .appendText ("could not find subsequence in " )
46- .appendValueList ("[" , ", " , "]" , subsequenceToMatch );
47- return false ;
27+ final boolean matched = cursor .foundFirst () && cursor .foundRemainder ();
28+ if (!matched ) cursor .describeNotFound (mismatchDescription );
29+ return matched ;
4830 }
4931
5032 @ Override
@@ -53,4 +35,60 @@ public void describeTo(Description description) {
5335 .appendText ("iterable contains subsequence matching " )
5436 .appendList ("[" , ", " , "]" , matchers );
5537 }
38+
39+ static private class Cursor <T > {
40+ private final List <T > viewedItems = new ArrayList <>();
41+ private Matcher <? super T > currentMatcher ;
42+ private final Iterator <? extends T > items ;
43+ private final Iterator <Matcher <? super T >> remainingMatchers ;
44+
45+ public Cursor (Iterator <? extends T > items , Iterator <Matcher <? super T >> remainingMatchers ) {
46+ this .items = items ;
47+ this .remainingMatchers = remainingMatchers ;
48+ }
49+
50+ boolean foundFirst () {
51+ final Matcher <? super T > firstMatcher = nextMatcher ();
52+ while (items .hasNext ()) {
53+ if (firstMatcher .matches (nextItem ())) return true ;
54+ }
55+ return false ;
56+ }
57+
58+
59+ boolean foundRemainder () {
60+ while (remainingMatchers .hasNext ()) {
61+ if (! nextItemMatches (nextMatcher ())) {
62+ return false ;
63+ }
64+ }
65+ return true ;
66+ }
67+
68+ private boolean nextItemMatches (Matcher <? super T > matcher ) {
69+ return items .hasNext () && matcher .matches (nextItem ());
70+ }
71+
72+ private T nextItem () {
73+ final T next = items .next ();
74+ viewedItems .add (next );
75+ return next ;
76+ }
77+
78+ private Matcher <? super T > nextMatcher () {
79+ final Matcher <? super T > matcher = remainingMatchers .next ();
80+ currentMatcher = matcher ;
81+ return matcher ;
82+ }
83+
84+ private void describeNotFound (Description mismatch ) {
85+ mismatch .appendText ("subsequence not in " )
86+ .appendValueList ("[" , ", " , "]" , viewedItems );
87+
88+ if (null != currentMatcher ) {
89+ mismatch .appendText (" because no match for " );
90+ mismatch .appendDescriptionOf (currentMatcher );
91+ }
92+ }
93+ }
5694}
0 commit comments