Skip to content

Commit e227510

Browse files
committed
Added more @SafeVarargs annotations and deleted fixed-arity overloads of varargs methods
1 parent 4c22fe2 commit e227510

File tree

4 files changed

+14
-240
lines changed

4 files changed

+14
-240
lines changed

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

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,11 @@ public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.
1717
* For example:
1818
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
1919
*/
20+
@SafeVarargs
2021
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T>... matchers) {
2122
return org.hamcrest.core.AllOf.allOf(matchers);
2223
}
2324

24-
/**
25-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
26-
* For example:
27-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
28-
*/
29-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) {
30-
return org.hamcrest.core.AllOf.allOf(first, second);
31-
}
32-
33-
/**
34-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
35-
* For example:
36-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
37-
*/
38-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
39-
return org.hamcrest.core.AllOf.allOf(first, second, third);
40-
}
41-
42-
/**
43-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
44-
* For example:
45-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
46-
*/
47-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
48-
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth);
49-
}
50-
51-
/**
52-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
53-
* For example:
54-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
55-
*/
56-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
57-
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth, fifth);
58-
}
59-
60-
/**
61-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
62-
* For example:
63-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
64-
*/
65-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {
66-
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth, fifth, sixth);
67-
}
6825

6926
/**
7027
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
@@ -80,55 +37,11 @@ public static <T> org.hamcrest.core.AnyOf<T> anyOf(java.lang.Iterable<org.hamcre
8037
* For example:
8138
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
8239
*/
40+
@SafeVarargs
8341
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T>... matchers) {
8442
return org.hamcrest.core.AnyOf.anyOf(matchers);
8543
}
8644

87-
/**
88-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
89-
* For example:
90-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
91-
*/
92-
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second) {
93-
return org.hamcrest.core.AnyOf.anyOf(first, second);
94-
}
95-
96-
/**
97-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
98-
* For example:
99-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
100-
*/
101-
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
102-
return org.hamcrest.core.AnyOf.anyOf(first, second, third);
103-
}
104-
105-
/**
106-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
107-
* For example:
108-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
109-
*/
110-
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
111-
return org.hamcrest.core.AnyOf.anyOf(first, second, third, fourth);
112-
}
113-
114-
/**
115-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
116-
* For example:
117-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
118-
*/
119-
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
120-
return org.hamcrest.core.AnyOf.anyOf(first, second, third, fourth, fifth);
121-
}
122-
123-
/**
124-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
125-
* For example:
126-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
127-
*/
128-
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {
129-
return org.hamcrest.core.AnyOf.anyOf(first, second, third, fourth, fifth, sixth);
130-
}
131-
13245
/**
13346
* Creates a matcher that matches when both of the specified matchers match the examined object.
13447
* For example:
@@ -271,6 +184,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T
271184
* @param itemMatchers
272185
* the matchers to apply to items provided by the examined {@link Iterable}
273186
*/
187+
@SafeVarargs
274188
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) {
275189
return org.hamcrest.core.IsCollectionContaining.hasItems(itemMatchers);
276190
}
@@ -286,6 +200,7 @@ public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcr
286200
* @param items
287201
* the items to compare against the items provided by the examined {@link Iterable}
288202
*/
203+
@SafeVarargs
289204
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) {
290205
return org.hamcrest.core.IsCollectionContaining.hasItems(items);
291206
}

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

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import org.hamcrest.DiagnosingMatcher;
55
import org.hamcrest.Matcher;
66

7-
import java.util.ArrayList;
87
import java.util.Arrays;
9-
import java.util.List;
108

119
/**
1210
* Calculates the logical conjunction of multiple matchers. Evaluation is shortcut, so
@@ -43,85 +41,16 @@ public void describeTo(Description description) {
4341
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
4442
*/
4543
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
46-
return new AllOf<T>(matchers);
44+
return new AllOf<>(matchers);
4745
}
4846

4947
/**
5048
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
5149
* For example:
5250
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
5351
*/
52+
@SafeVarargs
5453
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
5554
return allOf(Arrays.asList(matchers));
5655
}
57-
58-
/**
59-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
60-
* For example:
61-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
62-
*/
63-
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second) {
64-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(2);
65-
matchers.add(first);
66-
matchers.add(second);
67-
return allOf(matchers);
68-
}
69-
70-
/**
71-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
72-
* For example:
73-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
74-
*/
75-
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third) {
76-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(3);
77-
matchers.add(first);
78-
matchers.add(second);
79-
matchers.add(third);
80-
return allOf(matchers);
81-
}
82-
83-
/**
84-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
85-
* For example:
86-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
87-
*/
88-
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
89-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(4);
90-
matchers.add(first);
91-
matchers.add(second);
92-
matchers.add(third);
93-
matchers.add(fourth);
94-
return allOf(matchers);
95-
}
96-
97-
/**
98-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
99-
* For example:
100-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
101-
*/
102-
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
103-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(5);
104-
matchers.add(first);
105-
matchers.add(second);
106-
matchers.add(third);
107-
matchers.add(fourth);
108-
matchers.add(fifth);
109-
return allOf(matchers);
110-
}
111-
112-
/**
113-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
114-
* For example:
115-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
116-
*/
117-
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
118-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(6);
119-
matchers.add(first);
120-
matchers.add(second);
121-
matchers.add(third);
122-
matchers.add(fourth);
123-
matchers.add(fifth);
124-
matchers.add(sixth);
125-
return allOf(matchers);
126-
}
12756
}

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

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
55

