1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
58
58
* @author Sam Brannen
59
59
* @author Colin Sampaleanu
60
60
* @author Rob Harrop
61
+ * @author Sebastien Deleuze
61
62
* @since 1.1.2
62
63
*/
63
64
public abstract class Assert {
@@ -209,6 +210,7 @@ public static void notNull(@Nullable Object object, Supplier<String> messageSupp
209
210
* @throws IllegalArgumentException if the text is empty
210
211
* @see StringUtils#hasLength
211
212
*/
213
+ @ Contract ("null, _ -> fail" )
212
214
public static void hasLength (@ Nullable String text , String message ) {
213
215
if (!StringUtils .hasLength (text )) {
214
216
throw new IllegalArgumentException (message );
@@ -229,6 +231,7 @@ public static void hasLength(@Nullable String text, String message) {
229
231
* @since 5.0
230
232
* @see StringUtils#hasLength
231
233
*/
234
+ @ Contract ("null, _ -> fail" )
232
235
public static void hasLength (@ Nullable String text , Supplier <String > messageSupplier ) {
233
236
if (!StringUtils .hasLength (text )) {
234
237
throw new IllegalArgumentException (nullSafeGet (messageSupplier ));
@@ -244,6 +247,7 @@ public static void hasLength(@Nullable String text, Supplier<String> messageSupp
244
247
* @throws IllegalArgumentException if the text does not contain valid text content
245
248
* @see StringUtils#hasText
246
249
*/
250
+ @ Contract ("null, _ -> fail" )
247
251
public static void hasText (@ Nullable String text , String message ) {
248
252
if (!StringUtils .hasText (text )) {
249
253
throw new IllegalArgumentException (message );
@@ -264,6 +268,7 @@ public static void hasText(@Nullable String text, String message) {
264
268
* @since 5.0
265
269
* @see StringUtils#hasText
266
270
*/
271
+ @ Contract ("null, _ -> fail" )
267
272
public static void hasText (@ Nullable String text , Supplier <String > messageSupplier ) {
268
273
if (!StringUtils .hasText (text )) {
269
274
throw new IllegalArgumentException (nullSafeGet (messageSupplier ));
@@ -387,6 +392,7 @@ public static void noNullElements(@Nullable Object[] array, Supplier<String> mes
387
392
* @throws IllegalArgumentException if the collection is {@code null} or
388
393
* contains no elements
389
394
*/
395
+ @ Contract ("null, _ -> fail" )
390
396
public static void notEmpty (@ Nullable Collection <?> collection , String message ) {
391
397
if (CollectionUtils .isEmpty (collection )) {
392
398
throw new IllegalArgumentException (message );
@@ -406,6 +412,7 @@ public static void notEmpty(@Nullable Collection<?> collection, String message)
406
412
* contains no elements
407
413
* @since 5.0
408
414
*/
415
+ @ Contract ("null, _ -> fail" )
409
416
public static void notEmpty (@ Nullable Collection <?> collection , Supplier <String > messageSupplier ) {
410
417
if (CollectionUtils .isEmpty (collection )) {
411
418
throw new IllegalArgumentException (nullSafeGet (messageSupplier ));
@@ -461,6 +468,7 @@ public static void noNullElements(@Nullable Collection<?> collection, Supplier<S
461
468
* @param message the exception message to use if the assertion fails
462
469
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
463
470
*/
471
+ @ Contract ("null, _ -> fail" )
464
472
public static void notEmpty (@ Nullable Map <?, ?> map , String message ) {
465
473
if (CollectionUtils .isEmpty (map )) {
466
474
throw new IllegalArgumentException (message );
@@ -479,6 +487,7 @@ public static void notEmpty(@Nullable Map<?, ?> map, String message) {
479
487
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
480
488
* @since 5.0
481
489
*/
490
+ @ Contract ("null, _ -> fail" )
482
491
public static void notEmpty (@ Nullable Map <?, ?> map , Supplier <String > messageSupplier ) {
483
492
if (CollectionUtils .isEmpty (map )) {
484
493
throw new IllegalArgumentException (nullSafeGet (messageSupplier ));
@@ -497,6 +506,7 @@ public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSup
497
506
* of the offending object's type will be appended.
498
507
* @throws IllegalArgumentException if the object is not an instance of type
499
508
*/
509
+ @ Contract ("_, null, _ -> fail" )
500
510
public static void isInstanceOf (Class <?> type , @ Nullable Object obj , String message ) {
501
511
notNull (type , "Type to check against must not be null" );
502
512
if (!type .isInstance (obj )) {
@@ -516,6 +526,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj, String mess
516
526
* @throws IllegalArgumentException if the object is not an instance of type
517
527
* @since 5.0
518
528
*/
529
+ @ Contract ("_, null, _ -> fail" )
519
530
public static void isInstanceOf (Class <?> type , @ Nullable Object obj , Supplier <String > messageSupplier ) {
520
531
notNull (type , "Type to check against must not be null" );
521
532
if (!type .isInstance (obj )) {
@@ -530,6 +541,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<St
530
541
* @param obj the object to check
531
542
* @throws IllegalArgumentException if the object is not an instance of type
532
543
*/
544
+ @ Contract ("_, null -> fail" )
533
545
public static void isInstanceOf (Class <?> type , @ Nullable Object obj ) {
534
546
isInstanceOf (type , obj , "" );
535
547
}
@@ -546,6 +558,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj) {
546
558
* offending subtype will be appended.
547
559
* @throws IllegalArgumentException if the classes are not assignable
548
560
*/
561
+ @ Contract ("_, null, _ -> fail" )
549
562
public static void isAssignable (Class <?> superType , @ Nullable Class <?> subType , String message ) {
550
563
notNull (superType , "Supertype to check against must not be null" );
551
564
if (subType == null || !superType .isAssignableFrom (subType )) {
@@ -565,6 +578,7 @@ public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
565
578
* @throws IllegalArgumentException if the classes are not assignable
566
579
* @since 5.0
567
580
*/
581
+ @ Contract ("_, null, _ -> fail" )
568
582
public static void isAssignable (Class <?> superType , @ Nullable Class <?> subType , Supplier <String > messageSupplier ) {
569
583
notNull (superType , "Supertype to check against must not be null" );
570
584
if (subType == null || !superType .isAssignableFrom (subType )) {
@@ -579,7 +593,8 @@ public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
579
593
* @param subType the subtype to check
580
594
* @throws IllegalArgumentException if the classes are not assignable
581
595
*/
582
- public static void isAssignable (Class <?> superType , Class <?> subType ) {
596
+ @ Contract ("_, null -> fail" )
597
+ public static void isAssignable (Class <?> superType , @ Nullable Class <?> subType ) {
583
598
isAssignable (superType , subType , "" );
584
599
}
585
600
0 commit comments