Skip to content

Commit a2c9338

Browse files
author
nat.pryce
committed
nat - type wildcard tweaks
1 parent cf0efab commit a2c9338

File tree

3 files changed

+68
-67
lines changed

3 files changed

+68
-67
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package org.hamcrest.collection;
22

3-
import static org.hamcrest.core.IsEqual.equalTo;
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
47

58
import java.util.ArrayList;
6-
import java.util.Arrays;
79
import java.util.Collection;
810
import java.util.List;
911

10-
import org.hamcrest.Description;
11-
import org.hamcrest.Factory;
12-
import org.hamcrest.Matcher;
13-
import org.hamcrest.TypeSafeMatcher;
12+
import static java.util.Arrays.asList;
13+
import static org.hamcrest.core.IsEqual.equalTo;
1414

1515
public class IsArrayContainingInOrder<E> extends TypeSafeMatcher<E[]> {
1616
private final Collection<Matcher<? super E>> matchers;
17-
private final IsIterableContainingInOrder<E> iterableMatcher;
17+
private final IsIterableContainingInOrder<E,Iterable<E>> iterableMatcher;
1818

1919
public IsArrayContainingInOrder(List<Matcher<? super E>> matchers) {
20-
this.iterableMatcher = new IsIterableContainingInOrder<E>(matchers);
20+
this.iterableMatcher = new IsIterableContainingInOrder<E,Iterable<E>>(matchers);
2121
this.matchers = matchers;
2222
}
2323

2424
@Override
2525
public boolean matchesSafely(E[] item) {
26-
return iterableMatcher.matches(Arrays.asList(item));
26+
return iterableMatcher.matches(asList(item));
2727
}
2828

2929
@Override
3030
public void describeMismatchSafely(E[] item, Description mismatchDescription) {
31-
iterableMatcher.describeMismatch(Arrays.asList(item), mismatchDescription);
32-
};
31+
iterableMatcher.describeMismatch(asList(item), mismatchDescription);
32+
}
3333

