Skip to content

Commit b59ee14

Browse files
committed
Renamed, IsCollectionContaining to IsIterableContaining
Idea-prompted tidy-ups.
1 parent c445ec3 commit b59ee14

File tree

8 files changed

+31
-33
lines changed

8 files changed

+31
-33
lines changed

hamcrest-core/src/main/java/org/hamcrest/CoreMatchers.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hamcrest;
22

3+
import org.hamcrest.core.IsIterableContaining;
4+
35
@SuppressWarnings("UnusedDeclaration")
46
public class CoreMatchers {
57

@@ -155,7 +157,7 @@ public static org.hamcrest.Matcher<java.lang.Object> anything(java.lang.String d
155157
* the matcher to apply to items provided by the examined {@link Iterable}
156158
*/
157159
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) {
158-
return org.hamcrest.core.IsCollectionContaining.hasItem(itemMatcher);
160+
return IsIterableContaining.hasItem(itemMatcher);
159161
}
160162

161163
/**
@@ -170,7 +172,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(or
170172
* the item to compare against the items provided by the examined {@link Iterable}
171173
*/
172174
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) {
173-
return org.hamcrest.core.IsCollectionContaining.hasItem(item);
175+
return IsIterableContaining.hasItem(item);
174176
}
175177

176178
/**
@@ -186,7 +188,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T
186188
*/
187189
@SafeVarargs
188190
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) {
189-
return org.hamcrest.core.IsCollectionContaining.hasItems(itemMatchers);
191+
return IsIterableContaining.hasItems(itemMatchers);
190192
}
191193

192194
/**
@@ -202,7 +204,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcr
202204
*/
203205
@SafeVarargs
204206
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) {
205-
return org.hamcrest.core.IsCollectionContaining.hasItems(items);
207+
return IsIterableContaining.hasItems(items);
206208
}
207209

208210
/**

hamcrest-core/src/main/java/org/hamcrest/core/IsCollectionContaining.java renamed to hamcrest-core/src/main/java/org/hamcrest/core/IsIterableContaining.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import static org.hamcrest.core.AllOf.allOf;
1111
import static org.hamcrest.core.IsEqual.equalTo;
1212

13-
public class IsCollectionContaining<T> extends TypeSafeDiagnosingMatcher<Iterable<? super T>> {
13+
public class IsIterableContaining<T> extends TypeSafeDiagnosingMatcher<Iterable<? super T>> {
1414
private final Matcher<? super T> elementMatcher;
1515

16-
public IsCollectionContaining(Matcher<? super T> elementMatcher) {
16+
public IsIterableContaining(Matcher<? super T> elementMatcher) {
1717
this.elementMatcher = elementMatcher;
1818
}
1919

@@ -67,7 +67,7 @@ public void describeTo(Description description) {
6767
* the matcher to apply to items provided by the examined {@link Iterable}
6868
*/
6969
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) {
70-
return new IsCollectionContaining<>(itemMatcher);
70+
return new IsIterableContaining<>(itemMatcher);
7171
}
7272

7373
/**
@@ -83,7 +83,7 @@ public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMa
8383
*/
8484
public static <T> Matcher<Iterable<? super T>> hasItem(T item) {
8585
// Doesn't forward to hasItem() method so compiler can sort out generics.
86-
return new IsCollectionContaining<>(equalTo(item));
86+
return new IsIterableContaining<>(equalTo(item));
8787
}
8888

8989
/**
@@ -103,7 +103,7 @@ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatche
103103

104104
for (Matcher<? super T> elementMatcher : itemMatchers) {
105105
// Doesn't forward to hasItem() method so compiler can sort out generics.
106-
all.add(new IsCollectionContaining<>(elementMatcher));
106+
all.add(new IsIterableContaining<>(elementMatcher));
107107
}
108108

109109
return allOf(all);

hamcrest-core/src/test/java/org/hamcrest/core/IsCollectionContainingTest.java renamed to hamcrest-core/src/test/java/org/hamcrest/core/IsIterableContainingTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
import static java.util.Arrays.asList;
1313
import static org.hamcrest.AbstractMatcherTest.*;
14-
import static org.hamcrest.core.IsCollectionContaining.hasItem;
15-
import static org.hamcrest.core.IsCollectionContaining.hasItems;
14+
import static org.hamcrest.core.IsIterableContaining.hasItem;
15+
import static org.hamcrest.core.IsIterableContaining.hasItems;
1616
import static org.hamcrest.core.IsEqual.equalTo;
1717

18-
public final class IsCollectionContainingTest {
18+
public final class IsIterableContainingTest {
1919

2020
@Test public void
2121
copesWithNullsAndUnknownTypes() {
@@ -52,11 +52,11 @@ public final class IsCollectionContainingTest {
5252

5353
@Test public void
5454
canMatchItemWhenCollectionHoldsSuperclass() { // Issue 24
55-
final Set<Number> s = new HashSet<Number>();
55+
final Set<Number> s = new HashSet<>();
5656
s.add(2);
5757

58-
assertMatches(new IsCollectionContaining<Number>(new IsEqual<Number>(2)), s);
59-
assertMatches(IsCollectionContaining.hasItem(2), s);
58+
assertMatches(new IsIterableContaining<>(new IsEqual<Number>(2)), s);
59+
assertMatches(IsIterableContaining.hasItem(2), s);
6060
}
6161

6262
@SuppressWarnings("unchecked")

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hamcrest;
22

33
import org.hamcrest.collection.ArrayMatching;
4+
import org.hamcrest.core.IsIterableContaining;
45
import org.hamcrest.core.StringRegularExpression;
56

67
import java.util.regex.Pattern;
@@ -249,7 +250,7 @@ public static org.hamcrest.Matcher<java.lang.Object> anything(java.lang.String d
249250
* the matcher to apply to items provided by the examined {@link Iterable}
250251
*/
251252
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) {
252-
return org.hamcrest.core.IsCollectionContaining.hasItem(itemMatcher);
253+
return IsIterableContaining.hasItem(itemMatcher);
253254
}
254255

