Skip to content

Commit 0221ff1

Browse files
committed
Factory methods. Collections contains
1 parent 21a350f commit 0221ff1

File tree

4 files changed

+73
-82
lines changed

4 files changed

+73
-82
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> emptyIterableOf(ja
773773
*/
774774
@SafeVarargs
775775
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(E... items) {
776-
return org.hamcrest.collection.IsIterableContainingInOrder.contains(items);
776+
return MatchCollections.contains(items);
777777
}
778778

779779
/**
@@ -787,7 +787,7 @@ public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains
787787
* examined {@link Iterable}
788788
*/
789789
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E> itemMatcher) {
790-
return org.hamcrest.collection.IsIterableContainingInOrder.contains(itemMatcher);
790+
return MatchCollections.contains(itemMatcher);
791791
}
792792

793793
/**
@@ -802,7 +802,7 @@ public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains
802802
*/
803803
@SafeVarargs
804804
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E>... itemMatchers) {
805-
return org.hamcrest.collection.IsIterableContainingInOrder.contains(itemMatchers);
805+
return MatchCollections.contains(itemMatchers);
806806
}
807807

808808
/**
@@ -817,7 +817,7 @@ public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains
817817
* an examined {@link Iterable}
818818
*/
819819
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {
820-
return org.hamcrest.collection.IsIterableContainingInOrder.contains(itemMatchers);
820+
return MatchCollections.contains(itemMatchers);
821821
}
822822

823823
/**

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

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
55
import org.hamcrest.TypeSafeDiagnosingMatcher;
6-
import org.hamcrest.internal.Wrapping;
76

8-
import java.util.ArrayList;
97
import java.util.List;
108

11-
import static java.util.Arrays.asList;
12-
import static org.hamcrest.core.IsEqual.equalTo;
13-
149
public class IsIterableContainingInOrder<E> extends TypeSafeDiagnosingMatcher<Iterable<? extends E>> {
1510
private final List<Matcher<? super E>> matchers;
1611

@@ -80,76 +75,4 @@ private void describeMismatch(Matcher<? super F> matcher, F item) {
8075
matcher.describeMismatch(item, mismatchDescription);
8176
}
8277
}
83-
84-
/**
85-
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
86-
* examined {@link Iterable} yields a series of items, each logically equal to the
87-
* corresponding item in the specified items. For a positive match, the examined iterable
88-
* must be of the same length as the number of specified items.
89-
* For example:
90-
* <pre>assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))</pre>
91-
*
92-
* @param items
93-
* the items that must equal the items provided by an examined {@link Iterable}
94-
*/
95-
@SafeVarargs
96-
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);
103-
}
104-
105-
/**
106-
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
107-
* examined {@link Iterable} yields a single item that satisfies the specified matcher.
108-
* For a positive match, the examined iterable must only yield one item.
109-
* For example:
110-
* <pre>assertThat(Arrays.asList("foo"), contains(equalTo("foo")))</pre>
111-
*
112-
* @param itemMatcher
113-
* the matcher that must be satisfied by the single item provided by an
114-
* examined {@link Iterable}
115-
*/
116-
@SuppressWarnings("unchecked")
117-
public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super E> itemMatcher) {
118-
return contains(new ArrayList<Matcher<? super E>>(asList(itemMatcher)));
119-
}
120-
121-
/**
122-
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
123-
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
124-
* matcher in the specified matchers. For a positive match, the examined iterable
125-
* must be of the same length as the number of specified matchers.
126-
* For example:
127-
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))</pre>
128-
*
129-
* @param itemMatchers
130-
* the matchers that must be satisfied by the items provided by an examined {@link Iterable}
131-
*/
132-
@SafeVarargs
133-
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers) {
134-
// required for JDK 1.6
135-
//noinspection RedundantTypeArguments
136-
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = Wrapping.<E>nullSafe(itemMatchers);
137-
return contains(nullSafeWithExplicitTypeMatchers);
138-
}
139-
140-
/**
141-
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
142-
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
143-
* matcher in the specified list of matchers. For a positive match, the examined iterable
144-
* must be of the same length as the specified list of matchers.
145-
* For example:
146-
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
147-
*
148-
* @param itemMatchers
149-
* a list of matchers, each of which must be satisfied by the corresponding item provided by
150-
* an examined {@link Iterable}
151-
*/
152-
public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) {
153-
return new IsIterableContainingInOrder<>(itemMatchers);
154-
}
15578
}

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

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

33
import org.hamcrest.Matcher;
4+
import org.hamcrest.internal.Wrapping;
45

6+
import java.util.ArrayList;
57
import java.util.Collection;
8+
import java.util.List;
69

10+
import static java.util.Arrays.asList;
711
import static org.hamcrest.object.MatchObjects.equalTo;
812

913
/**
@@ -56,4 +60,68 @@ public static <E> Matcher<Collection<? extends E>> empty() {
5660
public static <E> Matcher<Collection<E>> emptyCollectionOf(Class<E> unusedToForceReturnType) {
5761
return (Matcher)empty();
5862
}
63+
64+
/**
65+
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
66+
* examined {@link Iterable} yields a series of items, each logically equal to the
67+
* corresponding item in the specified items. For a positive match, the examined iterable
68+
* must be of the same length as the number of specified items.
69+
* For example:
70+
* <pre>assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))</pre>
71+
*
72+
* @param items
73+
* the items that must equal the items provided by an examined {@link Iterable}
74+
*/
75+
@SafeVarargs
76+
public static <E> Matcher<Iterable<? extends E>> contains(E... items) {
77+
return contains(Wrapping.asEqualToMatchers(items));
78+
}
79+
80+
/**
81+
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
82+
* examined {@link Iterable} yields a single item that satisfies the specified matcher.
83+
* For a positive match, the examined iterable must only yield one item.
84+
* For example:
85+
* <pre>assertThat(Arrays.asList("foo"), contains(equalTo("foo")))</pre>
86+
*
87+
* @param itemMatcher
88+
* the matcher that must be satisfied by the single item provided by an
89+
* examined {@link Iterable}
90+
*/
91+
@SuppressWarnings("unchecked")
92+
public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super E> itemMatcher) {
93+
return contains(new ArrayList<Matcher<? super E>>(asList(itemMatcher)));
94+
}
95+
96+
/**
97+
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
98+
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
99+
* matcher in the specified matchers. For a positive match, the examined iterable
100+
* must be of the same length as the number of specified matchers.
101+
* For example:
102+
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))</pre>
103+
*
104+
* @param itemMatchers
105+
* the matchers that must be satisfied by the items provided by an examined {@link Iterable}
106+
*/
107+
@SafeVarargs
108+
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers) {
109+
return contains(Wrapping.nullSafe(itemMatchers));
110+
}
111+
112+
/**
113+
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
114+
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
115+
* matcher in the specified list of matchers. For a positive match, the examined iterable
116+
* must be of the same length as the specified list of matchers.
117+
* For example:
118+
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
119+
*
120+
* @param itemMatchers
121+
* a list of matchers, each of which must be satisfied by the corresponding item provided by
122+
* an examined {@link Iterable}
123+
*/
124+
public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) {
125+
return new IsIterableContainingInOrder<>(itemMatchers);
126+
}
59127
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.List;
99

1010
import static java.util.Arrays.asList;
11-
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
11+
import static org.hamcrest.collection.MatchIterables.contains;
1212
import static org.hamcrest.core.IsEqual.equalTo;
1313

1414
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)