1919import static io .github .ascopes .jct .utils .IterableUtils .requireNonNullValues ;
2020import static java .util .Objects .requireNonNull ;
2121import static java .util .function .Predicate .not ;
22+ import static java .util .stream .Collectors .collectingAndThen ;
23+ import static java .util .stream .Collectors .toUnmodifiableList ;
2224
2325import io .github .ascopes .jct .diagnostics .TraceDiagnostic ;
2426import io .github .ascopes .jct .repr .TraceDiagnosticListRepresentation ;
2729import java .util .LinkedHashSet ;
2830import java .util .List ;
2931import java .util .Locale ;
32+ import java .util .Objects ;
3033import java .util .function .Predicate ;
31- import java .util .stream .Collectors ;
3234import java .util .stream .StreamSupport ;
3335import javax .tools .Diagnostic .Kind ;
3436import javax .tools .JavaFileObject ;
@@ -136,12 +138,11 @@ public TraceDiagnosticListAssert others() {
136138 * @throws NullPointerException if any of the kinds are null.
137139 */
138140 public TraceDiagnosticListAssert filteringByKinds (Kind kind , Kind ... moreKinds ) {
139- requireNonNull (kind , "kind must not be null " );
141+ requireNonNull (kind , "kind" );
140142 requireNonNullValues (moreKinds , "moreKinds" );
141143 return filteringByKinds (combineOneOrMore (kind , moreKinds ));
142144 }
143145
144-
145146 /**
146147 * Get a {@link TraceDiagnosticListAssert} that contains diagnostics corresponding to any of the
147148 * given {@link Kind kinds}.
@@ -153,7 +154,7 @@ public TraceDiagnosticListAssert filteringByKinds(Kind kind, Kind... moreKinds)
153154 */
154155 public TraceDiagnosticListAssert filteringByKinds (Iterable <Kind > kinds ) {
155156 requireNonNullValues (kinds , "kinds" );
156- return filteringBy (kind (kinds ));
157+ return filteringBy (kindIsOneOf (kinds ));
157158 }
158159
159160 /**
@@ -167,7 +168,7 @@ public TraceDiagnosticListAssert filteringByKinds(Iterable<Kind> kinds) {
167168 * @throws NullPointerException if any of the kinds are null.
168169 */
169170 public TraceDiagnosticListAssert excludingKinds (Kind kind , Kind ... moreKinds ) {
170- requireNonNull (kind , "kind must not be null " );
171+ requireNonNull (kind , "kind" );
171172 requireNonNullValues (moreKinds , "moreKinds" );
172173 return excludingKinds (combineOneOrMore (kind , moreKinds ));
173174 }
@@ -183,7 +184,12 @@ public TraceDiagnosticListAssert excludingKinds(Kind kind, Kind... moreKinds) {
183184 */
184185 public TraceDiagnosticListAssert excludingKinds (Iterable <Kind > kinds ) {
185186 requireNonNullValues (kinds , "kinds" );
186- return filteringBy (not (kind (kinds )));
187+ isNotNull ();
188+ return actual
189+ .stream ()
190+ .filter (Objects ::nonNull )
191+ .filter (not (kindIsOneOf (kinds )))
192+ .collect (collectingAndThen (toUnmodifiableList (), this ::newAbstractIterableAssert ));
187193 }
188194
189195 /**
@@ -268,7 +274,7 @@ public TraceDiagnosticListAssert hasNoOtherDiagnostics() {
268274 * @throws NullPointerException if the kind or more kinds are null.
269275 */
270276 public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds (Kind kind , Kind ... moreKinds ) {
271- requireNonNull (kind , "kind must not be null " );
277+ requireNonNull (kind , "kind" );
272278 requireNonNullValues (moreKinds , "moreKinds" );
273279 return hasNoDiagnosticsOfKinds (combineOneOrMore (kind , moreKinds ));
274280 }
@@ -283,31 +289,34 @@ public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds(Kind kind, Kind... more
283289 */
284290 public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds (Iterable <Kind > kinds ) {
285291 requireNonNullValues (kinds , "kinds" );
292+ isNotNull ();
286293
287294 var actualDiagnostics = actual
288295 .stream ()
289- .filter (kind (kinds ))
290- .collect (Collectors .toList ());
291-
292- if (!actualDiagnostics .isEmpty ()) {
293- var allKindsString = StreamSupport .stream (kinds .spliterator (), false )
294- .map (next -> next .name ().toLowerCase (Locale .ROOT ).replace ('_' , ' ' ))
295- .sorted ()
296- .collect (Collectors .collectingAndThen (
297- Collectors .toUnmodifiableList (),
298- names -> StringUtils .toWordedList (names , ", " , ", or " )
299- ));
300-
301- failWithActualExpectedAndMessage (
302- actualDiagnostics .size (),
303- 0 ,
304- "Expected no %s diagnostics.\n \n Diagnostics:\n %s" ,
305- allKindsString ,
306- TraceDiagnosticListRepresentation .getInstance ().toStringOf (actualDiagnostics )
307- );
296+ .filter (kindIsOneOf (kinds ))
297+ .collect (toUnmodifiableList ());
298+
299+ if (actualDiagnostics .isEmpty ()) {
300+ return myself ;
308301 }
309302
310- return myself ;
303+ var allKindsString = StreamSupport
304+ .stream (kinds .spliterator (), false )
305+ .map (next -> next .name ().toLowerCase (Locale .ROOT ).replace ('_' , ' ' ))
306+ .distinct ()
307+ .sorted ()
308+ .collect (collectingAndThen (
309+ toUnmodifiableList (),
310+ names -> StringUtils .toWordedList (names , ", " , ", or " )
311+ ));
312+
313+ throw failureWithActualExpected (
314+ actualDiagnostics .size (),
315+ 0 ,
316+ "Expected no %s diagnostics.\n \n Diagnostics:\n %s" ,
317+ allKindsString ,
318+ TraceDiagnosticListRepresentation .getInstance ().toStringOf (actualDiagnostics )
319+ );
311320 }
312321
313322 /**
@@ -322,17 +331,8 @@ public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds(Iterable<Kind> kinds) {
322331 public TraceDiagnosticListAssert filteringBy (
323332 Predicate <TraceDiagnostic <? extends JavaFileObject >> predicate
324333 ) {
325- requireNonNull (predicate , "predicate must not be null" );
326-
327334 isNotNull ();
328-
329- return actual
330- .stream ()
331- .filter (predicate )
332- .collect (Collectors .collectingAndThen (
333- Collectors .toUnmodifiableList (),
334- TraceDiagnosticListAssert ::new
335- ));
335+ return filteredOn (predicate );
336336 }
337337
338338 @ Override
@@ -352,10 +352,10 @@ protected TraceDiagnosticListAssert newAbstractIterableAssert(
352352 return new TraceDiagnosticListAssert (list );
353353 }
354354
355- private Predicate <TraceDiagnostic <? extends JavaFileObject >> kind (Iterable <Kind > kinds ) {
355+ private Predicate <TraceDiagnostic <? extends JavaFileObject >> kindIsOneOf (Iterable <Kind > kinds ) {
356356 var kindsSet = new LinkedHashSet <Kind >();
357357 kinds .forEach (kindsSet ::add );
358358
359- return diagnostic -> kindsSet .contains (diagnostic .getKind ());
359+ return diagnostic -> diagnostic != null && kindsSet .contains (diagnostic .getKind ());
360360 }
361361}
0 commit comments