Skip to content

Commit 5f9f9e3

Browse files
author
joeretro
committed
Started work on Java5ifying matchers. Done the 'collections' package.
1 parent 00b079e commit 5f9f9e3

23 files changed

Lines changed: 259 additions & 268 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.hamcrest;
2+
3+
public @interface Factory {
4+
}

‎src/library/org/hamcrest/Matchers.java‎

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
*/
33
package org.hamcrest;
44

5-
import org.hamcrest.collection.IsArrayContaining;
6-
import org.hamcrest.collection.IsCollectionContaining;
7-
import org.hamcrest.collection.IsMapContaining;
85
import org.hamcrest.core.And;
96
import org.hamcrest.core.IsAnything;
107
import org.hamcrest.core.IsCompatibleType;
@@ -146,80 +143,6 @@ public static IsIn isIn(Object[] array) {
146143
return new IsIn(array);
147144
}
148145

149-
public static IsCollectionContaining collectionContaining(Matcher elementMatcher) {
150-
return new IsCollectionContaining(elementMatcher);
151-
}
152-
153-
public static IsCollectionContaining collectionContaining(Object element) {
154-
return collectionContaining(eq(element));
155-
}
156-
157-
public static IsArrayContaining arrayContaining(Matcher elementMatcher) {
158-
return new IsArrayContaining(elementMatcher);
159-
}
160-
161-
public static IsArrayContaining arrayContaining(Object element) {
162-
return arrayContaining(eq(element));
163-
}
164-
165-
public static IsArrayContaining arrayContaining(boolean element) {
166-
// Avoid newing Boolean objects.
167-
// Boolean.valueOf() not available on JDK 1.3.
168-
return arrayContaining(element ? Boolean.TRUE : Boolean.FALSE);
169-
}
170-
171-
public static IsArrayContaining arrayContaining(byte element) {
172-
return arrayContaining(new Byte(element));
173-
}
174-
175-
public static IsArrayContaining arrayContaining(short element) {
176-
return arrayContaining(new Short(element));
177-
}
178-
179-
public static IsArrayContaining arrayContaining(char element) {
180-
return arrayContaining(new Character(element));
181-
}
182-
183-
public static IsArrayContaining arrayContaining(int element) {
184-
return arrayContaining(new Integer(element));
185-
}
186-
187-
public static IsArrayContaining arrayContaining(long element) {
188-
return arrayContaining(new Long(element));
189-
}
190-
191-
public static IsArrayContaining arrayContaining(float element) {
192-
return arrayContaining(new Float(element));
193-
}
194-
195-
public static IsArrayContaining arrayContaining(double element) {
196-
return arrayContaining(new Double(element));
197-
}
198-
199-
public static IsMapContaining mapContaining(Matcher keyMatcher, Matcher valueMatcher) {
200-
return new IsMapContaining(keyMatcher, valueMatcher);
201-
}
202-
203-
public static IsMapContaining mapContaining(Object key, Object value) {
204-
return mapContaining(eq(key), eq(value));
205-
}
206-
207-
public static IsMapContaining mapWithKey(Object key) {
208-
return mapWithKey(eq(key));
209-
}
210-
211-
public static IsMapContaining mapWithKey(Matcher keyMatcher) {
212-
return new IsMapContaining(keyMatcher, ANYTHING);
213-
}
214-
215-
public static IsMapContaining mapWithValue(Object value) {
216-
return mapWithValue(eq(value));
217-
}
218-
219-
public static IsMapContaining mapWithValue(Matcher valueMatcher) {
220-
return new IsMapContaining(ANYTHING, valueMatcher);
221-
}
222-
223146
/**
224147
* Temporary Matcher... don't use this.. it will go away!
225148
*/

‎src/library/org/hamcrest/collection/IsArrayContaining.java‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22

33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
5+
import org.hamcrest.Factory;
6+
import static org.hamcrest.core.IsEqual.eq;
57

68
import java.lang.reflect.Array;
79

