Skip to content

Commit eeb210e

Browse files
committed
remove generic diamond warnings
1 parent dcd62f0 commit eeb210e

File tree

4 files changed

+64
-56
lines changed

4 files changed

+64
-56
lines changed

hamcrest-core/src/main/java/org/hamcrest/core/CombinableMatcher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public void describeTo(Description description) {
2828
}
2929

3030
public CombinableMatcher<T> and(Matcher<? super T> other) {
31-
return new CombinableMatcher<T>(new AllOf<T>(templatedListWith(other)));
31+
return new CombinableMatcher<>(new AllOf<>(templatedListWith(other)));
3232
}
3333

3434
public CombinableMatcher<T> or(Matcher<? super T> other) {
35-
return new CombinableMatcher<T>(new AnyOf<T>(templatedListWith(other)));
35+
return new CombinableMatcher<>(new AnyOf<>(templatedListWith(other)));
3636
}
3737

3838
private ArrayList<Matcher<? super T>> templatedListWith(Matcher<? super T> other) {
39-
ArrayList<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
39+
ArrayList<Matcher<? super T>> matchers = new ArrayList<>();
4040
matchers.add(matcher);
4141
matchers.add(other);
4242
return matchers;
@@ -48,7 +48,7 @@ private ArrayList<Matcher<? super T>> templatedListWith(Matcher<? super T> other
4848
* <pre>assertThat("fab", both(containsString("a")).and(containsString("b")))</pre>
4949
*/
5050
public static <LHS> CombinableBothMatcher<LHS> both(Matcher<? super LHS> matcher) {
51-
return new CombinableBothMatcher<LHS>(matcher);
51+
return new CombinableBothMatcher<>(matcher);
5252
}
5353

5454
public static final class CombinableBothMatcher<X> {
@@ -57,7 +57,7 @@ public CombinableBothMatcher(Matcher<? super X> matcher) {
5757
this.first = matcher;
5858
}
5959
public CombinableMatcher<X> and(Matcher<? super X> other) {
60-
return new CombinableMatcher<X>(first).and(other);
60+
return new CombinableMatcher<>(first).and(other);
6161
}
6262
}
6363

@@ -67,7 +67,7 @@ public CombinableMatcher<X> and(Matcher<? super X> other) {
6767
* <pre>assertThat("fan", either(containsString("a")).or(containsString("b")))</pre>
6868
*/
6969
public static <LHS> CombinableEitherMatcher<LHS> either(Matcher<? super LHS> matcher) {
70-
return new CombinableEitherMatcher<LHS>(matcher);
70+
return new CombinableEitherMatcher<>(matcher);
7171
}
7272

7373
public static final class CombinableEitherMatcher<X> {
@@ -76,7 +76,7 @@ public CombinableEitherMatcher(Matcher<? super X> matcher) {
7676
this.first = matcher;
7777
}
7878
public CombinableMatcher<X> or(Matcher<? super X> other) {
79-
return new CombinableMatcher<X>(first).or(other);
79+
return new CombinableMatcher<>(first).or(other);
8080
}
8181
}
8282
}

hamcrest-core/src/test/java/org/hamcrest/AbstractMatcherTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ public static void assertNullSafe(Matcher<?> matcher) {
5151
public static void assertUnknownTypeSafe(Matcher<?> matcher) {
5252
try {
5353
matcher.matches(new UnknownType());
54-
}
55-
catch (Exception e) {
56-
Assert.fail("Matcher was not unknown type safe");
54+
} catch (Exception e) {
55+
Assert.fail("Matcher was not unknown type safe, because: " + e);
5756
}
5857
}
5958

@@ -68,7 +67,7 @@ public void testIsNullSafe() {
6867
}
6968

7069
public void testCopesWithUnknownTypes() {
71-
assertUnknownTypeSafe(createMatcher());
70+
createMatcher().matches(new UnknownType());
7271
}
7372

7473
public static class UnknownType {

hamcrest-core/src/test/java/org/hamcrest/TypeSafeDiagnosingMatcherTest.java

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

3+
import org.junit.Ignore;
34
import org.junit.Test;
45

5-
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
6+
import static org.hamcrest.AbstractMatcherTest.*;
67

78
/**
89
* @author Steve Freeman 2016 http://www.hamcrest.com
910
*/
1011
public class TypeSafeDiagnosingMatcherTest {
1112

12-
private final TypeSafeDiagnosingMatcher matcher = new TypeSafeDiagnosingMatcher<String>() {
13+
private final TypeSafeDiagnosingMatcher stringMatcher = new TypeSafeDiagnosingMatcher<String>() {
1314
@Override
1415
protected boolean matchesSafely(String item, Description mismatchDescription) {
1516
mismatchDescription.appendText("mismatching");
@@ -23,8 +24,48 @@ public void describeTo(Description description) { }
2324

2425
@Test public void
2526
describesMismatches() {
26-
assertMismatchDescription("was null", matcher, null);
27-
assertMismatchDescription("was Character \"c\"", matcher, 'c');
28-
assertMismatchDescription("mismatching", matcher, "other");
27+
assertMismatchDescription("was null", stringMatcher, null);
28+
assertMismatchDescription("was Character \"c\"", stringMatcher, 'c');
29+
assertMismatchDescription("mismatching", stringMatcher, "other");
2930
}
31+
32+
@Test public void
33+
detects_non_builtin_types() {
34+
final Matcher<NotBuiltIn> matcher = new TypeSafeDiagnosingMatcher<NotBuiltIn>() {
35+
@Override
36+
protected boolean matchesSafely(NotBuiltIn item, Description mismatchDescription) {
37+
return true;
38+
}
39+
40+
@Override public void describeTo(Description description) { description.appendText("a not builtin"); }
41+
};
42+
43+
assertMatches("not built in", matcher, new NotBuiltIn());
44+
assertDoesNotMatch("other not built in", (Matcher)matcher, new OtherNotBuiltIn());
45+
}
46+
47+
@Ignore("not yet") @Test public void
48+
detects_for_subtypes_of_matcher() {
49+
final Matcher<NotBuiltIn> matcher = new SubMatcher<>();
50+
51+
assertMatches("not built in", matcher, new NotBuiltIn());
52+
assertDoesNotMatch("other not built in", (Matcher)matcher, new OtherNotBuiltIn());
53+
}
54+
55+
56+
public static class SubMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
57+
@Override protected boolean matchesSafely(T item, Description mismatchDescription) { return true; }
58+
59+
@Override public void describeTo(Description description) { description.appendText("sub type"); }
60+
}
61+
62+
public static class NotBuiltIn {
63+
public final String value = "not built in";
64+
@Override public String toString() { return "NotBuiltIn"; }
65+
}
66+
67+
public static class OtherNotBuiltIn {
68+
69+
}
70+
3071
}

hamcrest-library/src/test/java/org/hamcrest/object/HasEqualsValuesTest.java

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

33
import org.hamcrest.AbstractMatcherTest;
4-
import org.hamcrest.Description;
54
import org.hamcrest.Matcher;
6-
import org.hamcrest.TypeSafeDiagnosingMatcher;
7-
8-
import java.lang.reflect.Field;
9-
import java.util.ArrayList;
10-
import java.util.List;
115

126
public class HasEqualsValuesTest extends AbstractMatcherTest {
137
private static final WithPublicFields WITH_PUBLIC_FIELDS = new WithPublicFields(666, "a string");
14-
public static final HasEqualValues<WithPublicFields> WITH_PUBLIC_FIELDS_MATCHER = new HasEqualValues<>(WITH_PUBLIC_FIELDS);
15-
16-
public static class HasEqualValues<T> extends TypeSafeDiagnosingMatcher<T> {
17-
18-
private final T expectedObject;
19-
20-
public HasEqualValues(T expectedObject) {
21-
this.expectedObject = expectedObject;
22-
}
23-
24-
@Override
25-
protected boolean matchesSafely(T item, Description mismatchDescription) {
26-
27-
return true;
28-
}
29-
30-
@Override
31-
public void describeTo(Description description) {
32-
description.appendText("has equal values to ")
33-
.appendText(expectedObject.getClass().getSimpleName())
34-
.appendValueList("[", ",", "]", fieldNames());
35-
}
36-
37-
private List<String> fieldNames() {
38-
final List<String> result = new ArrayList<>();
39-
for (Field field : expectedObject.getClass().getFields()) {
40-
result.add(field.getName());
41-
}
42-
return result;
43-
}
44-
}
8+
private static final HasEqualValues<WithPublicFields> WITH_PUBLIC_FIELDS_MATCHER = new HasEqualValues<>(WITH_PUBLIC_FIELDS);
459

4610
@Override
4711
protected Matcher<?> createMatcher() {
@@ -50,14 +14,18 @@ protected Matcher<?> createMatcher() {
5014

5115
public void testDescribesItself() {
5216
assertDescription(
53-
"has equal values to WithPublicFields[\"i\",\"s\"]",
54-
new HasEqualValues<>(WITH_PUBLIC_FIELDS));
17+
"WithPublicFields has values [i: <666>, s: \"a string\"]",
18+
WITH_PUBLIC_FIELDS_MATCHER);
5519
}
5620

57-
public void testMisMatchesDifferentType() {
21+
public void testMatchesEquivalentObject() {
5822
assertMatches(WITH_PUBLIC_FIELDS_MATCHER, new WithPublicFields(666, "a string"));
5923
}
6024

25+
public void testMisMatchesFieldInequality() {
26+
assertMismatchDescription("'s' was \"different\"", WITH_PUBLIC_FIELDS_MATCHER, new WithPublicFields(666, "different"));
27+
}
28+
6129

6230
public static class WithPublicFields {
6331
public final int i;

0 commit comments

Comments
 (0)