Skip to content

Commit 645526d

Browse files
author
nat.pryce
committed
Made AllOf and AnyOf be composed of Matcher<? super T>. Fixed lots of compile errors that resulted because the Java 5 type system doesn't work. Did a bit of refactoring of the ordering comparisons, deprecated GreaterThan as a result.
1 parent 378a9d5 commit 645526d

14 files changed

Lines changed: 163 additions & 159 deletions

File tree

‎.classpath‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
<classpathentry kind="lib" path="lib/integration/jmock-1.10RC1.jar"/>
1414
<classpathentry kind="lib" path="lib/integration/junit-4.0.jar"/>
1515
<classpathentry kind="lib" path="lib/integration/testng-4.6-jdk15.jar"/>
16-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.5.0_08"/>
1717
<classpathentry kind="output" path="classes"/>
1818
</classpath>

‎build.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@
7070
<target name="library"
7171
depends="core,generator"
7272
description="Build library of matchers">
73+
<mkdir dir="build/temp/hamcrest-library/generated-code"/>
7374
<java-to-jar srcdir="hamcrest-library/src/main/java"
7475
destjar="build/hamcrest-library-${version}.jar"
7576
classpath="build/hamcrest-core-${version}.jar"/>
7677
<!-- Generate one class with all static imports -->
77-
<mkdir dir="build/temp/hamcrest-library/generated-code"/>
7878
<java classname="org.hamcrest.generator.config.XmlConfigurator"
7979
fork="yes"
8080
failonerror="yes"
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
11
package org.hamcrest.core;
22

3-
import org.hamcrest.BaseMatcher;
4-
import org.hamcrest.Matcher;
5-
import org.hamcrest.Description;
6-
import org.hamcrest.Factory;
7-
import org.hamcrest.introspection.Combination;
8-
93
import java.util.Arrays;
104