6-
import java.util.ArrayList;
76
import java.util.Arrays;
8-
import java.util.List;
97

108
/**
119
* Calculates the logical disjunction of multiple matchers. Evaluation is shortcut, so
@@ -33,7 +31,7 @@ public void describeTo(Description description) {
3331
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
3432
*/
3533
public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
36-
return new AnyOf<T>(matchers);
34+
return new AnyOf<>(matchers);
3735
}
3836

3937
/**
@@ -45,74 +43,4 @@ public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
4543
public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
4644
return anyOf(Arrays.asList(matchers));
4745
}
48-
49-
/**
50-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
51-
* For example:
52-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
53-
*/
54-
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second) {
55-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
56-
matchers.add(first);
57-
matchers.add(second);
58-
return anyOf(matchers);
59-
}
60-
61-
/**
62-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
63-
* For example:
64-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
65-
*/
66-
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
67-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
68-
matchers.add(first);
69-
matchers.add(second);
70-
matchers.add(third);
71-
return anyOf(matchers);
72-
}
73-
74-
/**
75-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
76-
* For example:
77-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
78-
*/
79-
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
80-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
81-
matchers.add(first);
82-
matchers.add(second);
83-
matchers.add(third);
84-
matchers.add(fourth);
85-
return anyOf(matchers);
86-
}
87-
88-
/**
89-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
90-
* For example:
91-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
92-
*/
93-
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
94-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
95-
matchers.add(first);
96-
matchers.add(second);
97-
matchers.add(third);
98-
matchers.add(fourth);
99-
matchers.add(fifth);
100-
return anyOf(matchers);
101-
}
102-
103-
/**
104-
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
105-
* For example:
106-
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
107-
*/
108-
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
109-
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
110-
matchers.add(first);
111-
matchers.add(second);
112-
matchers.add(third);
113-
matchers.add(fourth);
114-
matchers.add(fifth);
115-
matchers.add(sixth);
116-
return anyOf(matchers);
117-
}
11846
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void describeTo(Description description) {
6767
* the matcher to apply to items provided by the examined {@link Iterable}
6868
*/
6969
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) {
70-
return new IsCollectionContaining<T>(itemMatcher);
70+
return new IsCollectionContaining<>(itemMatcher);
7171
}
7272

7373
/**
@@ -83,7 +83,7 @@ public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMa
8383
*/
8484
public static <T> Matcher<Iterable<? super T>> hasItem(T item) {
8585
// Doesn't forward to hasItem() method so compiler can sort out generics.
86-
return new IsCollectionContaining<T>(equalTo(item));
86+
return new IsCollectionContaining<>(equalTo(item));
8787
}
8888

8989
/**
@@ -97,12 +97,13 @@ public static <T> Matcher<Iterable<? super T>> hasItem(T item) {
9797
* @param itemMatchers
9898
* the matchers to apply to items provided by the examined {@link Iterable}
9999
*/
100+
@SafeVarargs
100101
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) {
101-
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(itemMatchers.length);
102+
List<Matcher<? super Iterable<T>>> all = new ArrayList<>(itemMatchers.length);
102103

103104
for (Matcher<? super T> elementMatcher : itemMatchers) {
104105
// Doesn't forward to hasItem() method so compiler can sort out generics.
105-
all.add(new IsCollectionContaining<T>(elementMatcher));
106+
all.add(new IsCollectionContaining<>(elementMatcher));
106107
}
107108

108109
return allOf(all);
@@ -119,8 +120,9 @@ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatche
119120
* @param items
120121
* the items to compare against the items provided by the examined {@link Iterable}
121122
*/
123+
@SafeVarargs
122124
public static <T> Matcher<Iterable<T>> hasItems(T... items) {
123-
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(items.length);
125+
List<Matcher<? super Iterable<T>>> all = new ArrayList<>(items.length);
124126
for (T item : items) {
125127
all.add(hasItem(item));
126128
}

0 commit comments

Comments
 (0)