8-
public class IsArrayContaining implements Matcher {
9-
private final Matcher elementMatcher;
10+
public class IsArrayContaining<T> implements Matcher<T[]> {
1011

11-
public IsArrayContaining(Matcher elementMatcher) {
12-
this.elementMatcher = elementMatcher;
13-
}
12+
private final Matcher<T> elementMatcher;
1413

15-
public boolean match(Object o) {
16-
return o != null
17-
&& o.getClass().isArray()
18-
&& arrayContainsMatchingElement(o);
14+
public IsArrayContaining(Matcher<T> elementMatcher) {
15+
this.elementMatcher = elementMatcher;
1916
}
2017

21-
private boolean arrayContainsMatchingElement(Object array) {
18+
@SuppressWarnings({"unchecked"})
19+
public boolean match(T[] array) {
20+
if (array == null) {
21+
return false;
22+
}
2223
int size = Array.getLength(array);
2324
for (int i = 0; i < size; i++) {
24-
if (elementMatcher.match(Array.get(array, i))) return true;
25+
if (elementMatcher.match((T)Array.get(array, i))) {
26+
return true;
27+
}
2528
}
2629
return false;
2730
}
@@ -30,4 +33,15 @@ public void describeTo(Description description) {
3033
description.appendText("an array containing ");
3134
elementMatcher.describeTo(description);
3235
}
36+
37+
@Factory
38+
public static <T> Matcher<T[]> arrayContaining(Matcher<T> elementMatcher) {
39+
return new IsArrayContaining<T>(elementMatcher);
40+
}
41+
42+
@Factory
43+
public static <T> Matcher<T[]> arrayContaining(T element) {
44+
return arrayContaining(eq(element));
45+
}
46+
3347
}

‎src/library/org/hamcrest/collection/IsCollectionContaining.java‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
5+
import org.hamcrest.Factory;
6+
import static org.hamcrest.core.IsEqual.eq;
57

68
import java.util.Collection;
7-
import java.util.Iterator;
89

9-
public class IsCollectionContaining implements Matcher {
10-
private final Matcher elementMatcher;
10+
public class IsCollectionContaining<T> implements Matcher<Collection<T>> {
11+
private final Matcher<T> elementMatcher;
12+
//Comparator<? super T> c
13+
//int binarySearch(List<? extends Comparable<? super T>> list, T key) {
1114

12-
public IsCollectionContaining(Matcher elementMatcher) {
15+
public IsCollectionContaining(Matcher<T> elementMatcher) {
1316
this.elementMatcher = elementMatcher;
1417
}
1518

16-
public boolean match(Object o) {
17-
return o != null
18-
&& o instanceof Collection
19-
&& collectionContainsMatchingElement((Collection) o);
20-
}
21-
22-
private boolean collectionContainsMatchingElement(Collection collection) {
23-
Iterator i = collection.iterator();
24-
while (i.hasNext()) {
25-
if (elementMatcher.match(i.next())) return true;
19+
public boolean match(Collection<T> collection) {
20+
if (collection == null) {
21+
return false;
22+
}
23+
for (T item : collection) {
24+
if (elementMatcher.match(item)){
25+
return true;
26+
}
2627
}
2728
return false;
2829
}
@@ -31,4 +32,15 @@ public void describeTo(Description description) {
3132
description.appendText("a collection containing ");
3233
elementMatcher.describeTo(description);
3334
}
35+
36+
@Factory
37+
public static <T> Matcher<Collection<T>> collectionContaining(Matcher<T> elementMatcher) {
38+
return new IsCollectionContaining<T>(elementMatcher);
39+
}
40+
41+
@Factory
42+
public static <T> Matcher<Collection<T>> collectionContaining(T element) {
43+
return collectionContaining(eq(element));
44+
}
45+
3446
}
Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
package org.hamcrest.collection;
22

33
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
45
import org.hamcrest.Matcher;
6+
import static org.hamcrest.core.IsAnything.anything;
7+
import static org.hamcrest.core.IsEqual.eq;
58

6-
import java.util.Iterator;
79
import java.util.Map;
810
import java.util.Map.Entry;
911

10-
public class IsMapContaining implements Matcher {
11-
private final Matcher keyMatcher;
12-
private final Matcher valueMatcher;
12+
public class IsMapContaining<K,V> implements Matcher<Map<K, V>> {
1313

14-
public IsMapContaining(Matcher keyMatcher, Matcher valueMatcher) {
14+
private final Matcher<K> keyMatcher;
15+
private final Matcher<V> valueMatcher;
16+
17+
public IsMapContaining(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
1518
this.keyMatcher = keyMatcher;
1619
this.valueMatcher = valueMatcher;
1720
}
1821

19-
public boolean match(Object o) {
20-
if (o != null && o instanceof Map) {
21-
Map map = (Map) o;
22-
23-
for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
24-
Map.Entry entry = (Entry) i.next();
25-
26-
if (keyMatcher.match(entry.getKey()) && valueMatcher.match(entry.getValue())) {
27-
return true;
28-
}
22+
public boolean match(Map<K, V> map) {
23+
if (map == null) {
24+
return false;
25+
}
26+
for (Entry<K, V> entry : map.entrySet()) {
27+
if (keyMatcher.match(entry.getKey()) && valueMatcher.match(entry.getValue())) {
28+
return true;
2929
}
3030
}
31-
3231
return false;
3332
}
3433

@@ -39,4 +38,35 @@ public void describeTo(Description description) {
3938
valueMatcher.describeTo(description);
4039
description.appendText("]");
4140
}
41+
42+
@Factory
43+
public static <K,V> Matcher<Map<K,V>> mapContaining(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
44+
return new IsMapContaining<K,V>(keyMatcher, valueMatcher);
45+
}
46+
47+
@Factory
48+
public static <K,V> Matcher<Map<K,V>> mapContaining(K key, V value) {
49+
return mapContaining(eq(key), eq(value));
50+
}
51+
52+
@Factory
53+
public static <K,V> Matcher<Map<K,V>> mapWithKey(Matcher<K> keyMatcher) {
54+
return mapContaining(keyMatcher, (Matcher<V>)anything());
55+
}
56+
57+
@Factory
58+
public static <K,V> Matcher<Map<K,V>> mapWithKey(K key) {
59+
return mapWithKey(eq(key));
60+
}
61+
62+
@Factory
63+
public static <K,V> Matcher<Map<K,V>> mapWithValue(Matcher<V> valueMatcher) {
64+
return mapContaining((Matcher<K>)anything(), valueMatcher);
65+
}
66+
67+
@Factory
68+
public static <K,V> Matcher<Map<K,V>> mapWithValue(V value) {
69+
return mapWithValue(eq(value));
70+
}
71+
4272
}

‎src/library/org/hamcrest/core/And.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
* shortcut, so that the second matcher is not called if the first
1212
* matcher returns <code>false</code>.
1313
*/
14-
public class And implements Matcher {
15-
private final Matcher left;
16-
private final Matcher right;
14+
public class And<T> implements Matcher<T> {
15+
private final Matcher<T> left;
16+
private final Matcher<T> right;
1717

18-
public And(Matcher left, Matcher right) {
18+
public And(Matcher<T> left, Matcher<T> right) {
1919
this.left = left;
2020
this.right = right;
2121
}
2222

23-
public boolean match(Object o) {
23+
public boolean match(T o) {
2424
return left.match(o) && right.match(o);
2525
}
2626

‎src/library/org/hamcrest/core/DescribedAs.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
/**
99
* Provides a custom description to another matcher.
1010
*/
11-
public class DescribedAs implements Matcher {
11+
public class DescribedAs<T> implements Matcher<T> {
1212

1313
private final String description;
14-
private final Matcher matcher;
14+
private final Matcher<T> matcher;
1515

16-
public DescribedAs(String description, Matcher matcher) {
16+
public DescribedAs(String description, Matcher<T> matcher) {
1717
this.description = description;
1818
this.matcher = matcher;
1919
}
2020

21-
public boolean match(Object o) {
21+
public boolean match(T o) {
2222
return matcher.match(o);
2323
}
2424

‎src/library/org/hamcrest/core/IsAnything.java‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
1011
* A matcher that always returns <code>true</code>.
1112
*/
12-
public class IsAnything implements Matcher {
13-
public static final IsAnything INSTANCE = new IsAnything();
13+
public class IsAnything<T> implements Matcher<T> {
14+
1415
private final String description;
1516

1617
public IsAnything() {
@@ -21,11 +22,21 @@ public IsAnything(String description) {
2122
this.description = description;
2223
}
2324

24-
public boolean match(Object o) {
25+
public boolean match(T o) {
2526
return true;
2627
}
2728

2829
public void describeTo(Description description) {
2930
description.appendText(this.description);
3031
}
32+
33+
@Factory
34+
public static <T> Matcher<T> anything() {
35+
return new IsAnything<T>();
36+
}
37+
38+
@Factory
39+
public static <T> Matcher<T> anything(String description) {
40+
return new IsAnything<T>(description);
41+
}
3142
}

0 commit comments

Comments
 (0)