5+
import org.hamcrest.Factory;
6+
import org.hamcrest.Matcher;
7+
118
/**
129
* Calculates the logical conjunction of two matchers. Evaluation is
1310
* shortcut, so that the second matcher is not called if the first
1411
* matcher returns <code>false</code>.
1512
*/
16-
public class AllOf<T> extends BaseMatcher<T> implements Combination {
17-
private final Iterable<? extends Matcher<? extends T>> matchers;
18-
19-
public AllOf(Iterable<? extends Matcher<? extends T>> matchers) {
20-
this.matchers = matchers;
13+
public class AllOf<T> extends ShortcutCombination<T> {
14+
public AllOf(Iterable<Matcher<? super T>> matchers) {
15+
super(matchers);
2116
}
2217

23-
public boolean matches(Object o) {
24-
for (Matcher<? extends T> matcher : matchers) {
25-
if (!matcher.matches(o)) {
26-
return false;
27-
}
28-
}
29-
return true;
30-
}
31-
32-
public void describeTo(Description description) {
33-
description.appendList("(", " and ", ")", matchers);
18+
@Override
19+
protected boolean shortcut() {
20+
return false;
3421
}
3522

36-
public Iterable<? extends Matcher<?>> combined() {
37-
return matchers;
38-
}
39-
4023
/**
4124
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
4225
*/
4326
@Factory
44-
public static <T> Matcher<T> allOf(Matcher<? extends T>... matchers) {
27+
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
4528
return allOf(Arrays.asList(matchers));
4629
}
4730

4831
/**
4932
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
5033
*/
5134
@Factory
52-
public static <T> Matcher<T> allOf(Iterable<? extends Matcher<? extends T>> matchers) {
35+
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
5336
return new AllOf<T>(matchers);
5437
}
5538
}

‎hamcrest-core/src/main/java/org/hamcrest/core/AnyOf.java‎

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,38 @@
22

33
import java.util.Arrays;
44

5-
import org.hamcrest.BaseMatcher;
6-
import org.hamcrest.Description;
75
import org.hamcrest.Factory;
86
import org.hamcrest.Matcher;
9-
import org.hamcrest.introspection.Combination;
107

118
/**
129
* Calculates the logical disjunction of two matchers. Evaluation is
1310
* shortcut, so that the second matcher is not called if the first
1411
* matcher returns <code>true</code>.
1512
*/
16-
public class AnyOf<T> extends BaseMatcher<T> implements Combination {
17-
private final Iterable<? extends Matcher<? extends T>> matchers;
18-
19-
public AnyOf(Iterable<? extends Matcher<? extends T>> matchers) {
20-
this.matchers = matchers;
13+
public class AnyOf<T> extends ShortcutCombination<T> {
14+
public AnyOf(Iterable<Matcher<? super T>> matchers) {
15+
super(matchers);
2116
}
2217

23-
public boolean matches(Object o) {
24-
for (Matcher<? extends T> matcher : matchers) {
25-
if (matcher.matches(o)) {
26-
return true;
27-
}
28-
}
29-
return false;
30-
}
31-
32-
public void describeTo(Description description) {
33-
description.appendList("(", " or ", ")", matchers);
18+
@Override
19+
protected boolean shortcut() {
20+
return true;
3421
}
3522

36-
public Iterable<? extends Matcher<?>> combined() {
37-
return matchers;
38-
}
39-
4023
/**
4124
* Evaluates to true if ANY of the passed in matchers evaluate to true.
4225
*/
4326
@Factory
44-
public static <T> Matcher<T> anyOf(Matcher<? extends T>... matchers) {
27+
public static <T> Matcher<T> anyOf(Matcher<? super T>... matchers) {
4528
return anyOf(Arrays.asList(matchers));
4629
}
4730

4831
/**
4932
* Evaluates to true if ANY of the passed in matchers evaluate to true.
5033
*/
5134
@Factory
52-
public static <T> Matcher<T> anyOf(Iterable<? extends Matcher<? extends T>> matchers) {
35+
public static <T> Matcher<T> anyOf(Iterable<Matcher<? super T>> matchers) {
5336
return new AnyOf<T>(matchers);
5437
}
38+
5539
}

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayOnlyContaining.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static org.hamcrest.core.IsEqual.equalTo;
55

66
import java.util.ArrayList;
7-
import java.util.Collection;
7+
import java.util.List;
88

99
import org.hamcrest.Description;
1010
import org.hamcrest.Factory;
@@ -19,9 +19,9 @@
1919
*/
2020
public class IsArrayOnlyContaining<T> extends TypeSafeMatcher<T[]> {
2121

22-
private final Matcher<T> matcher;
22+
private final Matcher<? super T> matcher;
2323

24-
public IsArrayOnlyContaining(Matcher<T> matcher) {
24+
public IsArrayOnlyContaining(Matcher<? super T> matcher) {
2525
this.matcher = matcher;
2626
}
2727

@@ -46,15 +46,15 @@ public void describeTo(Description description) {
4646

4747
@Factory
4848
public static <T> Matcher<T[]> isArrayOnlyContaining(T...items) {
49-
Collection<Matcher<T>> matchers = new ArrayList<Matcher<T>>();
49+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
5050
for (T item : items) {
5151
matchers.add(equalTo(item));
5252
}
5353
return new IsArrayOnlyContaining<T>(anyOf(matchers));
5454
}
5555

5656
@Factory
57-
public static <T> Matcher<T[]> isArrayOnlyContaining(Matcher<T>...matchers) {
57+
public static <T> Matcher<T[]> isArrayOnlyContaining(Matcher<? super T>...matchers) {
5858
return new IsArrayOnlyContaining<T>(anyOf(matchers));
5959
}
6060
}
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package org.hamcrest.collection;
22

33
import static org.hamcrest.core.AllOf.allOf;
4-
import org.hamcrest.Description;
5-
import org.hamcrest.Matcher;
6-
import org.hamcrest.Factory;
7-
import org.hamcrest.TypeSafeMatcher;
84
import static org.hamcrest.core.IsEqual.equalTo;
95

10-
import java.util.Collection;
116
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.hamcrest.Description;
10+
import org.hamcrest.Factory;
11+
import org.hamcrest.Matcher;
12+
import org.hamcrest.TypeSafeMatcher;
1213

1314
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> {
14-
private final Matcher<? extends T> elementMatcher;
15+
private final Matcher<? super T> elementMatcher;
1516

16-
public IsCollectionContaining(Matcher<? extends T> elementMatcher) {
17+
public IsCollectionContaining(Matcher<? super T> elementMatcher) {
1718
this.elementMatcher = elementMatcher;
1819
}
1920

@@ -33,7 +34,7 @@ public void describeTo(Description description) {
3334
}
3435

3536
@Factory
36-
public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) {
37+
public static <T> Matcher<Iterable<T>> hasItem(Matcher<? super T> elementMatcher) {
3738
return new IsCollectionContaining<T>(elementMatcher);
3839
}
3940

@@ -43,23 +44,25 @@ public static <T> Matcher<Iterable<T>> hasItem(T element) {
4344
}
4445

4546
@Factory
46-
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) {
47-
Collection<Matcher<? extends Iterable<T>>> all
48-
= new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length);
49-
for (Matcher<? extends T> elementMatcher : elementMatchers) {
50-
all.add(hasItem(elementMatcher));
47+
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {
48+
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(elementMatchers.length);
49+
50+
for (Matcher<? super T> elementMatcher : elementMatchers) {
51+
Matcher<? super Iterable<T>> itemMatcher = hasItem(elementMatcher);
52+
all.add(itemMatcher);
5153
}
54+
5255
return allOf(all);
5356
}
54-
57+
5558
@Factory
5659
public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
57-
Collection<Matcher<? extends Iterable<T>>> all
58-
= new ArrayList<Matcher<? extends Iterable<T>>>(elements.length);
60+
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(elements.length);
61+
5962
for (T element : elements) {
6063
all.add(hasItem(element));
6164
}
65+
6266
return allOf(all);
6367
}
64-
6568
}

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsCollectionOnlyContaining.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.ArrayList;
77
import java.util.Collection;
8+
import java.util.List;
89

910
import org.hamcrest.Description;
1011
import org.hamcrest.Factory;
@@ -26,9 +27,9 @@
2627
*/
2728
public class IsCollectionOnlyContaining<T> extends TypeSafeMatcher<Collection<T>> {
2829

29-
private final Matcher<T> matcher;
30+
private final Matcher<? super T> matcher;
3031

31-
public IsCollectionOnlyContaining(Matcher<T> matcher) {
32+
public IsCollectionOnlyContaining(Matcher<? super T> matcher) {
3233
this.matcher = matcher;
3334
}
3435

@@ -53,15 +54,15 @@ public void describeTo(Description description) {
5354

5455
@Factory
5556
public static <T> Matcher<Collection<T>> onlyContains(T... items) {
56-
Collection<Matcher<T>> matchers = new ArrayList<Matcher<T>>();
57+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
5758
for (T item : items) {
5859
matchers.add(equalTo(item));
5960
}
6061
return new IsCollectionOnlyContaining<T>(anyOf(matchers));
6162
}
6263

6364
@Factory
64-
public static <T> Matcher<Collection<T>> onlyContains(Matcher<T>... matchers) {
65+
public static <T> Matcher<Collection<T>> onlyContains(Matcher<? super T>... matchers) {
6566
return new IsCollectionOnlyContaining<T>(anyOf(matchers));
6667
}
6768
}

‎hamcrest-library/src/main/java/org/hamcrest/number/IsGreaterThan.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/**
99
* Is the value less than or greater than another {@link java.lang.Comparable} value?
1010
*/
11+
@Deprecated
1112
public class IsGreaterThan<T extends Comparable<T>> extends TypeSafeMatcher<T> {
1213
private final Comparable<T> compareTo;
1314

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
11
package org.hamcrest.number;
22

3-
import static org.hamcrest.core.AnyOf.anyOf;
4-
import static org.hamcrest.core.IsEqual.equalTo;
5-
import static org.hamcrest.core.IsNot.not;
6-
import static org.hamcrest.core.DescribedAs.describedAs;
7-
3+
import org.hamcrest.Description;
84
import org.hamcrest.Factory;
95
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
107

118
@SuppressWarnings("unchecked")
129
public class OrderingComparisons {
10+
public static class OrderingComparison<T extends Comparable> extends TypeSafeMatcher<T> {
11+
private final T value;
12+
private final int minCompare, maxCompare;
13+
14+
public OrderingComparison(T value, int minCompare, int maxCompare) {
15+
this.value = value;
16+
this.minCompare = minCompare;
17+
this.maxCompare = maxCompare;
18+
}
19+
20+
@Override
21+
public boolean matchesSafely(T other) {
22+
int compare = Integer.signum(value.compareTo(other));
23+
return minCompare <= compare && compare <= maxCompare;
24+
}
25+
26+
public void describeTo(Description description) {
27+
description.appendText("a value ")
28+
.appendText(comparison(minCompare));
29+
if (minCompare != maxCompare) {
30+
description.appendText(" or ")
31+
.appendText(comparison(maxCompare));
32+
}
33+
description.appendText(" ")
34+
.appendValue(value);
35+
}
36+
37+
private String comparison(int compare) {
38+
if (compare > 0) {
39+
return "less than";
40+
}
41+
else if (compare == 0) {
42+
return "equal to ";
43+
}
44+
else {
45+
return "greater than";
46+
}
47+
}
48+
}
49+
1350
@Factory
1451
public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) {
15-
return new IsGreaterThan<T>(value);
52+
return new OrderingComparison<T>(value, -1, -1);
1653
}
1754

1855
@Factory
1956
public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) {
20-
return describedAs("a value greater than or equal to %0",
21-
anyOf(greaterThan(value), equalTo(value)),
22-
value);
57+
return new OrderingComparison<T>(value, -1, 0);
2358
}
2459

2560
@Factory
2661
public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
27-
return describedAs("a value less than %0",
28-
not(greaterThanOrEqualTo(value)),
29-
value);
62+
return new OrderingComparison<T>(value, 1, 1);
3063
}
3164

3265
@Factory
3366
public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) {
34-
return describedAs("a value less than or equal to %0",
35-
not(greaterThan(value)),
36-
value);
67+
return new OrderingComparison<T>(value, 0, 1);
3768
}
3869
}

0 commit comments

Comments
 (0)