Skip to content

Commit 2e7b1f2

Browse files
committed
add empty iterable/collection factory methods with fixed generic type, to ease interop with jmock
1 parent 3a569dc commit 2e7b1f2

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,38 @@ public void describeMismatchSafely(Collection<? extends E> item, Description mis
2222
mismatchDescription.appendValue(item);
2323
}
2424

25-
@Override
26-
public void describeTo(Description description) {
25+
@Override
26+
public void describeTo(Description description) {
2727
description.appendText("an empty collection");
2828
}
2929

30-
/**
31-
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
32-
* method returns <code>true</code>.
33-
* <p/>
34-
* For example:
35-
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(empty()))</pre>
36-
*
37-
*/
30+
/**
31+
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
32+
* method returns <code>true</code>.
33+
* <p/>
34+
* For example:
35+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(empty()))</pre>
36+
*
37+
*/
3838
@Factory
3939
public static <E> Matcher<Collection<? extends E>> empty() {
4040
return new IsEmptyCollection<E>();
4141
}
42+
43+
/**
44+
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
45+
* method returns <code>true</code>.
46+
* <p/>
47+
* For example:
48+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyCollectionOf(String.class)))</pre>
49+
*
50+
* @param type
51+
* the type of the collection's content
52+
*/
53+
@Factory
54+
public static <E> Matcher<Collection<E>> emptyCollectionOf(Class<E> type) {
55+
@SuppressWarnings({ "rawtypes", "unchecked" })
56+
final Matcher<Collection<E>> result = (Matcher)empty();
57+
return result;
58+
}
4259
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,20 @@ public void describeTo(Description description) {
3535
public static <E> Matcher<Iterable<? extends E>> emptyIterable() {
3636
return new IsEmptyIterable<E>();
3737
}
38+
39+
/**
40+
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
41+
* <p/>
42+
* For example:
43+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterableOf(String.class)))</pre>
44+
*
45+
* @param type
46+
* the type of the iterable's content
47+
*/
48+
@Factory
49+
public static <E> Matcher<Iterable<E>> emptyIterableOf(Class<E> type) {
50+
@SuppressWarnings({ "rawtypes", "unchecked" })
51+
final Matcher<Iterable<E>> result = (Matcher)emptyIterable();
52+
return result;
53+
}
3854
}

hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsEmptyCollectionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public void testHasAReadableDescription() {
2929
assertDescription("an empty collection", createMatcher());
3030
}
3131

32+
public void testCompiles() {
33+
needs(IsEmptyCollection.emptyCollectionOf(String.class));
34+
}
35+
36+
private void needs(@SuppressWarnings("unused") Matcher<Collection<String>> bar) { }
37+
3238
private static Collection<String> collectionOfValues() {
3339
return new ArrayList<String>(asList("one", "three"));
3440
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package org.hamcrest.collection;
22

3-
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
4-
53
import java.util.ArrayList;
64
import java.util.Collection;
75

86
import org.hamcrest.AbstractMatcherTest;
97
import org.hamcrest.Matcher;
108

119
import static java.util.Arrays.asList;
10+
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
1211

1312
public class IsEmptyIterableTest extends AbstractMatcherTest {
1413

@@ -29,6 +28,12 @@ public void testHasAReadableDescription() {
2928
assertDescription("an empty iterable", createMatcher());
3029
}
3130

31+
public void testCompiles() {
32+
needs(IsEmptyIterable.emptyIterableOf(String.class));
33+
}
34+
35+
private void needs(@SuppressWarnings("unused") Matcher<Iterable<String>> bar) { }
36+
3237
private static Collection<String> collectionOfValues() {
3338
return new ArrayList<String>(asList("one", "three"));
3439
}

0 commit comments

Comments
 (0)