Skip to content

Commit 8fba4a2

Browse files
committed
Moving Array static methods to ArrayMatching
Cleanups (templating etc) for Java 7
1 parent 8174fc8 commit 8fba4a2

File tree

10 files changed

+223
-205
lines changed

10 files changed

+223
-205
lines changed

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

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

3+
import org.hamcrest.collection.ArrayMatching;
4+
35
@SuppressWarnings("unused")
46
public class Matchers {
57

@@ -560,7 +562,7 @@ public static <T> org.hamcrest.collection.IsArray<T> array(org.hamcrest.Matcher<
560562
* the matcher to apply to elements in examined arrays
561563
*/
562564
public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(org.hamcrest.Matcher<? super T> elementMatcher) {
563-
return org.hamcrest.collection.IsArrayContaining.hasItemInArray(elementMatcher);
565+
return ArrayMatching.hasItemInArray(elementMatcher);
564566
}
565567

566568
/**
@@ -574,7 +576,7 @@ public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(org.hamcrest.Matcher<
574576
* the element that should be present in examined arrays
575577
*/
576578
public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(T element) {
577-
return org.hamcrest.collection.IsArrayContaining.hasItemInArray(element);
579+
return ArrayMatching.hasItemInArray(element);
578580
}
579581

580582
/**
@@ -589,7 +591,7 @@ public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(T element) {
589591
*/
590592
@SafeVarargs
591593
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) {
592-
return org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining(items);
594+
return ArrayMatching.arrayContaining(items);
593595
}
594596

595597
/**
@@ -604,7 +606,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) {
604606
*/
605607
@SafeVarargs
606608
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher<? super E>... itemMatchers) {
607-
return org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining(itemMatchers);
609+
return ArrayMatching.arrayContaining(itemMatchers);
608610
}
609611

610612
/**
@@ -618,7 +620,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher
618620
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
619621
*/
620622
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {
621-
return org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining(itemMatchers);
623+
return ArrayMatching.arrayContaining(itemMatchers);
622624
}
623625

624626
/**
@@ -643,7 +645,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.h
643645
*/
644646
@SafeVarargs
645647
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>... itemMatchers) {
646-
return org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(itemMatchers);
648+
return ArrayMatching.arrayContainingInAnyOrder(itemMatchers);
647649
}
648650

649651
/**
@@ -667,7 +669,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcre
667669
* a list of matchers, each of which must be satisfied by an item provided by an examined array
668670
*/
669671
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers) {
670-
return org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(itemMatchers);
672+
return ArrayMatching.arrayContainingInAnyOrder(itemMatchers);
671673
}
672674

673675
/**
@@ -690,7 +692,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.
690692
*/
691693
@SafeVarargs
692694
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(E... items) {
693-
return org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(items);
695+
return ArrayMatching.arrayContainingInAnyOrder(items);
694696
}
695697

