Skip to content

Commit a7fa9d8

Browse files
committed
Collapsing factory methods
1 parent 8daec02 commit a7fa9d8

File tree

4 files changed

+11
-63
lines changed

4 files changed

+11
-63
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(T element) {
526526
*/
527527
@SafeVarargs
528528
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) {
529-
return org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining(items);
529+
return MatchArrays.arrayContaining(items);
530530
}
531531

532532
/**
@@ -540,7 +540,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) {
540540
*/
541541
@SafeVarargs
542542
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher<? super E>... itemMatchers) {
543-
return org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining(itemMatchers);
543+
return MatchArrays.arrayContaining(itemMatchers);
544544
}
545545

546546
/**
@@ -553,7 +553,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher
553553
* @param itemMatchers a list of matchers, each of which must be satisfied by the corresponding item in an examined array
554554
*/
555555
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {
556-
return org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining(itemMatchers);
556+
return MatchArrays.arrayContaining(itemMatchers);
557557
}
558558

559559
/**

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

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

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

1210
import static java.util.Arrays.asList;
13-
import static org.hamcrest.core.IsEqual.equalTo;
1411

1512
public class IsArrayContainingInOrder<E> extends TypeSafeMatcher<E[]> {
1613
private final Collection<Matcher<? super E>> matchers;
@@ -35,53 +32,4 @@ public void describeMismatchSafely(E[] item, Description mismatchDescription) {
3532
public void describeTo(Description description) {
3633
description.appendList("[", ", ", "]", matchers);
3734
}
38-
39-
/**
40-
* Creates a matcher for arrays that matches when each item in the examined array is
41-
* logically equal to the corresponding item in the specified items. For a positive match,
42-
* the examined array must be of the same length as the number of specified items.
43-
* For example:
44-
* <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre>
45-
*
46-
* @param items
47-
* the items that must equal the items within an examined array
48-
*/
49-
@SafeVarargs
50-
public static <E> Matcher<E[]> arrayContaining(E... items) {
51-
List<Matcher<? super E>> matchers = new ArrayList<>();
52-
for (E item : items) {
53-
matchers.add(equalTo(item));
54-
}
55-
return arrayContaining(matchers);
56-
}
57-
58-
/**
59-
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
60-
* corresponding matcher in the specified matchers. For a positive match, the examined array
61-
* must be of the same length as the number of specified matchers.
62-
* For example:
63-
* <pre>assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))</pre>
64-
*
65-
* @param itemMatchers
66-
* the matchers that must be satisfied by the items in the examined array
67-
*/
68-
@SafeVarargs
69-
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
70-
return arrayContaining(Wrapping.nullSafe(itemMatchers));
71-
}
72-
73-
/**
74-
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
75-
* corresponding matcher in the specified list of matchers. For a positive match, the examined array
76-
* must be of the same length as the specified list of matchers.
77-
* For example:
78-
* <pre>assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
79-
*
80-
* @param itemMatchers
81-
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
82-
*/
83-
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
84-
return new IsArrayContainingInOrder<>(itemMatchers);
85-
}
86-
8735
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.hamcrest.FunctionMatcher;
44
import org.hamcrest.FunctionMatcher.Feature;
55
import org.hamcrest.Matcher;
6+
import org.hamcrest.internal.Wrapping;
67

78
import java.util.Collection;
9+
import java.util.List;
810

911
import static org.hamcrest.core.DescribedAs.describedAs;
1012
import static org.hamcrest.core.IsEqual.equalTo;
@@ -61,7 +63,7 @@ public static <T> Matcher<T[]> hasItemInArray(T element) {
6163
*/
6264
@SafeVarargs
6365
public static <E> Matcher<E[]> arrayContaining(E... items) {
64-
return IsArrayContainingInOrder.arrayContaining(items);
66+
return arrayContaining(Wrapping.asEqualToMatchers(items));
6567
}
6668

6769
/**
@@ -75,7 +77,7 @@ public static <E> Matcher<E[]> arrayContaining(E... items) {
7577
*/
7678
@SafeVarargs
7779
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
78-
return IsArrayContainingInOrder.arrayContaining(itemMatchers);
80+
return arrayContaining(Wrapping.nullSafe(itemMatchers));
7981
}
8082

8183
/**
@@ -87,8 +89,8 @@ public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatcher
8789
*
8890
* @param itemMatchers a list of matchers, each of which must be satisfied by the corresponding item in an examined array
8991
*/
90-
public static <E> Matcher<E[]> arrayContaining(java.util.List<Matcher<? super E>> itemMatchers) {
91-
return IsArrayContainingInOrder.arrayContaining(itemMatchers);
92+
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
93+
return new IsArrayContainingInOrder<>(itemMatchers);
9294
}
9395

9496
/**
@@ -172,9 +174,7 @@ public static <E> Matcher<E[]> arrayWithSize(Matcher<? super Integer> sizeMatche
172174
return new FunctionMatcher<>(
173175
"an array with size", "array size",
174176
sizeMatcher,
175-
new Feature<E[], Integer>() {
176-
@Override public Integer from(E[] actual) { return actual.length; }
177-
});
177+
new Feature<E[], Integer>() { @Override public Integer from(E[] actual) { return actual.length; } });
178178
}
179179

180180
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
55

6-
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
6+
import static org.hamcrest.collection.MatchArrays.arrayContaining;
77
import static org.hamcrest.core.IsEqual.equalTo;
88

99
public class IsArrayContainingInOrderTest extends AbstractMatcherTest {

0 commit comments

Comments
 (0)