11
11
12
12
import static org .hamcrest .core .IsEqual .equalTo ;
13
13
14
- public class IsIterableContainingInOrder <E > extends TypeSafeDiagnosingMatcher < Iterable <? extends E >> {
14
+ public class IsIterableContainingInOrder <E , C extends Iterable <? extends E >> extends TypeSafeDiagnosingMatcher < C > {
15
15
private final List <Matcher <? super E >> matchers ;
16
16
17
17
public IsIterableContainingInOrder (List <Matcher <? super E >> matchers ) {
18
- this .matchers = matchers ;
18
+ this .matchers = matchers ;
19
19
}
20
-
20
+
21
21
@ Override
22
- protected boolean matchesSafely (Iterable <? extends E > iterable , Description mismatchDescription ) {
23
- MatchSeries <E > matchSeries = new MatchSeries <E >(matchers , mismatchDescription );
24
- for (E item : iterable ) {
25
- if (! matchSeries .matches (item )) {
26
- return false ;
22
+ protected boolean matchesSafely (C iterable , Description mismatchDescription ) {
23
+ MatchSeries <E > matchSeries = new MatchSeries <E >(matchers , mismatchDescription );
24
+ for (E item : iterable ) {
25
+ if (!matchSeries .matches (item )) {
26
+ return false ;
27
+ }
27
28
}
28
- }
29
-
30
- return matchSeries .isFinished ();
29
+
30
+ return matchSeries .isFinished ();
31
31
}
32
32
33
33
public void describeTo (Description description ) {
34
34
description .appendText ("iterable containing " ).appendList ("[" , ", " , "]" , matchers );
35
35
}
36
36
37
37
private static class MatchSeries <F > {
38
- public final List <Matcher <? super F >> matchers ;
39
- private final Description mismatchDescription ;
40
- public int nextMatchIx = 0 ;
41
-
42
- public MatchSeries (List <Matcher <? super F >> matchers , Description mismatchDescription ) {
43
- this .mismatchDescription = mismatchDescription ;
44
- if (matchers .isEmpty ()) {
45
- throw new IllegalArgumentException ("Should specify at least one expected element" );
38
+ public final List <Matcher <? super F >> matchers ;
39
+ private final Description mismatchDescription ;
40
+ public int nextMatchIx = 0 ;
41
+
42
+ public MatchSeries (List <Matcher <? super F >> matchers , Description mismatchDescription ) {
43
+ this .mismatchDescription = mismatchDescription ;
44
+ if (matchers .isEmpty ()) {
45
+ throw new IllegalArgumentException ("Should specify at least one expected element" );
46
+ }
47
+ this .matchers = matchers ;
46
48
}
47
- this .matchers = matchers ;
48
- }
49
49
50
- public boolean matches (F item ) {
51
- return isNotSurplus (item ) && isMatched (item );
52
- }
50
+ public boolean matches (F item ) {
51
+ return isNotSurplus (item ) && isMatched (item );
52
+ }
53
53
54
- public boolean isFinished () {
55
- if (nextMatchIx < matchers .size ()) {
56
- mismatchDescription .appendText ("No item matched: " ).appendDescriptionOf (matchers .get (nextMatchIx ));
57
- return false ;
54
+ public boolean isFinished () {
55
+ if (nextMatchIx < matchers .size ()) {
56
+ mismatchDescription .appendText ("No item matched: " ).appendDescriptionOf (matchers .get (nextMatchIx ));
57
+ return false ;
58
+ }
59
+ return true ;
58
60
}
59
- return true ;
60
- }
61
-
62
- private boolean isMatched (F item ) {
63
- Matcher <? super F > matcher = matchers .get (nextMatchIx );
64
- if (!matcher .matches (item )) {
65
- describeMismatch (matcher , item );
66
- return false ;
61
+
62
+ private boolean isMatched (F item ) {
63
+ Matcher <? super F > matcher = matchers .get (nextMatchIx );
64
+ if (!matcher .matches (item )) {
65
+ describeMismatch (matcher , item );
66
+ return false ;
67
+ }
68
+ nextMatchIx ++;
69
+ return true ;
67
70
}
68
- nextMatchIx ++;
69
- return true ;
70
- }
71
-
72
- private boolean isNotSurplus (F item ) {
73
- if (matchers .size () <= nextMatchIx ) {
74
- mismatchDescription .appendText ("Not matched: " ).appendValue (item );
75
- return false ;
71
+
72
+ private boolean isNotSurplus (F item ) {
73
+ if (matchers .size () <= nextMatchIx ) {
74
+ mismatchDescription .appendText ("Not matched: " ).appendValue (item );
75
+ return false ;
76
+ }
77
+ return true ;
78
+ }
79
+
80
+ private void describeMismatch (Matcher <? super F > matcher , F item ) {
81
+ mismatchDescription .appendText ("item " + nextMatchIx + ": " );
82
+ matcher .describeMismatch (item , mismatchDescription );
76
83
}
77
- return true ;
78
- }
79
-
80
- private void describeMismatch (Matcher <? super F > matcher , F item ) {
81
- mismatchDescription .appendText ("item " + nextMatchIx + ": " );
82
- matcher .describeMismatch (item , mismatchDescription );
83
- }
84
84
}
85
85
86
86
@ Factory
@@ -93,17 +93,17 @@ public static <E> Matcher<Iterable<? extends E>> contains(E... items) {
93
93
}
94
94
95
95
@ Factory
96
- public static <E > Matcher < Iterable <? extends E >> contains (final Matcher <E > item ) {
96
+ public static <E , C extends Iterable <? extends E >> Matcher < C > contains (final Matcher <E > item ) {
97
97
return contains (new ArrayList <Matcher <? super E >>(Arrays .asList (item )));
98
98
}
99
99
100
100
@ Factory
101
- public static <E > Matcher < Iterable <? extends E >> contains (Matcher <? super E >... items ) {
101
+ public static <E , C extends Iterable <? extends E >> Matcher < C > contains (Matcher <? super E >... items ) {
102
102
return contains (Arrays .asList (items ));
103
103
}
104
104
105
105
@ Factory
106
- public static <E > Matcher < Iterable <? extends E >> contains (List <Matcher <? super E >> contents ) {
107
- return new IsIterableContainingInOrder <E >(contents );
106
+ public static <E , C extends Iterable <? extends E >> Matcher < C > contains (List <Matcher <? super E >> contents ) {
107
+ return new IsIterableContainingInOrder <E , C >(contents );
108
108
}
109
109
}
0 commit comments