Skip to content

Commit 97b3a06

Browse files
author
joeretro
committed
Added AllOf, AnyOf
1 parent 88be713 commit 97b3a06

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.BaseMatcher;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
8+
import java.util.Arrays;
9+
10+
/**
11+
* Calculates the logical conjunction of two matchers. Evaluation is
12+
* shortcut, so that the second matcher is not called if the first
13+
* matcher returns <code>false</code>.
14+
*/
15+
public class AllOf<T> extends BaseMatcher<T> {
16+
private final Iterable<Matcher<T>> matchers;
17+
18+
public AllOf(Iterable<Matcher<T>> matchers) {
19+
this.matchers = matchers;
20+
}
21+
22+
public boolean matches(Object o) {
23+
for (Matcher<T> matcher : matchers) {
24+
if (!matcher.matches(o)) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
31+
public void describeTo(Description description) {
32+
description.appendText("(");
33+
boolean seenFirst = false;
34+
for (Matcher<T> matcher : matchers) {
35+
if (seenFirst) {
36+
description.appendText(" and ");
37+
} else {
38+
seenFirst = true;
39+
}
40+
matcher.describeTo(description);
41+
}
42+
description.appendText(")");
43+
}
44+
45+
@Factory
46+
public static <T> Matcher<T> allOf(Matcher<T>... matchers) {
47+
return allOf(Arrays.asList(matchers));
48+
}
49+
50+
@Factory
51+
public static <T> Matcher<T> allOf(Iterable<Matcher<T>> matchers) {
52+
return new AllOf<T>(matchers);
53+
}
54+
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.BaseMatcher;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
8+
import java.util.Arrays;
9+
10+
/**
11+
* Calculates the logical disjunction of two matchers. Evaluation is
12+
* shortcut, so that the second matcher is not called if the first
13+
* matcher returns <code>true</code>.
14+
*/
15+
public class AnyOf<T> extends BaseMatcher<T> {
16+
17+
private final Iterable<Matcher<T>> matchers;
18+
19+
public AnyOf(Iterable<Matcher<T>> matchers) {
20+
this.matchers = matchers;
21+
}
22+
23+
public boolean matches(Object o) {
24+
for (Matcher<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.appendText("(");
34+
boolean seenFirst = false;
35+
for (Matcher<T> matcher : matchers) {
36+
if (seenFirst) {
37+
description.appendText(" or ");
38+
} else {
39+
seenFirst = true;
40+
}
41+
matcher.describeTo(description);
42+
}
43+
description.appendText(")");
44+
}
45+
46+
@Factory
47+
public static <T> Matcher<T> anyOf(Matcher<T>... matchers) {
48+
return anyOf(Arrays.asList(matchers));
49+
}
50+
51+
@Factory
52+
public static <T> Matcher<T> anyOf(Iterable<Matcher<T>> matchers) {
53+
return new AnyOf<T>(matchers);
54+
}
55+
}

0 commit comments

Comments
 (0)