255256
/**
@@ -264,7 +265,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(or
264265
* the item to compare against the items provided by the examined {@link Iterable}
265266
*/
266267
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) {
267-
return org.hamcrest.core.IsCollectionContaining.hasItem(item);
268+
return IsIterableContaining.hasItem(item);
268269
}
269270

270271
/**
@@ -280,7 +281,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T
280281
*/
281282
@SafeVarargs
282283
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) {
283-
return org.hamcrest.core.IsCollectionContaining.hasItems(itemMatchers);
284+
return IsIterableContaining.hasItems(itemMatchers);
284285
}
285286

286287
/**
@@ -296,7 +297,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcr
296297
*/
297298
@SafeVarargs
298299
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) {
299-
return org.hamcrest.core.IsCollectionContaining.hasItems(items);
300+
return IsIterableContaining.hasItems(items);
300301
}
301302

302303
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatc
164164
return new ArrayAsIterableMatcher<>(new IsIterableContainingInOrder<>(itemMatchers), itemMatchers, "");
165165
}
166166

167-
private static <E> List<Matcher<? super E>> asEqualMatchers(E[] items) {
167+
public static <E> List<Matcher<? super E>> asEqualMatchers(E[] items) {
168168
final List<Matcher<? super E>> matchers = new ArrayList<>();
169169
for (E item : items) {
170170
matchers.add(equalTo(item));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.hamcrest.Matcher;
55
import org.hamcrest.TypeSafeDiagnosingMatcher;
66
import org.hamcrest.TypeSafeMatcher;
7-
import org.hamcrest.core.IsCollectionContaining;
7+
import org.hamcrest.core.IsIterableContaining;
88

99
import static java.util.Arrays.asList;
1010

@@ -17,7 +17,7 @@ public class HasItemInArray<T> extends TypeSafeMatcher<T[]> {
1717

1818
public HasItemInArray(Matcher<? super T> elementMatcher) {
1919
this.elementMatcher = elementMatcher;
20-
this.collectionMatcher = new IsCollectionContaining<>(elementMatcher);
20+
this.collectionMatcher = new IsIterableContaining<>(elementMatcher);
2121
}
2222

2323
@Override

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010

1111
import static java.util.Collections.singletonList;
12-
import static org.hamcrest.core.IsEqual.equalTo;
12+
import static org.hamcrest.collection.ArrayMatching.asEqualMatchers;
1313

1414
public class IsIterableContainingInOrder<E> extends TypeSafeDiagnosingMatcher<Iterable<? extends E>> {
1515
private final List<Matcher<? super E>> matchers;
@@ -94,12 +94,7 @@ private void describeMismatch(Matcher<? super F> matcher, F item) {
9494
*/
9595
@SafeVarargs
9696
public static <E> Matcher<Iterable<? extends E>> contains(E... items) {
97-
List<Matcher<? super E>> matchers = new ArrayList<>();
98-
for (E item : items) {
99-
matchers.add(equalTo(item));
100-
}
101-
102-
return contains(matchers);
97+
return contains(asEqualMatchers(items));
10398
}
10499

105100
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99

1010
import static java.util.Arrays.asList;
11+
import static java.util.Collections.singletonList;
1112
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
1213
import static org.hamcrest.core.IsEqual.equalTo;
1314

@@ -22,7 +23,7 @@ protected Matcher<?> createMatcher() {
2223
}
2324

2425
public void testMatchingSingleItemIterable() throws Exception {
25-
assertMatches("Single item iterable", contains(1), asList(1));
26+
assertMatches("Single item iterable", contains(1), singletonList(1));
2627
}
2728

2829
public void testMatchingMultipleItemIterable() throws Exception {
@@ -34,12 +35,11 @@ public void testDoesNotMatchWithMoreElementsThanExpected() throws Exception {
3435
}
3536

3637
public void testDoesNotMatchWithFewerElementsThanExpected() throws Exception {
37-
List<WithValue> valueList = asList(make(1), make(2));
38-
assertMismatchDescription("no item was value with <3>", contains123, valueList);
38+
assertMismatchDescription("no item was value with <3>", contains123, asList(make(1), make(2)));
3939
}
4040

4141
public void testDoesNotMatchIfSingleItemMismatches() throws Exception {
42-
assertMismatchDescription("item 0: value was <3>", contains(value(4)), asList(make(3)));
42+
assertMismatchDescription("item 0: value was <3>", contains(value(4)), singletonList(make(3)));
4343
}
4444

4545
public void testDoesNotMatchIfOneOfMultipleItemsMismatch() throws Exception {

0 commit comments

Comments
 (0)