Skip to content

Commit 7c629d3

Browse files
committed
more factory methods for iterables. Started MatchCollections
1 parent 0dd0797 commit 7c629d3

File tree

6 files changed

+70
-81
lines changed

6 files changed

+70
-81
lines changed

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ public static <E> org.hamcrest.Matcher<java.util.Collection<E>> emptyCollectionO
746746
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterable()))</pre>
747747
*/
748748
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> emptyIterable() {
749-
return org.hamcrest.collection.IsEmptyIterable.emptyIterable();
749+
return MatchIterables.emptyIterable();
750750
}
751751

752752
/**
@@ -757,7 +757,7 @@ public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> emptyIte
757757
* @param unusedToForceReturnType the type of the iterable's content
758758
*/
759759
public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> emptyIterableOf(java.lang.Class<E> unusedToForceReturnType) {
760-
return org.hamcrest.collection.IsEmptyIterable.emptyIterableOf(unusedToForceReturnType);
760+
return MatchIterables.emptyIterableOf(unusedToForceReturnType);
761761
}
762762

763763
/**
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.hamcrest.collection;
22

33
import org.hamcrest.Description;
4-
import org.hamcrest.Matcher;
54
import org.hamcrest.TypeSafeMatcher;
65

76
/**
@@ -22,27 +21,4 @@ public void describeMismatchSafely(Iterable<? extends E> iter, Description misma
2221
public void describeTo(Description description) {
2322
description.appendText("an empty iterable");
2423
}
25-
26-
/**
27-
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
28-
* For example:
29-
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterable()))</pre>
30-
*
31-
*/
32-
public static <E> Matcher<Iterable<? extends E>> emptyIterable() {
33-
return new IsEmptyIterable<>();
34-
}
35-
36-
/**
37-
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
38-
* For example:
39-
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterableOf(String.class)))</pre>
40-
*
41-
* @param unusedToForceReturnType
42-
* the type of the iterable's content
43-
*/
44-
@SuppressWarnings({"unchecked", "UnusedParameters"})
45-
public static <E> Matcher<Iterable<E>> emptyIterableOf(Class<E> unusedToForceReturnType) {
46-
return (Matcher)emptyIterable();
47-
}
4824
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public IsIterableContainingInAnyOrder(Collection<Matcher<? super T>> matchers) {
2020

2121
@Override
2222
protected boolean matchesSafely(Iterable<? extends T> items, Description mismatchDescription) {
23-
final Matching<T> matching = new Matching<>(matchers, mismatchDescription);
23+
final Containing<T> containing = new Containing<>(matchers, mismatchDescription);
2424
for (T item : items) {
25-
if (! matching.matches(item)) {
25+
if (! containing.matches(item)) {
2626
return false;
2727
}
2828
}
2929

30-
return matching.isFinished(items);
30+
return containing.isFinished(items);
3131
}
3232

3333
@Override
@@ -37,11 +37,11 @@ public void describeTo(Description description) {
3737
.appendText(" in any order");
3838
}
3939

40-
private static class Matching<S> {
40+
private static class Containing<S> {
4141
private final Collection<Matcher<? super S>> matchers;
4242
private final Description mismatchDescription;
4343

44-
public Matching(Collection<Matcher<? super S>> matchers, Description mismatchDescription) {
44+
public Containing(Collection<Matcher<? super S>> matchers, Description mismatchDescription) {
4545
this.matchers = new ArrayList<>(matchers);
4646
this.mismatchDescription = mismatchDescription;
4747
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.Matcher;
4+
5+
import java.util.Collection;
6+
7+
import static org.hamcrest.object.MatchObjects.equalTo;
8+
9+
/**
10+
* @since 2.0
11+
*/
12+
public class MatchCollections {
13+
/**
14+
* Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns
15+
* a value that satisfies the specified matcher.
16+
* For example:
17+
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))</pre>
18+
*
19+
* @param sizeMatcher a matcher for the size of an examined {@link java.util.Collection}
20+
*/
21+
public static <E> Matcher<Collection<? extends E>> hasSize(Matcher<? super Integer> sizeMatcher) {
22+
return new IsCollectionWithSize<>(sizeMatcher);
23+
}
24+
25+
/**
26+
* Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns
27+
* a value equal to the specified <code>size</code>.
28+
* For example:
29+
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(2))</pre>
30+
*
31+
* @param size the expected size of an examined {@link java.util.Collection}
32+
*/
33+
public static <E> Matcher<Collection<? extends E>> hasSize(int size) {
34+
return hasSize(equalTo(size));
35+
}
36+
37+
/**
38+
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
39+
* method returns <code>true</code>.
40+
* For example:
41+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(empty()))</pre>
42+
*/
43+
public static <E> Matcher<Collection<? extends E>> empty() {
44+
return new IsEmptyCollection<>();
45+
}
46+
47+
/**
48+
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
49+
* method returns <code>true</code>.
50+
* For example:
51+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyCollectionOf(String.class)))</pre>
52+
*
53+
* @param unusedToForceReturnType the type of the collection's content
54+
*/
55+
public static <E> Matcher<Collection<? extends E>> emptyCollectionOf(Class<E> unusedToForceReturnType) {
56+
return new IsEmptyCollection<>();
57+
}
58+
}

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

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -99,52 +99,6 @@ public static <T> Matcher<Iterable<T>> hasItems(T... items) {
9999
return Matchers.allOf(all);
100100
}
101101

102-
/**
103-
* Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns
104-
* a value that satisfies the specified matcher.
105-
* For example:
106-
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))</pre>
107-
*
108-
* @param sizeMatcher a matcher for the size of an examined {@link java.util.Collection}
109-
*/
110-
public static <E> Matcher<Collection<? extends E>> hasSize(Matcher<? super Integer> sizeMatcher) {
111-
return new IsCollectionWithSize<>(sizeMatcher);
112-
}
113-
114-
/**
115-
* Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns
116-
* a value equal to the specified <code>size</code>.
117-
* For example:
118-
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(2))</pre>
119-
*
120-
* @param size the expected size of an examined {@link java.util.Collection}
121-
*/
122-
public static <E> Matcher<Collection<? extends E>> hasSize(int size) {
123-
return hasSize(equalTo(size));
124-
}
125-
126-
/**
127-
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
128-
* method returns <code>true</code>.
129-
* For example:
130-
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(empty()))</pre>
131-
*/
132-
public static <E> Matcher<Collection<? extends E>> empty() {
133-
return new IsEmptyCollection<>();
134-
}
135-
136-
/**
137-
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
138-
* method returns <code>true</code>.
139-
* For example:
140-
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyCollectionOf(String.class)))</pre>
141-
*
142-
* @param unusedToForceReturnType the type of the collection's content
143-
*/
144-
public static <E> Matcher<Collection<? extends E>> emptyCollectionOf(Class<E> unusedToForceReturnType) {
145-
return new IsEmptyCollection<>();
146-
}
147-
148102
/**
149103
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
150104
* For example:
@@ -161,8 +115,9 @@ public static <E> Matcher<Iterable<? extends E>> emptyIterable() {
161115
*
162116
* @param unusedToForceReturnType the type of the iterable's content
163117
*/
164-
public static <E> Matcher<Iterable<? extends E>> emptyIterableOf(Class<E> unusedToForceReturnType) {
165-
return new IsEmptyIterable<>();
118+
@SuppressWarnings("unchecked")
119+
public static <E> Matcher<Iterable<E>> emptyIterableOf(Class<E> unusedToForceReturnType) {
120+
return (Matcher)new IsEmptyIterable<>();
166121
}
167122

168123
/**

hamcrest-library/src/test/java/org/hamcrest/collection/IsEmptyIterableTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Collection;
88

99
import static java.util.Arrays.asList;
10-
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
10+
import static org.hamcrest.collection.MatchIterables.emptyIterable;
1111

1212
public class IsEmptyIterableTest extends AbstractMatcherTest {
1313

@@ -29,7 +29,7 @@ public void testHasAReadableDescription() {
2929
}
3030

3131
public void testCompiles() {
32-
needs(IsEmptyIterable.emptyIterableOf(String.class));
32+
needs(MatchIterables.emptyIterableOf(String.class));
3333
}
3434

3535
private void needs(@SuppressWarnings("unused") Matcher<Iterable<String>> bar) { }

0 commit comments

Comments
 (0)