Skip to content

Commit 7265db5

Browse files
committed
address issue 97 (google code project issue tracker): generics too strict on IsEmptyCollection
1 parent 1c33be1 commit 7265db5

4 files changed

Lines changed: 39 additions & 24 deletions

File tree

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsEmptyCollection.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
/**
1111
* Tests if collection is empty.
1212
*/
13-
public class IsEmptyCollection<E> extends TypeSafeMatcher<Collection<E>> {
13+
public class IsEmptyCollection<E> extends TypeSafeMatcher<Collection<? extends E>> {
1414

1515
@Override
16-
public boolean matchesSafely(Collection<E> item) {
16+
public boolean matchesSafely(Collection<? extends E> item) {
1717
return item.isEmpty();
1818
}
1919

2020
@Override
21-
public void describeMismatchSafely(Collection<E> item, Description mismatchDescription) {
21+
public void describeMismatchSafely(Collection<? extends E> item, Description mismatchDescription) {
2222
mismatchDescription.appendValue(item);
2323
}
2424

@@ -31,7 +31,7 @@ public void describeTo(Description description) {
3131
* Matches an empty collection.
3232
*/
3333
@Factory
34-
public static <E> Matcher<Collection<E>> empty() {
34+
public static <E> Matcher<Collection<? extends E>> empty() {
3535
return new IsEmptyCollection<E>();
3636
}
3737
}

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsEmptyIterable.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
/**
99
* Tests if collection is empty.
1010
*/
11-
public class IsEmptyIterable<E> extends TypeSafeMatcher<Iterable<E>> {
11+
public class IsEmptyIterable<E> extends TypeSafeMatcher<Iterable<? extends E>> {
1212

1313
@Override
14-
public boolean matchesSafely(Iterable<E> iterable) {
14+
public boolean matchesSafely(Iterable<? extends E> iterable) {
1515
return !iterable.iterator().hasNext();
1616
}
1717
@Override
18-
public void describeMismatchSafely(Iterable<E> iter, Description mismatchDescription) {
18+
public void describeMismatchSafely(Iterable<? extends E> iter, Description mismatchDescription) {
1919
mismatchDescription.appendValueList("[", ",", "]", iter);
2020
}
2121

@@ -28,7 +28,7 @@ public void describeTo(Description description) {
2828
* Matches an empty iterable.
2929
*/
3030
@Factory
31-
public static <E> Matcher<Iterable<E>> emptyIterable() {
31+
public static <E> Matcher<Iterable<? extends E>> emptyIterable() {
3232
return new IsEmptyIterable<E>();
3333
}
3434
}
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
package org.hamcrest.collection;
22

3-
import org.hamcrest.AbstractMatcherTest;
4-
import org.hamcrest.Matcher;
5-
63
import java.util.ArrayList;
7-
import java.util.Arrays;
84
import java.util.Collection;
95

6+
import org.hamcrest.AbstractMatcherTest;
7+
import org.hamcrest.Matcher;
8+
9+
import static java.util.Arrays.asList;
1010
import static org.hamcrest.collection.IsEmptyCollection.empty;
1111
import static org.hamcrest.core.Is.is;
1212

1313
public class IsEmptyCollectionTest extends AbstractMatcherTest {
1414

1515
@Override
16-
protected Matcher<?> createMatcher() {
16+
protected Matcher<Collection<?>> createMatcher() {
1717
return empty();
1818
}
1919

2020
public void testMatchesAnEmptyCollection() {
21-
assertMatches("empty collection", empty(), Arrays.asList());
21+
assertMatches("empty collection", createMatcher(), emptyCollection());
2222
}
2323

2424
public void testDoesNotMatchACollectionWithAnItem() {
25-
assertMismatchDescription("<[one, three]>", is(empty()), collectionOfValues());
25+
assertMismatchDescription("<[one, three]>", is(createMatcher()), collectionOfValues());
2626
}
2727

2828
public void testHasAReadableDescription() {
29-
assertDescription("an empty collection", empty());
29+
assertDescription("an empty collection", createMatcher());
30+
}
31+
32+
private static Collection<String> collectionOfValues() {
33+
return new ArrayList<String>(asList("one", "three"));
3034
}
3135

32-
private static Collection<Object> collectionOfValues() {
33-
return new ArrayList<Object>() {{ add("one"); add("three"); }};
36+
private static Collection<Integer> emptyCollection() {
37+
return new ArrayList<Integer>();
3438
}
35-
}
39+
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsEmptyIterableTest.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22

33
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
44

5-
import java.util.Arrays;
5+
import java.util.ArrayList;
6+
import java.util.Collection;
67

78
import org.hamcrest.AbstractMatcherTest;
89
import org.hamcrest.Matcher;
910

11+
import static java.util.Arrays.asList;
12+
1013
public class IsEmptyIterableTest extends AbstractMatcherTest {
1114

1215
@Override
13-
protected Matcher<?> createMatcher() {
16+
protected Matcher<Iterable<?>> createMatcher() {
1417
return emptyIterable();
1518
}
1619

1720
public void testMatchesAnEmptyIterable() {
18-
assertMatches("empty iterable", emptyIterable(), Arrays.asList());
21+
assertMatches("empty iterable", createMatcher(), emptyCollection());
1922
}
2023

2124
public void testDoesNotMatchAnIterableWithItems() {
22-
assertDoesNotMatch("iterable with an item", emptyIterable(), Arrays.<Object>asList(1));
25+
assertDoesNotMatch("iterable with an item", createMatcher(), collectionOfValues());
2326
}
2427

2528
public void testHasAReadableDescription() {
26-
assertDescription("an empty iterable", emptyIterable());
29+
assertDescription("an empty iterable", createMatcher());
30+
}
31+
32+
private static Collection<String> collectionOfValues() {
33+
return new ArrayList<String>(asList("one", "three"));
34+
}
35+
36+
private static Collection<Integer> emptyCollection() {
37+
return new ArrayList<Integer>();
2738
}
2839
}

0 commit comments

Comments
 (0)