77import org .hamcrest .Description ;
88import org .hamcrest .Factory ;
99import org .hamcrest .Matcher ;
10+ import org .hamcrest .TypeSafeMatcher ;
1011
1112/**
1213 * Matches if collection size satisfies a nested matcher.
1314 */
14- public class IsCollectionWithSize <E > extends CollectionMatcher < E > {
15- private final Matcher <Integer > sizeMatcher ;
15+ public class IsCollectionWithSize <E > extends TypeSafeMatcher < Collection <? extends E > > {
16+ private final Matcher <? super Integer > sizeMatcher ;
1617
17- public IsCollectionWithSize (Matcher <Integer > sizeMatcher ) {
18+ public IsCollectionWithSize (Matcher <? super Integer > sizeMatcher ) {
1819 this .sizeMatcher = sizeMatcher ;
1920 }
2021
2122 @ Override
22- public boolean matchesSafely (Collection <E > item ) {
23+ public boolean matchesSafely (Collection <? extends E > item ) {
2324 return sizeMatcher .matches (item .size ());
2425 }
2526
@@ -32,18 +33,20 @@ public void describeTo(Description description) {
3233 * Does collection size satisfy a given matcher?
3334 */
3435 @ Factory
35- public static <E > Matcher <Collection <E >> hasSize (Matcher <Integer > size ) {
36+ public static <E > Matcher <? super Collection <? extends E >> hasSize (Matcher <? super Integer > size ) {
3637 return new IsCollectionWithSize <E >(size );
3738 }
3839
40+
3941 /**
4042 * This is a shortcut to the frequently used hasSize(equalTo(x)).
4143 *
4244 * For example, assertThat(hasSize(equal_to(x)))
4345 * vs. assertThat(hasSize(x))
4446 */
4547 @ Factory
46- public static <E > Matcher <Collection <E >> hasSize (int size ) {
47- return hasSize (equalTo (size ));
48+ public static <E > Matcher <? super Collection <? extends E >> hasSize (int size ) {
49+ Matcher <? super Integer > matcher = equalTo (size );
50+ return IsCollectionWithSize .<E >hasSize (matcher );
4851 }
4952}
0 commit comments