696698
/**
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.Matcher;
4+
import org.hamcrest.internal.NullSafety;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.List;
9+
10+
import static java.util.Arrays.asList;
11+
import static org.hamcrest.core.IsEqual.equalTo;
12+
13+
/**
14+
* @author Steve Freeman 2016 http://www.hamcrest.com
15+
* Collected helper code for converting matchers between lists and iterables.
16+
*/
17+
public class ArrayMatching {
18+
19+
20+
/**
21+
* Creates a matcher for arrays that matches when the examined array contains at least one item
22+
* that is matched by the specified <code>elementMatcher</code>. Whilst matching, the traversal
23+
* of the examined array will stop as soon as a matching element is found.
24+
* For example:
25+
* <pre>assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))</pre>
26+
*
27+
* @param elementMatcher
28+
* the matcher to apply to elements in examined arrays
29+
*/
30+
public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher) {
31+
return new IsArrayContaining<>(elementMatcher);
32+
}
33+
34+
/**
35+
* A shortcut to the frequently used <code>hasItemInArray(equalTo(x))</code>.
36+
* For example:
37+
* <pre>assertThat(hasItemInArray(x))</pre>
38+
* instead of:
39+
* <pre>assertThat(hasItemInArray(equalTo(x)))</pre>
40+
*
41+
* @param element
42+
* the element that should be present in examined arrays
43+
*/
44+
public static <T> Matcher<T[]> hasItemInArray(T element) {
45+
Matcher<? super T> matcher = equalTo(element);
46+
return hasItemInArray(matcher);
47+
}
48+
49+
/**
50+
* <p>
51+
* Creates an order agnostic matcher for arrays that matches when each item in the
52+
* examined array satisfies one matcher anywhere in the specified matchers.
53+
* For a positive match, the examined array must be of the same length as the number of
54+
* specified matchers.
55+
* </p>
56+
* <p>
57+
* N.B. each of the specified matchers will only be used once during a given examination, so be
58+
* careful when specifying matchers that may be satisfied by more than one entry in an examined
59+
* array.
60+
* </p>
61+
* <p>
62+
* For example:
63+
* </p>
64+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))</pre>
65+
*
66+
* @param itemMatchers
67+
* a list of matchers, each of which must be satisfied by an entry in an examined array
68+
*/
69+
@SafeVarargs
70+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers) {
71+
return arrayContainingInAnyOrder(asList(itemMatchers));
72+
}
73+
74+
/**
75+
* <p>
76+
* Creates an order agnostic matcher for arrays that matches when each item in the
77+
* examined array satisfies one matcher anywhere in the specified collection of matchers.
78+
* For a positive match, the examined array must be of the same length as the specified collection
79+
* of matchers.
80+
* </p>
81+
* <p>
82+
* N.B. each matcher in the specified collection will only be used once during a given
83+
* examination, so be careful when specifying matchers that may be satisfied by more than
84+
* one entry in an examined array.
85+
* </p>
86+
* <p>
87+
* For example:
88+
* </p>
89+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre>
90+
*
91+
* @param itemMatchers
92+
* a list of matchers, each of which must be satisfied by an item provided by an examined array
93+
*/
94+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> itemMatchers) {
95+
return new IsArrayContainingInAnyOrder<>(itemMatchers);
96+
}
97+
98+
/**
99+
* <p>Creates an order agnostic matcher for arrays that matches when each item in the
100+
* examined array is logically equal to one item anywhere in the specified items.
101+
* For a positive match, the examined array must be of the same length as the number of
102+
* specified items.
103+
* </p>
104+
* <p>N.B. each of the specified items will only be used once during a given examination, so be
105+
* careful when specifying items that may be equal to more than one entry in an examined
106+
* array.
107+
* </p>
108+
* <p>
109+
* For example:
110+
* </p>
111+
* <pre>assertThat(new String[]{"foo", "bar"}, containsInAnyOrder("bar", "foo"))</pre>
112+
*
113+
* @param items
114+
* the items that must equal the entries of an examined array, in any order
115+
*/
116+
@SafeVarargs
117+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) {
118+
List<Matcher<? super E>> matchers = new ArrayList<>();
119+
for (E item : items) {
120+
matchers.add(equalTo(item));
121+
}
122+
return new IsArrayContainingInAnyOrder<>(matchers);
123+
}
124+
125+
/**
126+
* Creates a matcher for arrays that matches when each item in the examined array is
127+
* logically equal to the corresponding item in the specified items. For a positive match,
128+
* the examined array must be of the same length as the number of specified items.
129+
* For example:
130+
* <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre>
131+
*
132+
* @param items
133+
* the items that must equal the items within an examined array
134+
*/
135+
@SafeVarargs
136+
public static <E> Matcher<E[]> arrayContaining(E... items) {
137+
List<Matcher<? super E>> matchers = new ArrayList<>();
138+
for (E item : items) {
139+
matchers.add(equalTo(item));
140+
}
141+
return arrayContaining(matchers);
142+
}
143+
144+
/**
145+
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
146+
* corresponding matcher in the specified matchers. For a positive match, the examined array
147+
* must be of the same length as the number of specified matchers.
148+
* For example:
149+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")))</pre>
150+
*
151+
* @param itemMatchers
152+
* the matchers that must be satisfied by the items in the examined array
153+
*/
154+
@SafeVarargs
155+
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
156+
//required for JDK 1.6
157+
//noinspection RedundantTypeArguments
158+
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = NullSafety.<E>nullSafe(itemMatchers);
159+
160+
return arrayContaining(nullSafeWithExplicitTypeMatchers);
161+
}
162+
163+
/**
164+
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
165+
* corresponding matcher in the specified list of matchers. For a positive match, the examined array
166+
* must be of the same length as the specified list of matchers.
167+
* For example:
168+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
169+
*
170+
* @param itemMatchers
171+
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
172+
*/
173+
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
174+
return new IsArrayContainingInOrder<>(itemMatchers);
175+
}
176+
}

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import java.util.Arrays;
88

9-
import static org.hamcrest.core.IsEqual.equalTo;
10-
119
/**
1210
* Matches if an array contains an item satisfying a nested matcher.
1311
*/
@@ -40,32 +38,4 @@ public void describeTo(Description description) {
4038
.appendDescriptionOf(elementMatcher);
4139
}
4240

43-
/**
44-
* Creates a matcher for arrays that matches when the examined array contains at least one item
45-
* that is matched by the specified <code>elementMatcher</code>. Whilst matching, the traversal
46-
* of the examined array will stop as soon as a matching element is found.
47-
* For example:
48-
* <pre>assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))</pre>
49-
*
50-
* @param elementMatcher
51-
* the matcher to apply to elements in examined arrays
52-
*/
53-
public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher) {
54-
return new IsArrayContaining<>(elementMatcher);
55-
}
56-
57-
/**
58-
* A shortcut to the frequently used <code>hasItemInArray(equalTo(x))</code>.
59-
* For example:
60-
* <pre>assertThat(hasItemInArray(x))</pre>
61-
* instead of:
62-
* <pre>assertThat(hasItemInArray(equalTo(x)))</pre>
63-
*
64-
* @param element
65-
* the element that should be present in examined arrays
66-
*/
67-
public static <T> Matcher<T[]> hasItemInArray(T element) {
68-
Matcher<? super T> matcher = equalTo(element);
69-
return IsArrayContaining.hasItemInArray(matcher);
70-
}
7141
}

0 commit comments

Comments
 (0)