3434
public void describeTo(Description description) {
3535
description.appendList("[", ", ", "]", matchers);
@@ -46,7 +46,7 @@ public static <E> Matcher<E[]> arrayContaining(E... items) {
4646

4747
@Factory
4848
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... matchers) {
49-
return arrayContaining(Arrays.asList(matchers));
49+
return arrayContaining(asList(matchers));
5050
}
5151

5252
@Factory

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

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,76 @@
1111

1212
import static org.hamcrest.core.IsEqual.equalTo;
1313

14-
public class IsIterableContainingInOrder<E> extends TypeSafeDiagnosingMatcher<Iterable<? extends E>> {
14+
public class IsIterableContainingInOrder<E, C extends Iterable<? extends E>> extends TypeSafeDiagnosingMatcher<C> {
1515
private final List<Matcher<? super E>> matchers;
1616

1717
public IsIterableContainingInOrder(List<Matcher<? super E>> matchers) {
18-
this.matchers = matchers;
18+
this.matchers = matchers;
1919
}
20-
20+
2121
@Override
22-
protected boolean matchesSafely(Iterable<? extends E> iterable, Description mismatchDescription) {
23-
MatchSeries<E> matchSeries = new MatchSeries<E>(matchers, mismatchDescription);
24-
for (E item : iterable) {
25-
if (! matchSeries.matches(item)) {
26-
return false;
22+
protected boolean matchesSafely(C iterable, Description mismatchDescription) {
23+
MatchSeries<E> matchSeries = new MatchSeries<E>(matchers, mismatchDescription);
24+
for (E item : iterable) {
25+
if (!matchSeries.matches(item)) {
26+
return false;
27+
}
2728
}
28-
}
29-
30-
return matchSeries.isFinished();
29+
30+
return matchSeries.isFinished();
3131
}
3232

3333
public void describeTo(Description description) {
3434
description.appendText("iterable containing ").appendList("[", ", ", "]", matchers);
3535
}
3636

3737
private static class MatchSeries<F> {
38-
public final List<Matcher<? super F>> matchers;
39-
private final Description mismatchDescription;
40-
public int nextMatchIx = 0;
41-
42-
public MatchSeries(List<Matcher<? super F>> matchers, Description mismatchDescription) {
43-
this.mismatchDescription = mismatchDescription;
44-
if (matchers.isEmpty()) {
45-
throw new IllegalArgumentException("Should specify at least one expected element");
38+
public final List<Matcher<? super F>> matchers;
39+
private final Description mismatchDescription;
40+
public int nextMatchIx = 0;
41+
42+
public MatchSeries(List<Matcher<? super F>> matchers, Description mismatchDescription) {
43+
this.mismatchDescription = mismatchDescription;
44+
if (matchers.isEmpty()) {
45+
throw new IllegalArgumentException("Should specify at least one expected element");
46+
}
47+
this.matchers = matchers;
4648
}
47-
this.matchers = matchers;
48-
}
4949

50-
public boolean matches(F item) {
51-
return isNotSurplus(item) && isMatched(item);
52-
}
50+
public boolean matches(F item) {
51+
return isNotSurplus(item) && isMatched(item);
52+
}
5353

54-
public boolean isFinished() {
55-
if (nextMatchIx < matchers.size()) {
56-
mismatchDescription.appendText("No item matched: ").appendDescriptionOf(matchers.get(nextMatchIx));
57-
return false;
54+
public boolean isFinished() {
55+
if (nextMatchIx < matchers.size()) {
56+
mismatchDescription.appendText("No item matched: ").appendDescriptionOf(matchers.get(nextMatchIx));
57+
return false;
58+
}
59+
return true;
5860
}
59-
return true;
60-
}
61-
62-
private boolean isMatched(F item) {
63-
Matcher<? super F> matcher = matchers.get(nextMatchIx);
64-
if (!matcher.matches(item)) {
65-
describeMismatch(matcher, item);
66-
return false;
61+
62+
private boolean isMatched(F item) {
63+
Matcher<? super F> matcher = matchers.get(nextMatchIx);
64+
if (!matcher.matches(item)) {
65+
describeMismatch(matcher, item);
66+
return false;
67+
}
68+
nextMatchIx++;
69+
return true;
6770
}
68-
nextMatchIx++;
69-
return true;
70-
}
71-
72-
private boolean isNotSurplus(F item) {
73-
if (matchers.size() <= nextMatchIx) {
74-
mismatchDescription.appendText("Not matched: ").appendValue(item);
75-
return false;
71+
72+
private boolean isNotSurplus(F item) {
73+
if (matchers.size() <= nextMatchIx) {
74+
mismatchDescription.appendText("Not matched: ").appendValue(item);
75+
return false;
76+
}
77+
return true;
78+
}
79+
80+
private void describeMismatch(Matcher<? super F> matcher, F item) {
81+
mismatchDescription.appendText("item " + nextMatchIx + ": ");
82+
matcher.describeMismatch(item, mismatchDescription);
7683
}
77-
return true;
78-
}
79-
80-
private void describeMismatch(Matcher<? super F> matcher, F item) {
81-
mismatchDescription.appendText("item " + nextMatchIx + ": ");
82-
matcher.describeMismatch(item, mismatchDescription);
83-
}
8484
}
8585

8686
@Factory
@@ -93,17 +93,17 @@ public static <E> Matcher<Iterable<? extends E>> contains(E... items) {
9393
}
9494

9595
@Factory
96-
public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<E> item) {
96+
public static <E, C extends Iterable<? extends E>> Matcher<C> contains(final Matcher<E> item) {
9797
return contains(new ArrayList<Matcher<? super E>>(Arrays.asList(item)));
9898
}
9999

100100
@Factory
101-
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... items) {
101+
public static <E, C extends Iterable<? extends E>> Matcher<C> contains(Matcher<? super E>... items) {
102102
return contains(Arrays.asList(items));
103103
}
104104

105105
@Factory
106-
public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> contents) {
107-
return new IsIterableContainingInOrder<E>(contents);
106+
public static <E, C extends Iterable<? extends E>> Matcher<C> contains(List<Matcher<? super E>> contents) {
107+
return new IsIterableContainingInOrder<E, C>(contents);
108108
}
109109
}

hamcrest-unit-test/src/main/hamcrest-unit-tests.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module" module-name="hamcrest-core" />
1011
<orderEntry type="library" name="integration" level="project" />
1112
<orderEntry type="library" name="ant-output" level="project" />
1213
</component>

0 commit comments

Comments
 (0)