Skip to content

Commit d03571a

Browse files
committed
Address warnings about verbose code that can be simplified with Java 7 language features
1 parent 026cf0b commit d03571a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+112
-97
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
public abstract class Condition<T> {
14-
public static final NotMatched<Object> NOT_MATCHED = new NotMatched<Object>();
14+
public static final NotMatched<Object> NOT_MATCHED = new NotMatched<>();
1515

1616
public interface Step<I, O> {
1717
Condition<O> apply(I value, Description mismatch);
@@ -31,7 +31,7 @@ public static <T> Condition<T> notMatched() {
3131
}
3232

3333
public static <T> Condition<T> matched(final T theValue, final Description mismatch) {
34-
return new Matched<T>(theValue, mismatch);
34+
return new Matched<>(theValue, mismatch);
3535
}
3636

3737
private static final class Matched<T> extends Condition<T> {

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/main/java/org/hamcrest/core/DescribedAs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public void describeMismatch(Object item, Description description) {
6666
* optional values to insert into the tokenised description
6767
*/
6868
public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher, Object... values) {
69-
return new DescribedAs<T>(description, matcher, values);
69+
return new DescribedAs<>(description, matcher, values);
7070
}
7171
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public void describeTo(Description description) {
3939
* the matcher to apply to every item provided by the examined {@link Iterable}
4040
*/
4141
public static <U> Matcher<Iterable<? extends U>> everyItem(final Matcher<U> itemMatcher) {
42-
return new Every<U>(itemMatcher);
42+
return new Every<>(itemMatcher);
4343
}
4444
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void describeTo(Description description) {
3636
* Creates a matcher that always matches, regardless of the examined object.
3737
*/
3838
public static Matcher<Object> anything() {
39-
return new IsAnything<Object>();
39+
return new IsAnything<>();
4040
}
4141

4242
/**
@@ -47,6 +47,6 @@ public static Matcher<Object> anything() {
4747
* a meaningful {@link String} used when describing itself
4848
*/
4949
public static Matcher<Object> anything(String description) {
50-
return new IsAnything<Object>(description);
50+
return new IsAnything<>(description);
5151
}
5252
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ private static boolean isArray(Object o) {
8686
*
8787
*/
8888
public static <T> Matcher<T> equalTo(T operand) {
89-
return new IsEqual<T>(operand);
89+
return new IsEqual<>(operand);
9090
}
9191

9292
/**
9393
* Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being
9494
* compared to be of the same static type.
9595
*/
9696
public static Matcher<Object> equalToObject(Object operand) {
97-
return new IsEqual<Object>(operand);
97+
return new IsEqual<>(operand);
9898
}
9999
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void describeTo(Description description) {
4040
* the matcher whose sense should be inverted
4141
*/
4242
public static <T> Matcher<T> not(Matcher<T> matcher) {
43-
return new IsNot<T>(matcher);
43+
return new IsNot<>(matcher);
4444
}
4545

4646
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void describeTo(Description description) {
2929
*
3030
*/
3131
public static Matcher<Object> nullValue() {
32-
return new IsNull<Object>();
32+
return new IsNull<>();
3333
}
3434

3535
/**
@@ -54,7 +54,7 @@ public static Matcher<Object> notNullValue() {
5454
* dummy parameter used to infer the generic type of the returned matcher
5555
*/
5656
public static <T> Matcher<T> nullValue(Class<T> type) {
57-
return new IsNull<T>();
57+
return new IsNull<>();
5858
}
5959

6060
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void describeTo(Description description) {
3737
* the target instance against which others should be assessed
3838
*/
3939
public static <T> Matcher<T> sameInstance(T target) {
40-
return new IsSame<T>(target);
40+
return new IsSame<>(target);
4141
}
4242

4343
/**
@@ -48,6 +48,6 @@ public static <T> Matcher<T> sameInstance(T target) {
4848
* the target instance against which others should be assessed
4949
*/
5050
public static <T> Matcher<T> theInstance(T target) {
51-
return new IsSame<T>(target);
51+
return new IsSame<>(target);
5252
}
5353
}

hamcrest-core/src/main/java/org/hamcrest/internal/NullSafety.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class NullSafety {
1010
@SuppressWarnings("unchecked")
1111
public static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>[] itemMatchers) {
12-
final List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(itemMatchers.length);
12+
final List<Matcher<? super E>> matchers = new ArrayList<>(itemMatchers.length);
1313
for (final Matcher<? super E> itemMatcher : itemMatchers) {
1414
matchers.add((Matcher<? super E>) (itemMatcher == null ? IsNull.nullValue() : itemMatcher));
1515
}

hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValueIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public boolean hasNext() {
1818

1919
@Override
2020
public SelfDescribing next() {
21-
return new SelfDescribingValue<T>(values.next());
21+
return new SelfDescribingValue<>(values.next());
2222
}
2323

2424
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import static org.hamcrest.core.IsNull.notNullValue;
1010

1111
public final class CombinableTest {
12-
private static final CombinableMatcher<Integer> EITHER_3_OR_4 = CombinableMatcher.<Integer>either(equalTo(3)).or(equalTo(4));
13-
private static final CombinableMatcher<Integer> NOT_3_AND_NOT_4 = CombinableMatcher.<Integer>both(not(equalTo(3))).and(not(equalTo(4)));
12+
private static final CombinableMatcher<Integer> EITHER_3_OR_4 = CombinableMatcher.either(equalTo(3)).or(equalTo(4));
13+
private static final CombinableMatcher<Integer> NOT_3_AND_NOT_4 = CombinableMatcher.both(not(equalTo(3))).and(not(equalTo(4)));
1414

1515
@Test public void
1616
copesWithNullsAndUnknownTypes() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public final class IsCollectionContainingTest {
5252

5353
@Test public void
5454
canMatchItemWhenCollectionHoldsSuperclass() { // Issue 24
55-
final Set<Number> s = new HashSet<Number>();
55+
final Set<Number> s = new HashSet<>();
5656
s.add(2);
5757

58-
assertMatches(new IsCollectionContaining<Number>(new IsEqual<Number>(2)), s);
58+
assertMatches(new IsCollectionContaining<>(new IsEqual<Number>(2)), s);
5959
assertMatches(IsCollectionContaining.hasItem(2), s);
6060
}
6161

hamcrest-library/src/main/java/org/hamcrest/beans/HasProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void describeTo(Description description) {
5252
* the name of the JavaBean property that examined beans should possess
5353
*/
5454
public static <T> Matcher<T> hasProperty(String propertyName) {
55-
return new HasProperty<T>(propertyName);
55+
return new HasProperty<>(propertyName);
5656
}
5757

5858
}

hamcrest-library/src/main/java/org/hamcrest/beans/HasPropertyWithValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@ public Condition<Method> apply(PropertyDescriptor property, Description mismatch
145145
* a matcher for the value of the specified property of the examined bean
146146
*/
147147
public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> valueMatcher) {
148-
return new HasPropertyWithValue<T>(propertyName, valueMatcher);
148+
return new HasPropertyWithValue<>(propertyName, valueMatcher);
149149
}
150150
}

hamcrest-library/src/main/java/org/hamcrest/beans/SamePropertyValuesAs.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ private boolean hasMatchingValues(T item, Description mismatchDescription) {
7272
}
7373

7474
private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDescriptor[] descriptors) {
75-
List<PropertyMatcher> result = new ArrayList<PropertyMatcher>(descriptors.length);
75+
List<PropertyMatcher> result = new ArrayList<>(descriptors.length);
7676
for (PropertyDescriptor propertyDescriptor : descriptors) {
7777
result.add(new PropertyMatcher(propertyDescriptor, bean));
7878
}
7979
return result;
8080
}
8181

8282
private static Set<String> propertyNamesFrom(PropertyDescriptor[] descriptors) {
83-
HashSet<String> result = new HashSet<String>();
83+
HashSet<String> result = new HashSet<>();
8484
for (PropertyDescriptor propertyDescriptor : descriptors) {
8585
result.add(propertyDescriptor.getDisplayName());
8686
}
@@ -134,7 +134,7 @@ private static Object readProperty(Method method, Object target) {
134134
* the bean against which examined beans are compared
135135
*/
136136
public static <T> Matcher<T> samePropertyValuesAs(T expectedBean) {
137-
return new SamePropertyValuesAs<T>(expectedBean);
137+
return new SamePropertyValuesAs<>(expectedBean);
138138
}
139139

140140
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ protected String descriptionEnd() {
9090
* @param elementMatchers
9191
* the matchers that the elements of examined arrays should satisfy
9292
*/
93+
@SafeVarargs
9394
public static <T> IsArray<T> array(Matcher<? super T>... elementMatchers) {
94-
return new IsArray<T>(elementMatchers);
95+
return new IsArray<>(elementMatchers);
9596
}
9697

9798
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void describeTo(Description description) {
5151
* the matcher to apply to elements in examined arrays
5252
*/
5353
public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher) {
54-
return new IsArrayContaining<T>(elementMatcher);
54+
return new IsArrayContaining<>(elementMatcher);
5555
}
5656

5757
/**
@@ -66,6 +66,6 @@ public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher)
6666
*/
6767
public static <T> Matcher<T[]> hasItemInArray(T element) {
6868
Matcher<? super T> matcher = equalTo(element);
69-
return IsArrayContaining.<T>hasItemInArray(matcher);
69+
return IsArrayContaining.hasItemInArray(matcher);
7070
}
7171
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class IsArrayContainingInAnyOrder<E> extends TypeSafeMatcher<E[]> {
1616
private final Collection<Matcher<? super E>> matchers;
1717

1818
public IsArrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) {
19-
this.iterableMatcher = new IsIterableContainingInAnyOrder<E>(matchers);
19+
this.iterableMatcher = new IsIterableContainingInAnyOrder<>(matchers);
2020
this.matchers = matchers;
2121
}
2222

@@ -56,6 +56,7 @@ public void describeTo(Description description) {
5656
* @param itemMatchers
5757
* a list of matchers, each of which must be satisfied by an entry in an examined array
5858
*/
59+
@SafeVarargs
5960
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers) {
6061
return arrayContainingInAnyOrder(Arrays.asList(itemMatchers));
6162
}
@@ -81,7 +82,7 @@ public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... i
8182
* a list of matchers, each of which must be satisfied by an item provided by an examined array
8283
*/
8384
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> itemMatchers) {
84-
return new IsArrayContainingInAnyOrder<E>(itemMatchers);
85+
return new IsArrayContainingInAnyOrder<>(itemMatchers);
8586
}
8687

8788
/**
@@ -102,11 +103,12 @@ public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? su
102103
* @param items
103104
* the items that must equal the entries of an examined array, in any order
104105
*/
106+
@SafeVarargs
105107
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) {
106-
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
108+
List<Matcher<? super E>> matchers = new ArrayList<>();
107109
for (E item : items) {
108110
matchers.add(equalTo(item));
109111
}
110-
return new IsArrayContainingInAnyOrder<E>(matchers);
112+
return new IsArrayContainingInAnyOrder<>(matchers);
111113
}
112114
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class IsArrayContainingInOrder<E> extends TypeSafeMatcher<E[]> {
1717
private final IsIterableContainingInOrder<E> iterableMatcher;
1818

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

@@ -46,8 +46,9 @@ public void describeTo(Description description) {
4646
* @param items
4747
* the items that must equal the items within an examined array
4848
*/
49+
@SafeVarargs
4950
public static <E> Matcher<E[]> arrayContaining(E... items) {
50-
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
51+
List<Matcher<? super E>> matchers = new ArrayList<>();
5152
for (E item : items) {
5253
matchers.add(equalTo(item));
5354
}
@@ -64,6 +65,7 @@ public static <E> Matcher<E[]> arrayContaining(E... items) {
6465
* @param itemMatchers
6566
* the matchers that must be satisfied by the items in the examined array
6667
*/
68+
@SafeVarargs
6769
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
6870
//required for JDK 1.6
6971
//noinspection RedundantTypeArguments
@@ -83,7 +85,7 @@ public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatcher
8385
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
8486
*/
8587
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
86-
return new IsArrayContainingInOrder<E>(itemMatchers);
88+
return new IsArrayContainingInOrder<>(itemMatchers);
8789
}
8890

8991
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected Integer featureValueOf(E[] actual) {
2929
* a matcher for the length of an examined array
3030
*/
3131
public static <E> Matcher<E[]> arrayWithSize(Matcher<? super Integer> sizeMatcher) {
32-
return new IsArrayWithSize<E>(sizeMatcher);
32+
return new IsArrayWithSize<>(sizeMatcher);
3333
}
3434

3535
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected Integer featureValueOf(Collection<? extends E> actual) {
3030
* a matcher for the size of an examined {@link java.util.Collection}
3131
*/
3232
public static <E> Matcher<Collection<? extends E>> hasSize(Matcher<? super Integer> sizeMatcher) {
33-
return new IsCollectionWithSize<E>(sizeMatcher);
33+
return new IsCollectionWithSize<>(sizeMatcher);
3434
}
3535

3636
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void describeTo(Description description) {
3434
*
3535
*/
3636
public static <E> Matcher<Collection<? extends E>> empty() {
37-
return new IsEmptyCollection<E>();
37+
return new IsEmptyCollection<>();
3838
}
3939

4040
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void describeTo(Description description) {
3030
*
3131
*/
3232
public static <E> Matcher<Iterable<? extends E>> emptyIterable() {
33-
return new IsEmptyIterable<E>();
33+
return new IsEmptyIterable<>();
3434
}
3535

3636
/**

0 commit comments

Comments
 (0)