1 /******************************************************************************* 2 * Copyright 2013 André Rouél and Dominik Seichter 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 package net.sf.qualitycheck; 17 18 import java.lang.annotation.Annotation; 19 import java.math.BigDecimal; 20 import java.math.BigInteger; 21 import java.util.Collection; 22 import java.util.Map; 23 import java.util.regex.Pattern; 24 25 import javax.annotation.Nonnegative; 26 import javax.annotation.Nonnull; 27 import javax.annotation.Nullable; 28 29 import net.sf.qualitycheck.exception.IllegalArgumentNotContainedException; 30 import net.sf.qualitycheck.exception.IllegalEmptyArgumentException; 31 import net.sf.qualitycheck.exception.IllegalInstanceOfArgumentException; 32 import net.sf.qualitycheck.exception.IllegalMissingAnnotationException; 33 import net.sf.qualitycheck.exception.IllegalNaNArgumentException; 34 import net.sf.qualitycheck.exception.IllegalNegativeArgumentException; 35 import net.sf.qualitycheck.exception.IllegalNotEqualException; 36 import net.sf.qualitycheck.exception.IllegalNotGreaterOrEqualThanException; 37 import net.sf.qualitycheck.exception.IllegalNotGreaterThanException; 38 import net.sf.qualitycheck.exception.IllegalNotLesserThanException; 39 import net.sf.qualitycheck.exception.IllegalNotNullArgumentException; 40 import net.sf.qualitycheck.exception.IllegalNullArgumentException; 41 import net.sf.qualitycheck.exception.IllegalNullElementsException; 42 import net.sf.qualitycheck.exception.IllegalNumberArgumentException; 43 import net.sf.qualitycheck.exception.IllegalNumericArgumentException; 44 import net.sf.qualitycheck.exception.IllegalPatternArgumentException; 45 import net.sf.qualitycheck.exception.IllegalPositionIndexException; 46 import net.sf.qualitycheck.exception.IllegalPositiveArgumentException; 47 import net.sf.qualitycheck.exception.IllegalRangeException; 48 import net.sf.qualitycheck.exception.IllegalStateOfArgumentException; 49 import net.sf.qualitycheck.exception.RuntimeInstantiationException; 50 51 /** 52 * This class offers simple static methods to test your arguments to be valid. 53 * 54 * Checks should be added to all arguments of all public methods in your class to assure that only valid values can be 55 * encountered within your class. This is major step to avoid technical errors like NullPointerExceptions or 56 * IndexOutOfBoundsException in your code. 57 * 58 * @author André Rouél 59 * @author Dominik Seichter 60 */ 61 public final class Check { 62 63 /** 64 * Holder for the regular expression to determine numeric values. Using the holder pattern guarantees that the 65 * regular expression is initialized before the first use (thread safe!) and that it is only initialized if it is 66 * needed. So, we do not pay any performance bounty for regular expressions when using other checks. 67 */ 68 protected static final class NumericRegularExpressionHolder { 69 70 private static final Pattern NUMERIC_REGEX = Pattern.compile("[0-9]+"); 71 72 @Nonnull 73 public static Pattern getPattern() { 74 return NUMERIC_REGEX; 75 } 76 77 } 78 79 /** 80 * Representation of an empty argument name. 81 */ 82 private static final String EMPTY_ARGUMENT_NAME = ""; 83 84 /** 85 * Checks the passed {@code value} against the ranges of the given datatype. 86 * 87 * @param value 88 * value which must be a number and in the range of the given datatype. 89 * @param type 90 * requested return value type, must be a subclass of {@code Number} 91 * @return a number 92 * 93 * @throws NumberFormatException 94 * if the given value can not be parsed as a number 95 */ 96 private static <T> Number checkNumberInRange(final String value, final Class<T> type) { 97 final Number ret; 98 if (type.equals(Byte.class)) { 99 final Number number = new BigInteger(value); 100 NumberInRange.checkByte(number); 101 ret = Byte.valueOf(number.byteValue()); 102 } else if (type.equals(Double.class)) { 103 final Number number = new BigDecimal(value); 104 NumberInRange.checkDouble(number); 105 ret = Double.valueOf(number.doubleValue()); 106 } else if (type.equals(Float.class)) { 107 final Number number = new BigDecimal(value); 108 NumberInRange.checkFloat(number); 109 ret = Float.valueOf(number.floatValue()); 110 } else if (type.equals(Integer.class)) { 111 final Number number = new BigInteger(value); 112 NumberInRange.checkInteger(number); 113 ret = Integer.valueOf(number.intValue()); 114 } else if (type.equals(Long.class)) { 115 final Number number = new BigInteger(value); 116 NumberInRange.checkLong(number); 117 ret = Long.valueOf(number.longValue()); 118 } else if (type.equals(Short.class)) { 119 final Number number = new BigInteger(value); 120 NumberInRange.checkShort(number); 121 ret = Short.valueOf(number.shortValue()); 122 } else if (type.equals(BigInteger.class)) { 123 ret = new BigInteger(value); 124 } else if (type.equals(BigDecimal.class)) { 125 ret = new BigDecimal(value); 126 } else { 127 throw new IllegalNumberArgumentException("Return value is no known subclass of 'java.lang.Number': " + type.getName()); 128 } 129 return ret; 130 } 131 132 /** 133 * Ensures that an element {@code needle} is contained in a collection {@code haystack}. 134 * 135 * <p> 136 * This is in particular useful if you want to check whether an enum value is contained in an {@code EnumSet}. The 137 * check is implemented using {@link java.util.Collection#contains(Object)}. 138 * 139 * <p> 140 * We recommend to use the overloaded method {@link Check#contains(Collection, Object, String)} and pass as second 141 * argument the name of the parameter to enhance the exception message. 142 * 143 * @param haystack 144 * A collection which must contain {@code needle} 145 * @param needle 146 * An object that must be contained into a collection. 147 * @return the passed argument {@code needle} 148 * 149 * @throws IllegalArgumentNotContainedException 150 * if the passed {@code needle} can not be found in {@code haystack} 151 */ 152 @ArgumentsChecked 153 @Throws({ IllegalNullArgumentException.class, IllegalArgumentNotContainedException.class }) 154 public static <T extends Object> T contains(@Nonnull final Collection<T> haystack, @Nonnull final T needle) { 155 Check.notNull(haystack, "haystack"); 156 Check.notNull(needle, "needle"); 157 158 if (!haystack.contains(needle)) { 159 throw new IllegalArgumentNotContainedException(); 160 } 161 162 return needle; 163 } 164 165 /** 166 * Ensures that an element {@code needle} is contained in a collection {@code haystack}. 167 * 168 * <p> 169 * This is in particular useful if you want to check whether an enum value is contained in an {@code EnumSet}. The 170 * check is implemented using {@link java.util.Collection#contains(Object)}. 171 * 172 * @param haystack 173 * A collection which must contain {@code needle} 174 * @param needle 175 * An object that must be contained into a collection. 176 * @param name 177 * name of argument of {@code needle} 178 * @return the passed argument {@code needle} 179 * 180 * @throws IllegalArgumentNotContainedException 181 * if the passed {@code needle} can not be found in {@code haystack} 182 */ 183 @ArgumentsChecked 184 @Throws({ IllegalNullArgumentException.class, IllegalArgumentNotContainedException.class }) 185 public static <T extends Object> T contains(@Nonnull final Collection<T> haystack, @Nonnull final T needle, @Nonnull final String name) { 186 Check.notNull(haystack, "haystack"); 187 Check.notNull(needle, "needle"); 188 189 if (!haystack.contains(needle)) { 190 throw new IllegalArgumentNotContainedException(name); 191 } 192 193 return needle; 194 } 195 196 /** 197 * Checks if the given array contains {@code null}. 198 * 199 * @param array 200 * reference to an array 201 * @return {@code true} if the array contains {@code null}, otherwise {@code false} 202 */ 203 private static boolean containsNullElements(@Nonnull final Object[] array) { 204 boolean containsNull = false; 205 for (final Object o : array) { 206 if (o == null) { 207 containsNull = true; 208 break; 209 } 210 } 211 return containsNull; 212 } 213 214 /** 215 * Ensures that a passed boolean is equal to another boolean. The comparison is made using 216 * <code>expected != check</code>. 217 * 218 * <p> 219 * We recommend to use the overloaded method {@link Check#equals(boolean, boolean, String)} and pass as second 220 * argument the name of the parameter to enhance the exception message. 221 * 222 * @param expected 223 * Expected value 224 * @param check 225 * boolean to be checked 226 * @return the passed boolean argument {@code check} 227 * 228 * @throws IllegalNotEqualException 229 * if both argument values are not equal 230 */ 231 @Throws(IllegalNotEqualException.class) 232 public static boolean equals(@Nonnull final boolean expected, @Nonnull final boolean check) { // NOSONAR 233 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 234 235 if (expected != check) { 236 throw new IllegalNotEqualException(); 237 } 238 239 return check; 240 } 241 242 /** 243 * Ensures that a passed boolean is equal to another boolean. The comparison is made using 244 * <code>expected != check</code>. 245 * 246 * @param expected 247 * Expected value 248 * @param check 249 * boolean to be checked 250 * @param message 251 * an error message describing why the booleans must equal (will be passed to 252 * {@code IllegalNotEqualException}) 253 * @return the passed boolean argument {@code check} 254 * 255 * @throws IllegalNotEqualException 256 * if both argument values are not equal 257 */ 258 @Throws(IllegalNotEqualException.class) 259 public static boolean equals(@Nonnull final boolean expected, @Nonnull final boolean check, final String message) { // NOSONAR 260 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 261 262 if (expected != check) { 263 throw new IllegalNotEqualException(message); 264 } 265 266 return check; 267 } 268 269 /** 270 * Ensures that a passed byte is equal to another byte. The comparison is made using <code>expected != check</code>. 271 * 272 * <p> 273 * We recommend to use the overloaded method {@link Check#equals(byte, byte, String)} and pass as second argument 274 * the name of the parameter to enhance the exception message. 275 * 276 * @param expected 277 * Expected value 278 * @param check 279 * byte to be checked 280 * @return the passed byte argument {@code check} 281 * 282 * @throws IllegalNotEqualException 283 * if both argument values are not equal 284 */ 285 @Throws(IllegalNotEqualException.class) 286 public static byte equals(@Nonnull final byte expected, @Nonnull final byte check) { // NOSONAR 287 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 288 289 if (expected != check) { 290 throw new IllegalNotEqualException(); 291 } 292 293 return check; 294 } 295 296 /** 297 * Ensures that a passed byte is equal to another byte. The comparison is made using <code>expected != check</code>. 298 * 299 * @param expected 300 * Expected value 301 * @param check 302 * byte to be checked 303 * @param message 304 * an error message describing why the bytes must equal (will be passed to 305 * {@code IllegalNotEqualException}) 306 * @return the byte boolean argument {@code check} 307 * 308 * @throws IllegalNotEqualException 309 * if both argument values are not equal 310 */ 311 @Throws(IllegalNotEqualException.class) 312 public static byte equals(@Nonnull final byte expected, @Nonnull final byte check, final String message) { // NOSONAR 313 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 314 315 if (expected != check) { 316 throw new IllegalNotEqualException(message); 317 } 318 319 return check; 320 } 321 322 /** 323 * Ensures that a passed char is equal to another char. The comparison is made using <code>expected != check</code>. 324 * 325 * <p> 326 * We recommend to use the overloaded method {@link Check#equals(char, char, String)} and pass as second argument 327 * the name of the parameter to enhance the exception message. 328 * 329 * @param expected 330 * Expected value 331 * @param check 332 * char to be checked 333 * @return the passed char argument {@code check} 334 * 335 * @throws IllegalNotEqualException 336 * if both argument values are not equal 337 */ 338 @Throws(IllegalNotEqualException.class) 339 public static char equals(@Nonnull final char expected, @Nonnull final char check) { // NOSONAR 340 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 341 342 if (expected != check) { 343 throw new IllegalNotEqualException(); 344 } 345 346 return check; 347 } 348 349 /** 350 * Ensures that a passed char is equal to another char. The comparison is made using <code>expected != check</code>. 351 * 352 * @param expected 353 * Expected value 354 * @param check 355 * char to be checked 356 * @param message 357 * an error message describing why the chars must equal (will be passed to 358 * {@code IllegalNotEqualException}) 359 * @return the passed char argument {@code check} 360 * 361 * @throws IllegalNotEqualException 362 * if both argument values are not equal 363 */ 364 @Throws(IllegalNotEqualException.class) 365 public static char equals(@Nonnull final char expected, @Nonnull final char check, final String message) { // NOSONAR 366 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 367 368 if (expected != check) { 369 throw new IllegalNotEqualException(message); 370 } 371 372 return check; 373 } 374 375 /** 376 * Ensures that a passed intH is equal to another int. The comparison is made using <code>expected != check</code>. 377 * 378 * <p> 379 * We recommend to use the overloaded method {@link Check#equals(int, int, String)} and pass as second argument the 380 * name of the parameter to enhance the exception message. 381 * 382 * @param expected 383 * Expected value 384 * @param check 385 * int to be checked 386 * @return the passed int argument {@code check} 387 * 388 * @throws IllegalNotEqualException 389 * if both argument values are not equal 390 */ 391 @Throws(IllegalNotEqualException.class) 392 public static int equals(@Nonnull final int expected, @Nonnull final int check) { // NOSONAR 393 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 394 395 if (expected != check) { 396 throw new IllegalNotEqualException(); 397 } 398 399 return check; 400 } 401 402 /** 403 * Ensures that a passed int is equal to another int. The comparison is made using <code>expected != check</code>. 404 * 405 * @param expected 406 * Expected value 407 * @param check 408 * int to be checked 409 * @param message 410 * an error message describing why the ints must equal (will be passed to 411 * {@code IllegalNotEqualException}) 412 * @return the passed int argument {@code check} 413 * 414 * @throws IllegalNotEqualException 415 * if both argument values are not equal 416 */ 417 @Throws(IllegalNotEqualException.class) 418 public static int equals(@Nonnull final int expected, @Nonnull final int check, final String message) { // NOSONAR 419 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 420 421 if (expected != check) { 422 throw new IllegalNotEqualException(message); 423 } 424 425 return check; 426 } 427 428 /** 429 * Ensures that a passed long is equal to another long. The comparison is made using <code>expected != check</code>. 430 * 431 * <p> 432 * We recommend to use the overloaded method {@link Check#equals(long, long, String)} and pass as second argument 433 * the name of the parameter to enhance the exception message. 434 * 435 * @param expected 436 * Expected value 437 * @param check 438 * long to be checked 439 * @return the passed long argument {@code check} 440 * 441 * @throws IllegalNotEqualException 442 * if both argument values are not equal 443 */ 444 @Throws(IllegalNotEqualException.class) 445 public static long equals(@Nonnull final long expected, @Nonnull final long check) { // NOSONAR 446 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 447 448 if (expected != check) { 449 throw new IllegalNotEqualException(); 450 } 451 452 return check; 453 } 454 455 /** 456 * Ensures that a passed long is equal to another long. The comparison is made using <code>expected != check</code>. 457 * 458 * @param expected 459 * Expected value 460 * @param check 461 * long to be checked 462 * @param message 463 * an error message describing why the longs must equal (will be passed to 464 * {@code IllegalNotEqualException}) 465 * @return the passed long argument {@code check} 466 * 467 * @throws IllegalNotEqualException 468 * if both argument values are not equal 469 */ 470 @Throws(IllegalNotEqualException.class) 471 public static long equals(@Nonnull final long expected, @Nonnull final long check, final String message) { // NOSONAR 472 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 473 474 if (expected != check) { 475 throw new IllegalNotEqualException(message); 476 } 477 478 return check; 479 } 480 481 /** 482 * Ensures that a passed short is equal to another short. The comparison is made using 483 * <code>expected != check</code>. 484 * 485 * <p> 486 * We recommend to use the overloaded method {@link Check#equals(short, short, String)} and pass as second argument 487 * the name of the parameter to enhance the exception message. 488 * 489 * @param expected 490 * Expected value 491 * @param check 492 * short to be checked 493 * @return the passed short argument {@code check} 494 * 495 * @throws IllegalNotEqualException 496 * if both argument values are not equal 497 */ 498 @Throws(IllegalNotEqualException.class) 499 public static short equals(@Nonnull final short expected, @Nonnull final short check) { // NOSONAR 500 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 501 502 if (expected != check) { 503 throw new IllegalNotEqualException(); 504 } 505 506 return check; 507 } 508 509 /** 510 * Ensures that a passed short is equal to another short. The comparison is made using 511 * <code>expected != check</code>. 512 * 513 * @param expected 514 * Expected value 515 * @param check 516 * short to be checked 517 * @param message 518 * an error message describing why the shorts must equal (will be passed to 519 * {@code IllegalNotEqualException}) 520 * @return the passed short {@code check} 521 * 522 * @throws IllegalNotEqualException 523 * if both argument values are not equal 524 */ 525 @Throws(IllegalNotEqualException.class) 526 public static short equals(@Nonnull final short expected, @Nonnull final short check, final String message) { // NOSONAR 527 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 528 529 if (expected != check) { 530 throw new IllegalNotEqualException(message); 531 } 532 533 return check; 534 } 535 536 /** 537 * Ensures that a passed {@code Comparable} is equal to another {@code Comparable}. The comparison is made using 538 * {@code expected.compareTo(check) != 0}. 539 * 540 * <p> 541 * We recommend to use the overloaded method {@link Check#equals(Comparable, Comparable, String)} and pass as second 542 * argument the name of the parameter to enhance the exception message. 543 * 544 * @param expected 545 * Expected value 546 * @param check 547 * Comparable to be checked 548 * @return the passed {@code Comparable} argument {@code check} 549 * 550 * @throws IllegalNotEqualException 551 * if both argument values are not equal 552 */ 553 @ArgumentsChecked 554 @Throws({ IllegalNullArgumentException.class, IllegalNotEqualException.class }) 555 public static <T extends Comparable<T>> T equals(@Nonnull final T expected, @Nonnull final T check) { // NOSONAR 556 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 557 Check.notNull(expected, "expected"); 558 Check.notNull(check, "check"); 559 560 if (expected.compareTo(check) != 0) { 561 throw new IllegalNotEqualException(); 562 } 563 564 return check; 565 } 566 567 /** 568 * Ensures that a passed object is equal to another object. The comparison is made using a call to 569 * {@code expected.equals(check) }. 570 * 571 * <p> 572 * We recommend to use the overloaded method {@link Check#equals(Object, Object, String)} and pass as second 573 * argument the name of the parameter to enhance the exception message. 574 * 575 * @param expected 576 * Expected value 577 * @param check 578 * Object to be checked 579 * @return the passed argument {@code check} 580 * 581 * @throws IllegalNotEqualException 582 * if both argument values are not equal 583 */ 584 @ArgumentsChecked 585 @Throws({ IllegalNullArgumentException.class, IllegalNotEqualException.class }) 586 public static <T extends Object> T equals(@Nonnull final T expected, @Nonnull final T check) { // NOSONAR 587 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 588 589 Check.notNull(expected, "expected"); 590 Check.notNull(check, "check"); 591 592 if (!expected.equals(check)) { 593 throw new IllegalNotEqualException(); 594 } 595 596 return check; 597 } 598 599 /** 600 * Ensures that a passed {@code Comparable} is equal to another {@code Comparable}. The comparison is made using 601 * {@code expected.compareTo(check) != 0}. 602 * 603 * @param expected 604 * Expected value 605 * @param check 606 * Comparable to be checked 607 * @param message 608 * an error message describing why the <a>s must equal (will be passed to 609 * {@code IllegalNotEqualException}) 610 * @return the passed {@code Comparable} argument {@code check} 611 * 612 * @throws IllegalNotEqualException 613 * if both argument values are not equal 614 */ 615 @ArgumentsChecked 616 @Throws({ IllegalNullArgumentException.class, IllegalNotEqualException.class }) 617 public static <T extends Comparable<T>> T equals(@Nonnull final T expected, @Nonnull final T check, final String message) { // NOSONAR 618 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 619 Check.notNull(expected, "expected"); 620 Check.notNull(check, "check"); 621 622 if (expected.compareTo(check) != 0) { 623 throw new IllegalNotEqualException(message); 624 } 625 626 return check; 627 } 628 629 /** 630 * Ensures that a passed object is equal to another object. The comparison is made using a call to 631 * {@code expected.equals(check) }. 632 * 633 * @param expected 634 * Expected value 635 * @param check 636 * Object to be checked 637 * @param message 638 * an error message describing why the objects must equal (will be passed to 639 * {@code IllegalNotEqualException}) 640 * @return the passed argument {@code check} 641 * 642 * @throws IllegalNotEqualException 643 * if both argument values are not equal 644 */ 645 @ArgumentsChecked 646 @Throws({ IllegalNullArgumentException.class, IllegalNotEqualException.class }) 647 public static <T extends Object> T equals(@Nonnull final T expected, @Nonnull final T check, final String message) { // NOSONAR 648 // Sonar warns about suspicious equals method name, as the name is intended deactivate sonar 649 650 Check.notNull(expected, "expected"); 651 Check.notNull(check, "check"); 652 653 if (!expected.equals(check)) { 654 throw new IllegalNotEqualException(message); 655 } 656 657 return check; 658 } 659 660 /** 661 * Ensures that a passed {@code Comparable} is greater or equal compared to another {@code Comparable}. The 662 * comparison is made using {@code expected.compareTo(check) > 0}. 663 * 664 * @param expected 665 * Expected value 666 * @param check 667 * Comparable to be checked 668 * @return the passed {@code Comparable} argument {@code check} 669 * 670 * @throws IllegalNotGreaterOrEqualThanException 671 * if the argument value {@code check} is not greater or equal than the value {@code expected} when 672 * using method {@code compareTo} 673 */ 674 @ArgumentsChecked 675 @Throws({ IllegalNullArgumentException.class, IllegalNotGreaterOrEqualThanException.class }) 676 public static <T extends Comparable<T>> T greaterOrEqualThan(@Nonnull final T expected, @Nonnull final T check) { 677 Check.notNull(expected, "expected"); 678 Check.notNull(check, "check"); 679 680 if (expected.compareTo(check) > 0) { 681 throw new IllegalNotGreaterOrEqualThanException(); 682 } 683 684 return check; 685 } 686 687 /** 688 * Ensures that a passed {@code Comparable} is greater or equal compared to another {@code Comparable}. The 689 * comparison is made using {@code expected.compareTo(check) > 0}. 690 * 691 * @param expected 692 * Expected value 693 * @param check 694 * Comparable to be checked 695 * @param message 696 * an error message describing why the comparable must be greater than a value (will be passed to 697 * {@code IllegalNotGreaterOrEqualThanException}) 698 * @return the passed {@code Comparable} argument {@code check} 699 * 700 * @throws IllegalNotGreaterOrEqualThanException 701 * if the argument value {@code check} is not greater or equal than the value {@code expected} when 702 * using method {@code compareTo} 703 */ 704 @ArgumentsChecked 705 @Throws({ IllegalNullArgumentException.class, IllegalNotGreaterOrEqualThanException.class }) 706 public static <T extends Comparable<T>> T greaterOrEqualThan(@Nonnull final T expected, @Nonnull final T check, final String message) { 707 Check.notNull(expected, "expected"); 708 Check.notNull(check, "check"); 709 710 if (expected.compareTo(check) > 0) { 711 throw new IllegalNotGreaterOrEqualThanException(message); 712 } 713 714 return check; 715 } 716 717 /** 718 * Ensures that a passed {@code byte} is greater to another {@code byte}. 719 * 720 * @param expected 721 * Expected value 722 * @param check 723 * Comparable to be checked 724 * @return the passed {@code Comparable} argument {@code check} 725 * 726 * @throws IllegalNotGreaterThanException 727 * if the argument value {@code check} is not greater than value {@code expected} 728 */ 729 @ArgumentsChecked 730 @Throws(IllegalNotGreaterThanException.class) 731 public static byte greaterThan(@Nonnull final byte expected, @Nonnull final byte check) { 732 if (expected >= check) { 733 throw new IllegalNotGreaterThanException(); 734 } 735 736 return check; 737 } 738 739 /** 740 * Ensures that a passed {@code byte} is greater than another {@code byte}. 741 * 742 * @param expected 743 * Expected value 744 * @param check 745 * Comparable to be checked 746 * @param message 747 * an error message describing why the comparable must be greater than a value (will be passed to 748 * {@code IllegalNotGreaterThanException}) 749 * @return the passed {@code Comparable} argument {@code check} 750 * 751 * @throws IllegalNotGreaterThanException 752 * if the argument value {@code check} is not greater than value {@code expected} 753 */ 754 @ArgumentsChecked 755 @Throws(IllegalNotGreaterThanException.class) 756 public static byte greaterThan(@Nonnull final byte expected, @Nonnull final byte check, final String message) { 757 if (expected >= check) { 758 throw new IllegalNotGreaterThanException(message); 759 } 760 761 return check; 762 } 763 764 /** 765 * Ensures that a passed {@code char} is greater to another {@code char}. 766 * 767 * @param expected 768 * Expected value 769 * @param check 770 * Comparable to be checked 771 * @return the passed {@code Comparable} argument {@code check} 772 * 773 * @throws IllegalNotGreaterThanException 774 * if the argument value {@code check} is not greater than value {@code expected} 775 */ 776 @ArgumentsChecked 777 @Throws(IllegalNotGreaterThanException.class) 778 public static char greaterThan(@Nonnull final char expected, @Nonnull final char check) { 779 if (expected >= check) { 780 throw new IllegalNotGreaterThanException(); 781 } 782 783 return check; 784 } 785 786 /** 787 * Ensures that a passed {@code char} is greater than another {@code char}. 788 * 789 * @param expected 790 * Expected value 791 * @param check 792 * Comparable to be checked 793 * @param message 794 * an error message describing why the comparable must be greater than a value (will be passed to 795 * {@code IllegalNotGreaterThanException}) 796 * @return the passed {@code Comparable} argument {@code check} 797 * 798 * @throws IllegalNotGreaterThanException 799 * if the argument value {@code check} is not greater than value {@code expected} 800 */ 801 @ArgumentsChecked 802 @Throws(IllegalNotGreaterThanException.class) 803 public static char greaterThan(@Nonnull final char expected, @Nonnull final char check, final String message) { 804 if (expected >= check) { 805 throw new IllegalNotGreaterThanException(message); 806 } 807 808 return check; 809 } 810 811 /** 812 * Ensures that a passed {@code double} is greater to another {@code double}. 813 * 814 * @param expected 815 * Expected value 816 * @param check 817 * Comparable to be checked 818 * @return the passed {@code Comparable} argument {@code check} 819 * 820 * @throws IllegalNotGreaterThanException 821 * if the argument value {@code check} is not greater than value {@code expected} 822 */ 823 @ArgumentsChecked 824 @Throws(IllegalNotGreaterThanException.class) 825 public static double greaterThan(@Nonnull final double expected, @Nonnull final double check) { 826 if (expected >= check) { 827 throw new IllegalNotGreaterThanException(); 828 } 829 830 return check; 831 } 832 833 /** 834 * Ensures that a passed {@code double} is greater than another {@code double}. 835 * 836 * @param expected 837 * Expected value 838 * @param check 839 * Comparable to be checked 840 * @param message 841 * an error message describing why the comparable must be greater than a value (will be passed to 842 * {@code IllegalNotGreaterThanException}) 843 * @return the passed {@code Comparable} argument {@code check} 844 * 845 * @throws IllegalNotGreaterThanException 846 * if the argument value {@code check} is not greater than value {@code expected} 847 */ 848 @ArgumentsChecked 849 @Throws(IllegalNotGreaterThanException.class) 850 public static double greaterThan(@Nonnull final double expected, @Nonnull final double check, final String message) { 851 if (expected >= check) { 852 throw new IllegalNotGreaterThanException(message); 853 } 854 855 return check; 856 } 857 858 /** 859 * Ensures that a passed {@code float} is greater to another {@code float}. 860 * 861 * @param expected 862 * Expected value 863 * @param check 864 * Comparable to be checked 865 * @return the passed {@code Comparable} argument {@code check} 866 * 867 * @throws IllegalNotGreaterThanException 868 * if the argument value {@code check} is not greater than value {@code expected} 869 */ 870 @ArgumentsChecked 871 @Throws(IllegalNotGreaterThanException.class) 872 public static float greaterThan(@Nonnull final float expected, @Nonnull final float check) { 873 if (expected >= check) { 874 throw new IllegalNotGreaterThanException(); 875 } 876 877 return check; 878 } 879 880 /** 881 * Ensures that a passed {@code float} is greater than another {@code float}. 882 * 883 * @param expected 884 * Expected value 885 * @param check 886 * Comparable to be checked 887 * @param message 888 * an error message describing why the comparable must be greater than a value (will be passed to 889 * {@code IllegalNotGreaterThanException}) 890 * @return the passed {@code Comparable} argument {@code check} 891 * 892 * @throws IllegalNotGreaterThanException 893 * if the argument value {@code check} is not greater than value {@code expected} 894 */ 895 @ArgumentsChecked 896 @Throws(IllegalNotGreaterThanException.class) 897 public static float greaterThan(@Nonnull final float expected, @Nonnull final float check, final String message) { 898 if (expected >= check) { 899 throw new IllegalNotGreaterThanException(message); 900 } 901 902 return check; 903 } 904 905 /** 906 * Ensures that a passed {@code int} is greater to another {@code int}. 907 * 908 * @param expected 909 * Expected value 910 * @param check 911 * Comparable to be checked 912 * @return the passed {@code Comparable} argument {@code check} 913 * 914 * @throws IllegalNotGreaterThanException 915 * if the argument value {@code check} is not greater than value {@code expected} 916 */ 917 @ArgumentsChecked 918 @Throws(IllegalNotGreaterThanException.class) 919 public static int greaterThan(@Nonnull final int expected, @Nonnull final int check) { 920 if (expected >= check) { 921 throw new IllegalNotGreaterThanException(); 922 } 923 924 return check; 925 } 926 927 /** 928 * Ensures that a passed {@code int} is greater than another {@code int}. 929 * 930 * @param expected 931 * Expected value 932 * @param check 933 * Comparable to be checked 934 * @param message 935 * an error message describing why the comparable must be greater than a value (will be passed to 936 * {@code IllegalNotGreaterThanException}) 937 * @return the passed {@code Comparable} argument {@code check} 938 * 939 * @throws IllegalNotGreaterThanException 940 * if the argument value {@code check} is not greater than value {@code expected} 941 */ 942 @ArgumentsChecked 943 @Throws(IllegalNotGreaterThanException.class) 944 public static int greaterThan(@Nonnull final int expected, @Nonnull final int check, final String message) { 945 if (expected >= check) { 946 throw new IllegalNotGreaterThanException(message); 947 } 948 949 return check; 950 } 951 952 /** 953 * Ensures that a passed {@code long} is greater to another {@code long}. 954 * 955 * @param expected 956 * Expected value 957 * @param check 958 * Comparable to be checked 959 * @return the passed {@code Comparable} argument {@code check} 960 * 961 * @throws IllegalNotGreaterThanException 962 * if the argument value {@code check} is not greater than value {@code expected} 963 */ 964 @ArgumentsChecked 965 @Throws(IllegalNotGreaterThanException.class) 966 public static long greaterThan(@Nonnull final long expected, @Nonnull final long check) { 967 if (expected >= check) { 968 throw new IllegalNotGreaterThanException(); 969 } 970 971 return check; 972 } 973 974 /** 975 * Ensures that a passed {@code long} is greater than another {@code long}. 976 * 977 * @param expected 978 * Expected value 979 * @param check 980 * Comparable to be checked 981 * @param message 982 * an error message describing why the comparable must be greater than a value (will be passed to 983 * {@code IllegalNotGreaterThanException}) 984 * @return the passed {@code Comparable} argument {@code check} 985 * 986 * @throws IllegalNotGreaterThanException 987 * if the argument value {@code check} is not greater than value {@code expected} 988 */ 989 @ArgumentsChecked 990 @Throws(IllegalNotGreaterThanException.class) 991 public static long greaterThan(@Nonnull final long expected, @Nonnull final long check, final String message) { 992 if (expected >= check) { 993 throw new IllegalNotGreaterThanException(message); 994 } 995 996 return check; 997 } 998 999 /** 1000 * Ensures that a passed {@code short} is greater to another {@code short}. 1001 * 1002 * @param expected 1003 * Expected value 1004 * @param check 1005 * Comparable to be checked 1006 * @return the passed {@code Comparable} argument {@code check} 1007 * 1008 * @throws IllegalNotGreaterThanException 1009 * if the argument value {@code check} is not greater than value {@code expected} 1010 */ 1011 @ArgumentsChecked 1012 @Throws(IllegalNotGreaterThanException.class) 1013 public static short greaterThan(@Nonnull final short expected, @Nonnull final short check) { 1014 if (expected >= check) { 1015 throw new IllegalNotGreaterThanException(); 1016 } 1017 1018 return check; 1019 } 1020 1021 /** 1022 * Ensures that a passed {@code short} is greater than another {@code short}. 1023 * 1024 * @param expected 1025 * Expected value 1026 * @param check 1027 * Comparable to be checked 1028 * @param message 1029 * an error message describing why the comparable must be greater than a value (will be passed to 1030 * {@code IllegalNotGreaterThanException}) 1031 * @return the passed {@code Comparable} argument {@code check} 1032 * 1033 * @throws IllegalNotGreaterThanException 1034 * if the argument value {@code check} is not greater than value {@code expected} 1035 */ 1036 @ArgumentsChecked 1037 @Throws(IllegalNotGreaterThanException.class) 1038 public static short greaterThan(@Nonnull final short expected, @Nonnull final short check, final String message) { 1039 if (expected >= check) { 1040 throw new IllegalNotGreaterThanException(message); 1041 } 1042 1043 return check; 1044 } 1045 1046 /** 1047 * Ensures that a passed {@code Comparable} is greater to another {@code Comparable}. The comparison is made using 1048 * {@code expected.compareTo(check) >= 0}. 1049 * 1050 * @param expected 1051 * Expected value 1052 * @param check 1053 * Comparable to be checked 1054 * @return the passed {@code Comparable} argument {@code check} 1055 * 1056 * @throws IllegalNotGreaterThanException 1057 * if the argument value {@code check} is not greater than value {@code expected} when using method 1058 * {@code compareTo} 1059 */ 1060 @ArgumentsChecked 1061 @Throws({ IllegalNullArgumentException.class, IllegalNotGreaterThanException.class }) 1062 public static <T extends Comparable<T>> T greaterThan(@Nonnull final T expected, @Nonnull final T check) { 1063 Check.notNull(expected, "expected"); 1064 Check.notNull(check, "check"); 1065 1066 if (expected.compareTo(check) >= 0) { 1067 throw new IllegalNotGreaterThanException(); 1068 } 1069 1070 return check; 1071 } 1072 1073 /** 1074 * Ensures that a passed {@code Comparable} is greater than another {@code Comparable}. The comparison is made using 1075 * {@code expected.compareTo(check) >= 0}. 1076 * 1077 * @param expected 1078 * Expected value 1079 * @param check 1080 * Comparable to be checked 1081 * @param message 1082 * an error message describing why the comparable must be greater than a value (will be passed to 1083 * {@code IllegalNotGreaterThanException}) 1084 * @return the passed {@code Comparable} argument {@code check} 1085 * 1086 * @throws IllegalNotGreaterThanException 1087 * if the argument value {@code check} is not greater than value {@code expected} when using method 1088 * {@code compareTo} 1089 */ 1090 @ArgumentsChecked 1091 @Throws({ IllegalNullArgumentException.class, IllegalNotGreaterThanException.class }) 1092 public static <T extends Comparable<T>> T greaterThan(@Nonnull final T expected, @Nonnull final T check, final String message) { 1093 Check.notNull(expected, "expected"); 1094 Check.notNull(check, "check"); 1095 1096 if (expected.compareTo(check) >= 0) { 1097 throw new IllegalNotGreaterThanException(message); 1098 } 1099 1100 return check; 1101 } 1102 1103 /** 1104 * Ensures that a passed class has an annotation of a specific type 1105 * 1106 * @param clazz 1107 * the class that must have a required annotation 1108 * @param annotation 1109 * the type of annotation that is required on the class 1110 * @return the given annotation which is present on the checked class 1111 * 1112 * @throws IllegalMissingAnnotationException 1113 * if the passed annotation is not annotated at the given class 1114 */ 1115 @ArgumentsChecked 1116 @Throws({ IllegalNullArgumentException.class, IllegalMissingAnnotationException.class }) 1117 public static Annotation hasAnnotation(@Nonnull final Class<?> clazz, @Nonnull final Class<? extends Annotation> annotation) { 1118 Check.notNull(clazz, "clazz"); 1119 Check.notNull(annotation, "annotation"); 1120 if (!clazz.isAnnotationPresent(annotation)) { 1121 throw new IllegalMissingAnnotationException(annotation, clazz); 1122 } 1123 1124 return clazz.getAnnotation(annotation); 1125 } 1126 1127 /** 1128 * Ensures that a passed argument is a member of a specific type. 1129 * 1130 * @param type 1131 * class that the given object is a member of 1132 * @param obj 1133 * the object reference that should be a member of a specific {@code type} 1134 * @return the given object cast to type 1135 * 1136 * @throws IllegalInstanceOfArgumentException 1137 * if the given argument {@code obj} is not a member of {@code type} 1138 */ 1139 @ArgumentsChecked 1140 @Throws({ IllegalNullArgumentException.class, IllegalInstanceOfArgumentException.class }) 1141 @SuppressWarnings("unchecked") 1142 public static <T> T instanceOf(@Nonnull final Class<?> type, @Nonnull final Object obj) { 1143 return (T) instanceOf(type, obj, EMPTY_ARGUMENT_NAME); 1144 } 1145 1146 /** 1147 * Ensures that a passed argument is a member of a specific type. 1148 * 1149 * @param type 1150 * class that the given object is a member of 1151 * @param obj 1152 * the object reference that should be a member of a specific {@code type} 1153 * @param name 1154 * name of object reference (in source code) 1155 * @return the given object cast to type 1156 * 1157 * @throws IllegalInstanceOfArgumentException 1158 * if the given argument {@code obj} is not a member of {@code type} 1159 */ 1160 @ArgumentsChecked 1161 @Throws({ IllegalNullArgumentException.class, IllegalInstanceOfArgumentException.class }) 1162 @SuppressWarnings("unchecked") 1163 public static <T> T instanceOf(@Nonnull final Class<?> type, @Nonnull final Object obj, @Nullable final String name) { 1164 Check.notNull(type, "type"); 1165 Check.notNull(obj, "obj"); 1166 if (!type.isInstance(obj)) { 1167 throw new IllegalInstanceOfArgumentException(name, type, obj.getClass()); 1168 } 1169 return (T) obj; 1170 } 1171 1172 /** 1173 * Ensures that a given argument is {@code null}. 1174 * 1175 * Normally, the usage of {@code null} arguments is disregarded by the authors of quality-check. Still, there are 1176 * certain circumstances where null is required, e.g. the primary key of an entity before it is written to the 1177 * database for the first time. In such cases it is ok to use null values and there should also be checks for them. 1178 * For example, to avoid overwriting an existing primary key with a new one. 1179 * 1180 * @param reference 1181 * reference which must be null 1182 * 1183 * @throws IllegalNotNullArgumentException 1184 * if the given argument {@code reference} is not null 1185 */ 1186 @Throws(IllegalNotNullArgumentException.class) 1187 public static void isNull(@Nullable final Object reference) { 1188 if (reference != null) { 1189 throw new IllegalNotNullArgumentException(); 1190 } 1191 } 1192 1193 /** 1194 * Ensures that a given argument is {@code null}. 1195 * 1196 * Normally, the usage of {@code null} arguments is disregarded by the authors of quality-check. Still, there are 1197 * certain circumstances where null is required, e.g. the primary key of an entity before it is written to the 1198 * database for the first time. In such cases it is ok to use null values and there should also be checks for them. 1199 * For example, to avoid overwriting an existing primary key with a new one. 1200 * 1201 * @param reference 1202 * reference which must be null. 1203 * @param name 1204 * name of object reference (in source code) 1205 * 1206 * @throws IllegalNotNullArgumentException 1207 * if the given argument {@code reference} is not null 1208 */ 1209 @Throws(IllegalNotNullArgumentException.class) 1210 public static void isNull(@Nullable final Object reference, @Nullable final String name) { 1211 if (reference != null) { 1212 throw new IllegalNotNullArgumentException(name); 1213 } 1214 } 1215 1216 /** 1217 * Ensures that a String argument is a number. 1218 * 1219 * @param value 1220 * value which must be a number 1221 * @return the given string argument converted to an int 1222 * 1223 * @throws IllegalNumberArgumentException 1224 * if the given argument {@code value} is not a number 1225 */ 1226 @ArgumentsChecked 1227 @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) 1228 public static int isNumber(@Nonnull final String value) { 1229 Check.notNull(value, "value"); 1230 return Check.isNumber(value, Integer.class).intValue(); 1231 } 1232 1233 /** 1234 * Ensures that a String argument is a number. This overload supports all subclasses of {@code Number}. The number 1235 * is first converted to a BigInteger 1236 * 1237 * @param value 1238 * value which must be a number 1239 * @param type 1240 * requested return value type, must be a subclass of {@code Number}, i.e. one of {@code BigDecimal, 1241 * BigInteger, Byte, Double, Float, Integer, Long, Short} 1242 * @return the given string argument converted to a number of the requested type 1243 * 1244 * @throws IllegalNumberArgumentException 1245 * if the given argument {@code value} is no number 1246 */ 1247 @ArgumentsChecked 1248 @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) 1249 public static <T extends Number> T isNumber(@Nonnull final String value, @Nonnull final Class<T> type) { 1250 return isNumber(value, null, type); 1251 } 1252 1253 /** 1254 * Ensures that a string argument is a number according to {@code Integer.parseInt} 1255 * 1256 * @param value 1257 * value which must be a number 1258 * @param name 1259 * name of object reference (in source code) 1260 * @return the given string argument converted to an int 1261 * 1262 * @throws IllegalNumberArgumentException 1263 * if the given argument {@code value} is no number 1264 */ 1265 @ArgumentsChecked 1266 @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) 1267 public static int isNumber(@Nonnull final String value, @Nullable final String name) { 1268 Check.notNull(value, "value"); 1269 return Check.isNumber(value, name, Integer.class).intValue(); 1270 } 1271 1272 /** 1273 * Ensures that a String argument is a number. This overload supports all subclasses of {@code Number}. The number 1274 * is first converted to a {@code BigDecimal} or {@code BigInteger}. Floating point types are only supported if the 1275 * {@code type} is one of {@code Float, Double, BigDecimal}. 1276 * 1277 * <p> 1278 * This method does also check against the ranges of the given datatypes. 1279 * 1280 * @param value 1281 * value which must be a number and in the range of the given datatype. 1282 * @param name 1283 * (optional) name of object reference (in source code). 1284 * @param type 1285 * requested return value type, must be a subclass of {@code Number}, i.e. one of {@code BigDecimal, 1286 * BigInteger, Byte, Double, Float, Integer, Long, Short} 1287 * @return the given string argument converted to a number of the requested type 1288 * 1289 * @throws IllegalNumberArgumentException 1290 * if the given argument {@code value} is no number 1291 */ 1292 @ArgumentsChecked 1293 @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) 1294 public static <T extends Number> T isNumber(@Nonnull final String value, @Nullable final String name, @Nonnull final Class<T> type) { 1295 Check.notNull(value, "value"); 1296 Check.notNull(type, "type"); 1297 1298 final Number ret; 1299 try { 1300 ret = checkNumberInRange(value, type); 1301 } catch (final NumberFormatException nfe) { 1302 if (name == null) { 1303 throw new IllegalNumberArgumentException(nfe); 1304 } else { 1305 throw new IllegalNumberArgumentException(name, nfe); 1306 } 1307 } 1308 1309 return type.cast(ret); 1310 } 1311 1312 /** 1313 * Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the 1314 * characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank 1315 * account number). 1316 * 1317 * <p> 1318 * We recommend to use the overloaded method {@link Check#isNumeric(CharSequence, String)} and pass as second 1319 * argument the name of the parameter to enhance the exception message. 1320 * 1321 * @param value 1322 * a readable sequence of {@code char} values which must be a number 1323 * @return the given string argument 1324 * @throws IllegalNumberArgumentException 1325 * if the given argument {@code value} is no number 1326 */ 1327 @ArgumentsChecked 1328 @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) 1329 public static <T extends CharSequence> T isNumeric(@Nonnull final T value) { 1330 return isNumeric(value, EMPTY_ARGUMENT_NAME); 1331 } 1332 1333 /** 1334 * Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the 1335 * characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank 1336 * account number). 1337 * 1338 * @param value 1339 * a readable sequence of {@code char} values which must be a number 1340 * @param name 1341 * name of object reference (in source code) 1342 * @return the given string argument 1343 * @throws IllegalNumberArgumentException 1344 * if the given argument {@code value} is no number 1345 */ 1346 @ArgumentsChecked 1347 @Throws({ IllegalNullArgumentException.class, IllegalNumericArgumentException.class }) 1348 public static <T extends CharSequence> T isNumeric(@Nonnull final T value, @Nullable final String name) { 1349 Check.notNull(value, "value"); 1350 if (!matches(NumericRegularExpressionHolder.getPattern(), value)) { 1351 throw new IllegalNumericArgumentException(name); 1352 } 1353 return value; 1354 } 1355 1356 /** 1357 * Ensures that a passed {@code byte} is less than another {@code byte}. 1358 * 1359 * @param expected 1360 * Expected value 1361 * @param check 1362 * Comparable to be checked 1363 * @return the passed {@code byte} argument {@code check} 1364 * 1365 * @throws IllegalNotLesserThanException 1366 * if the argument value {@code check} is not lesser than value {@code expected} 1367 */ 1368 @ArgumentsChecked 1369 @Throws(IllegalNotLesserThanException.class) 1370 public static byte lesserThan(@Nonnull final byte expected, @Nonnull final byte check) { 1371 if (expected <= check) { 1372 throw new IllegalNotLesserThanException(); 1373 } 1374 1375 return check; 1376 } 1377 1378 /** 1379 * Ensures that a passed {@code byte} is less than another {@code byte}. 1380 * 1381 * @param expected 1382 * Expected value 1383 * @param check 1384 * Comparable to be checked 1385 * @param message 1386 * an error message describing why the comparables must be less than a value (will be passed to 1387 * {@code IllegalNotLessThanException}) 1388 * @return the passed {@code Comparable} argument {@code check} 1389 * 1390 * @throws IllegalNotLesserThanException 1391 * if the argument value {@code check} is not lesser than value {@code expected} 1392 */ 1393 @ArgumentsChecked 1394 @Throws(IllegalNotLesserThanException.class) 1395 public static byte lesserThan(@Nonnull final byte expected, @Nonnull final byte check, final String message) { 1396 if (expected <= check) { 1397 throw new IllegalNotLesserThanException(message); 1398 } 1399 1400 return check; 1401 } 1402 1403 /** 1404 * Ensures that a passed {@code char} is less than another {@code char}. 1405 * 1406 * @param expected 1407 * Expected value 1408 * @param check 1409 * Comparable to be checked 1410 * @return the passed {@code char} argument {@code check} 1411 * 1412 * @throws IllegalNotLesserThanException 1413 * if the argument value {@code check} is not lesser than value {@code expected} 1414 */ 1415 @ArgumentsChecked 1416 @Throws(IllegalNotLesserThanException.class) 1417 public static char lesserThan(@Nonnull final char expected, @Nonnull final char check) { 1418 if (expected <= check) { 1419 throw new IllegalNotLesserThanException(); 1420 } 1421 1422 return check; 1423 } 1424 1425 /** 1426 * Ensures that a passed {@code char} is less than another {@code char}. 1427 * 1428 * @param expected 1429 * Expected value 1430 * @param check 1431 * Comparable to be checked 1432 * @param message 1433 * an error message describing why the comparables must be less than a value (will be passed to 1434 * {@code IllegalNotLessThanException}) 1435 * @return the passed {@code Comparable} argument {@code check} 1436 * 1437 * @throws IllegalNotLesserThanException 1438 * if the argument value {@code check} is not lesser than value {@code expected} 1439 */ 1440 @ArgumentsChecked 1441 @Throws(IllegalNotLesserThanException.class) 1442 public static char lesserThan(@Nonnull final char expected, @Nonnull final char check, final String message) { 1443 if (expected <= check) { 1444 throw new IllegalNotLesserThanException(message); 1445 } 1446 1447 return check; 1448 } 1449 1450 /** 1451 * Ensures that a passed {@code double} is less than another {@code double}. 1452 * 1453 * @param expected 1454 * Expected value 1455 * @param check 1456 * Comparable to be checked 1457 * @return the passed {@code double} argument {@code check} 1458 * 1459 * @throws IllegalNotLesserThanException 1460 * if the argument value {@code check} is not lesser than value {@code expected} 1461 */ 1462 @ArgumentsChecked 1463 @Throws(IllegalNotLesserThanException.class) 1464 public static double lesserThan(@Nonnull final double expected, @Nonnull final double check) { 1465 if (expected <= check) { 1466 throw new IllegalNotLesserThanException(); 1467 } 1468 1469 return check; 1470 } 1471 1472 /** 1473 * Ensures that a passed {@code double} is less than another {@code double}. 1474 * 1475 * @param expected 1476 * Expected value 1477 * @param check 1478 * Comparable to be checked 1479 * @param message 1480 * an error message describing why the comparables must be less than a value (will be passed to 1481 * {@code IllegalNotLessThanException}) 1482 * @return the passed {@code Comparable} argument {@code check} 1483 * 1484 * @throws IllegalNotLesserThanException 1485 * if the argument value {@code check} is not lesser than value {@code expected} 1486 */ 1487 @ArgumentsChecked 1488 @Throws(IllegalNotLesserThanException.class) 1489 public static double lesserThan(@Nonnull final double expected, @Nonnull final double check, final String message) { 1490 if (expected <= check) { 1491 throw new IllegalNotLesserThanException(message); 1492 } 1493 1494 return check; 1495 } 1496 1497 /** 1498 * Ensures that a passed {@code float} is less than another {@code float}. 1499 * 1500 * @param expected 1501 * Expected value 1502 * @param check 1503 * Comparable to be checked 1504 * @return the passed {@code float} argument {@code check} 1505 * 1506 * @throws IllegalNotLesserThanException 1507 * if the argument value {@code check} is not lesser than value {@code expected} 1508 */ 1509 @ArgumentsChecked 1510 @Throws(IllegalNotLesserThanException.class) 1511 public static float lesserThan(@Nonnull final float expected, @Nonnull final float check) { 1512 if (expected <= check) { 1513 throw new IllegalNotLesserThanException(); 1514 } 1515 1516 return check; 1517 } 1518 1519 /** 1520 * Ensures that a passed {@code float} is less than another {@code float}. 1521 * 1522 * @param expected 1523 * Expected value 1524 * @param check 1525 * Comparable to be checked 1526 * @param message 1527 * an error message describing why the comparables must be less than a value (will be passed to 1528 * {@code IllegalNotLessThanException}) 1529 * @return the passed {@code Comparable} argument {@code check} 1530 * 1531 * @throws IllegalNotLesserThanException 1532 * if the argument value {@code check} is not lesser than value {@code expected} 1533 */ 1534 @ArgumentsChecked 1535 @Throws(IllegalNotLesserThanException.class) 1536 public static float lesserThan(@Nonnull final float expected, @Nonnull final float check, final String message) { 1537 if (expected <= check) { 1538 throw new IllegalNotLesserThanException(message); 1539 } 1540 1541 return check; 1542 } 1543 1544 /** 1545 * Ensures that a passed {@code int} is less than another {@code int}. 1546 * 1547 * @param expected 1548 * Expected value 1549 * @param check 1550 * Comparable to be checked 1551 * @return the passed {@code int} argument {@code check} 1552 * 1553 * @throws IllegalNotLesserThanException 1554 * if the argument value {@code check} is not lesser than value {@code expected} 1555 */ 1556 @ArgumentsChecked 1557 @Throws(IllegalNotLesserThanException.class) 1558 public static int lesserThan(@Nonnull final int expected, @Nonnull final int check) { 1559 if (expected <= check) { 1560 throw new IllegalNotLesserThanException(); 1561 } 1562 1563 return check; 1564 } 1565 1566 /** 1567 * Ensures that a passed {@code int} is less than another {@code int}. 1568 * 1569 * @param expected 1570 * Expected value 1571 * @param check 1572 * Comparable to be checked 1573 * @param message 1574 * an error message describing why the comparables must be less than a value (will be passed to 1575 * {@code IllegalNotLessThanException}) 1576 * @return the passed {@code Comparable} argument {@code check} 1577 * 1578 * @throws IllegalNotLesserThanException 1579 * if the argument value {@code check} is not lesser than value {@code expected} 1580 */ 1581 @ArgumentsChecked 1582 @Throws(IllegalNotLesserThanException.class) 1583 public static int lesserThan(@Nonnull final int expected, @Nonnull final int check, final String message) { 1584 if (expected <= check) { 1585 throw new IllegalNotLesserThanException(message); 1586 } 1587 1588 return check; 1589 } 1590 1591 /** 1592 * Ensures that a passed {@code long} is less than another {@code long}. 1593 * 1594 * @param expected 1595 * Expected value 1596 * @param check 1597 * Comparable to be checked 1598 * @return the passed {@code long} argument {@code check} 1599 * 1600 * @throws IllegalNotLesserThanException 1601 * if the argument value {@code check} is not lesser than value {@code expected} 1602 */ 1603 @ArgumentsChecked 1604 @Throws(IllegalNotLesserThanException.class) 1605 public static long lesserThan(@Nonnull final long expected, @Nonnull final long check) { 1606 if (expected <= check) { 1607 throw new IllegalNotLesserThanException(); 1608 } 1609 1610 return check; 1611 } 1612 1613 /** 1614 * Ensures that a passed {@code long} is less than another {@code long}. 1615 * 1616 * @param expected 1617 * Expected value 1618 * @param check 1619 * Comparable to be checked 1620 * @param message 1621 * an error message describing why the comparables must be less than a value (will be passed to 1622 * {@code IllegalNotLessThanException}) 1623 * @return the passed {@code Comparable} argument {@code check} 1624 * 1625 * @throws IllegalNotLesserThanException 1626 * if the argument value {@code check} is not lesser than value {@code expected} 1627 */ 1628 @ArgumentsChecked 1629 @Throws(IllegalNotLesserThanException.class) 1630 public static long lesserThan(@Nonnull final long expected, @Nonnull final long check, final String message) { 1631 if (expected <= check) { 1632 throw new IllegalNotLesserThanException(message); 1633 } 1634 1635 return check; 1636 } 1637 1638 /** 1639 * Ensures that a passed {@code short} is less than another {@code short}. 1640 * 1641 * @param expected 1642 * Expected value 1643 * @param check 1644 * Comparable to be checked 1645 * @return the passed {@code short} argument {@code check} 1646 * 1647 * @throws IllegalNotLesserThanException 1648 * if the argument value {@code check} is not lesser than value {@code expected} 1649 */ 1650 @ArgumentsChecked 1651 @Throws(IllegalNotLesserThanException.class) 1652 public static short lesserThan(@Nonnull final short expected, @Nonnull final short check) { 1653 if (expected <= check) { 1654 throw new IllegalNotLesserThanException(); 1655 } 1656 1657 return check; 1658 } 1659 1660 /** 1661 * Ensures that a passed {@code short} is less than another {@code short}. 1662 * 1663 * @param expected 1664 * Expected value 1665 * @param check 1666 * Comparable to be checked 1667 * @param message 1668 * an error message describing why the comparables must be less than a value (will be passed to 1669 * {@code IllegalNotLessThanException}) 1670 * @return the passed {@code Comparable} argument {@code check} 1671 * 1672 * @throws IllegalNotLesserThanException 1673 * if the argument value {@code check} is not lesser than value {@code expected} 1674 */ 1675 @ArgumentsChecked 1676 @Throws(IllegalNotLesserThanException.class) 1677 public static short lesserThan(@Nonnull final short expected, @Nonnull final short check, final String message) { 1678 if (expected <= check) { 1679 throw new IllegalNotLesserThanException(message); 1680 } 1681 1682 return check; 1683 } 1684 1685 /** 1686 * Ensures that a passed {@code Comparable} is less than another {@code Comparable}. The comparison is made using 1687 * {@code expected.compareTo(check) <= 0}. 1688 * 1689 * @param expected 1690 * Expected value 1691 * @param check 1692 * Comparable to be checked 1693 * @return the passed {@code Comparable} argument {@code check} 1694 * 1695 * @throws IllegalNotLesserThanException 1696 * if the argument value {@code check} is not lesser than value {@code expected} when using method 1697 * {@code compareTo} 1698 */ 1699 @ArgumentsChecked 1700 @Throws({ IllegalNullArgumentException.class, IllegalNotLesserThanException.class }) 1701 public static <T extends Comparable<T>> T lesserThan(@Nonnull final T expected, @Nonnull final T check) { 1702 Check.notNull(expected, "expected"); 1703 Check.notNull(check, "check"); 1704 1705 if (expected.compareTo(check) <= 0) { 1706 throw new IllegalNotLesserThanException(); 1707 } 1708 1709 return check; 1710 } 1711 1712 /** 1713 * Ensures that a passed {@code Comparable} is less than another {@code Comparable}. The comparison is made using 1714 * {@code expected.compareTo(check) <= 0}. 1715 * 1716 * @param expected 1717 * Expected value 1718 * @param check 1719 * Comparable to be checked 1720 * @param message 1721 * an error message describing why the comparables must be less than a value (will be passed to 1722 * {@code IllegalNotLessThanException}) 1723 * @return the passed {@code Comparable} argument {@code check} 1724 * 1725 * @throws IllegalNotLesserThanException 1726 * if the argument value {@code check} is not lesser than value {@code expected} when using method 1727 * {@code compareTo} 1728 */ 1729 @ArgumentsChecked 1730 @Throws({ IllegalNullArgumentException.class, IllegalNotLesserThanException.class }) 1731 public static <T extends Comparable<T>> T lesserThan(@Nonnull final T expected, @Nonnull final T check, final String message) { 1732 Check.notNull(expected, "expected"); 1733 Check.notNull(check, "check"); 1734 1735 if (expected.compareTo(check) <= 0) { 1736 throw new IllegalNotLesserThanException(message); 1737 } 1738 1739 return check; 1740 } 1741 1742 /** 1743 * Checks whether a character sequence matches against a specified pattern or not. 1744 * 1745 * @param pattern 1746 * pattern, that the {@code chars} must correspond to 1747 * @param chars 1748 * a readable sequence of {@code char} values which should match the given pattern 1749 * @return {@code true} when {@code chars} matches against the passed {@code pattern}, otherwise {@code false} 1750 */ 1751 private static boolean matches(@Nonnull final Pattern pattern, @Nonnull final CharSequence chars) { 1752 return pattern.matcher(chars).matches(); 1753 } 1754 1755 /** 1756 * Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character 1757 * sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown. 1758 * 1759 * <p> 1760 * We recommend to use the overloaded method {@link Check#matchesPattern(Pattern, CharSequence, String)} and pass as 1761 * second argument the name of the parameter to enhance the exception message. 1762 * 1763 * @param pattern 1764 * pattern, that the {@code chars} must correspond to 1765 * @param chars 1766 * a readable sequence of {@code char} values which should match the given pattern 1767 * @return the passed {@code chars} that matches the given pattern 1768 * 1769 * @throws IllegalNullArgumentException 1770 * if the given argument {@code chars} is {@code null} 1771 * @throws IllegalPatternArgumentException 1772 * if the given {@code chars} that does not match the {@code pattern} 1773 */ 1774 @ArgumentsChecked 1775 @Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class }) 1776 public static <T extends CharSequence> T matchesPattern(@Nonnull final Pattern pattern, @Nonnull final T chars) { 1777 return matchesPattern(pattern, chars, EMPTY_ARGUMENT_NAME); 1778 } 1779 1780 /** 1781 * Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character 1782 * sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown. 1783 * 1784 * @param pattern 1785 * pattern, that the {@code chars} must correspond to 1786 * @param chars 1787 * a readable sequence of {@code char} values which should match the given pattern 1788 * @param name 1789 * name of object reference (in source code) 1790 * @return the passed {@code chars} that matches the given pattern 1791 * 1792 * @throws IllegalNullArgumentException 1793 * if the given argument {@code chars} is {@code null} 1794 * @throws IllegalPatternArgumentException 1795 * if the given {@code chars} that does not match the {@code pattern} 1796 */ 1797 @ArgumentsChecked 1798 @Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class }) 1799 public static <T extends CharSequence> T matchesPattern(@Nonnull final Pattern pattern, @Nonnull final T chars, 1800 @Nullable final String name) { 1801 Check.notNull(pattern, "pattern"); 1802 Check.notNull(chars, "chars"); 1803 if (!matches(pattern, chars)) { 1804 throw new IllegalPatternArgumentException(name, pattern); 1805 } 1806 return chars; 1807 } 1808 1809 /** 1810 * Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}. 1811 * 1812 * <p> 1813 * We recommend to use the overloaded method {@link Check#noNullElements(Iterable, String)} and pass as second 1814 * argument the name of the parameter to enhance the exception message. 1815 * 1816 * @param iterable 1817 * the iterable reference which should not contain {@code null} 1818 * @return the passed reference which contains no elements that are {@code null} 1819 * 1820 * @throws IllegalNullElementsException 1821 * if the given argument {@code iterable} contains elements that are {@code null} 1822 */ 1823 @ArgumentsChecked 1824 @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) 1825 public static <T extends Iterable<?>> T noNullElements(@Nonnull final T iterable) { 1826 return noNullElements(iterable, EMPTY_ARGUMENT_NAME); 1827 } 1828 1829 /** 1830 * Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}. 1831 * 1832 * @param iterable 1833 * the iterable reference which should not contain {@code null} 1834 * @param name 1835 * name of object reference (in source code) 1836 * @return the passed reference which contains no elements that are {@code null} 1837 * @throws IllegalNullElementsException 1838 * if the given argument {@code iterable} contains elements that are {@code null} 1839 */ 1840 @ArgumentsChecked 1841 @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) 1842 public static <T extends Iterable<?>> T noNullElements(@Nonnull final T iterable, final String name) { 1843 Check.notNull(iterable, "iterable"); 1844 for (final Object element : iterable) { 1845 if (element == null) { 1846 throw new IllegalNullElementsException(name); 1847 } 1848 } 1849 return iterable; 1850 } 1851 1852 /** 1853 * Ensures that an array does not contain {@code null}. 1854 * 1855 * <p> 1856 * We recommend to use the overloaded method {@link Check#noNullElements(Object[], String)} and pass as second 1857 * argument the name of the parameter to enhance the exception message. 1858 * 1859 * @param array 1860 * reference to an array 1861 * @return the passed reference which contains no elements that are {@code null} 1862 * @throws IllegalNullElementsException 1863 * if the given argument {@code array} contains {@code null} 1864 */ 1865 @ArgumentsChecked 1866 @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) 1867 public static <T> T[] noNullElements(@Nonnull final T[] array) { 1868 return noNullElements(array, EMPTY_ARGUMENT_NAME); 1869 } 1870 1871 /** 1872 * Ensures that an array does not contain {@code null}. 1873 * 1874 * @param array 1875 * reference to an array 1876 * @param name 1877 * name of object reference (in source code) 1878 * @return the passed reference which contains no elements that are {@code null} 1879 * @throws IllegalNullElementsException 1880 * if the given argument {@code array} contains {@code null} 1881 */ 1882 @ArgumentsChecked 1883 @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) 1884 public static <T> T[] noNullElements(@Nonnull final T[] array, @Nullable final String name) { 1885 Check.notNull(array, "array"); 1886 if (containsNullElements(array)) { 1887 throw new IllegalNullElementsException(name); 1888 } 1889 return array; 1890 } 1891 1892 /** 1893 * Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the 1894 * emptiness. 1895 * 1896 * <p> 1897 * We recommend to use the overloaded method {@link Check#notEmpty(boolean, String)} and pass as second argument the 1898 * name of the parameter to enhance the exception message. 1899 * 1900 * @param expression 1901 * the result of the expression to verify the emptiness of a reference ({@code true} means empty, 1902 * {@code false} means not empty) 1903 * 1904 * @throws IllegalNullArgumentException 1905 * if the given argument {@code reference} is {@code null} 1906 * @throws IllegalEmptyArgumentException 1907 * if the given argument {@code reference} is empty 1908 */ 1909 @ArgumentsChecked 1910 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 1911 public static void notEmpty(final boolean expression) { 1912 notEmpty(expression, EMPTY_ARGUMENT_NAME); 1913 } 1914 1915 /** 1916 * Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the 1917 * emptiness. 1918 * 1919 * @param expression 1920 * the result of the expression to verify the emptiness of a reference ({@code true} means empty, 1921 * {@code false} means not empty) 1922 * @param name 1923 * name of object reference (in source code) 1924 * 1925 * @throws IllegalEmptyArgumentException 1926 * if the given argument {@code reference} is empty 1927 */ 1928 @ArgumentsChecked 1929 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 1930 public static void notEmpty(final boolean expression, @Nullable final String name) { 1931 if (expression) { 1932 throw new IllegalEmptyArgumentException(name); 1933 } 1934 } 1935 1936 /** 1937 * Ensures that a passed string as a parameter of the calling method is not empty. 1938 * 1939 * <p> 1940 * We recommend to use the overloaded method {@link Check#notEmpty(CharSequence, String)} and pass as second 1941 * argument the name of the parameter to enhance the exception message. 1942 * 1943 * @param chars 1944 * a readable sequence of {@code char} values which should not be empty 1945 * @return the passed reference that is not empty 1946 * @throws IllegalNullArgumentException 1947 * if the given argument {@code reference} is {@code null} 1948 * @throws IllegalEmptyArgumentException 1949 * if the given argument {@code reference} is empty 1950 */ 1951 @ArgumentsChecked 1952 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 1953 public static <T extends CharSequence> T notEmpty(@Nonnull final T chars) { 1954 notNull(chars); 1955 notEmpty(chars, chars.length() == 0, EMPTY_ARGUMENT_NAME); 1956 return chars; 1957 } 1958 1959 /** 1960 * Ensures that a passed collection as a parameter of the calling method is not empty. 1961 * 1962 * <p> 1963 * We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument 1964 * the name of the parameter to enhance the exception message. 1965 * 1966 * @param collection 1967 * a collection which should not be empty 1968 * @return the passed reference that is not empty 1969 * @throws IllegalNullArgumentException 1970 * if the given argument {@code collection} is {@code null} 1971 * @throws IllegalEmptyArgumentException 1972 * if the given argument {@code collection} is empty 1973 */ 1974 @ArgumentsChecked 1975 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 1976 public static <T extends Collection<?>> T notEmpty(@Nonnull final T collection) { 1977 notNull(collection); 1978 notEmpty(collection, collection.isEmpty(), EMPTY_ARGUMENT_NAME); 1979 return collection; 1980 } 1981 1982 /** 1983 * Ensures that a passed map as a parameter of the calling method is not empty. 1984 * 1985 * <p> 1986 * We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument 1987 * the name of the parameter to enhance the exception message. 1988 * 1989 * @param map 1990 * a map which should not be empty 1991 * @return the passed reference that is not empty 1992 * @throws IllegalNullArgumentException 1993 * if the given argument {@code map} is {@code null} 1994 * @throws IllegalEmptyArgumentException 1995 * if the given argument {@code map} is empty 1996 */ 1997 @ArgumentsChecked 1998 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 1999 public static <T extends Map<?, ?>> T notEmpty(@Nonnull final T map) { 2000 notNull(map); 2001 notEmpty(map, map.isEmpty(), EMPTY_ARGUMENT_NAME); 2002 return map; 2003 } 2004 2005 /** 2006 * Ensures that an object reference passed as a parameter to the calling method is not empty. The passed boolean 2007 * value is the result of checking whether the reference is empty or not. 2008 * 2009 * <p> 2010 * The following example describes how to use it. 2011 * 2012 * <pre> 2013 * @ArgumentsChecked 2014 * public setText(String text) { 2015 * Check.notEmpty(text, text.isEmpty(), "text"); 2016 * this.text = text; 2017 * } 2018 * </pre> 2019 * 2020 * @param reference 2021 * an object reference which should not be empty 2022 * @param expression 2023 * the result of the expression to verify the emptiness of a reference ({@code true} means empty, 2024 * {@code false} means not empty) 2025 * @param name 2026 * name of object reference (in source code) 2027 * @return the passed reference that is not empty 2028 * @throws IllegalNullArgumentException 2029 * if the given argument {@code reference} is {@code null} 2030 * @throws IllegalEmptyArgumentException 2031 * if the given argument {@code reference} is empty 2032 */ 2033 @ArgumentsChecked 2034 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 2035 public static <T> T notEmpty(@Nonnull final T reference, final boolean expression, @Nullable final String name) { 2036 notNull(reference, name); 2037 if (expression) { 2038 throw new IllegalEmptyArgumentException(name); 2039 } 2040 return reference; 2041 } 2042 2043 /** 2044 * Ensures that a passed string as a parameter of the calling method is not empty. 2045 * 2046 * <p> 2047 * The following example describes how to use it. 2048 * 2049 * <pre> 2050 * @ArgumentsChecked 2051 * public setText(String text) { 2052 * this.text = Check.notEmpty(text, "text"); 2053 * } 2054 * </pre> 2055 * 2056 * @param chars 2057 * a readable sequence of {@code char} values which should not be empty 2058 * @param name 2059 * name of object reference (in source code) 2060 * @return the passed reference that is not empty 2061 * @throws IllegalNullArgumentException 2062 * if the given argument {@code string} is {@code null} 2063 * @throws IllegalEmptyArgumentException 2064 * if the given argument {@code string} is empty 2065 */ 2066 @ArgumentsChecked 2067 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 2068 public static <T extends CharSequence> T notEmpty(@Nonnull final T chars, @Nullable final String name) { 2069 notNull(chars, name); 2070 notEmpty(chars, chars.length() == 0, name); 2071 return chars; 2072 } 2073 2074 /** 2075 * Ensures that a passed map as a parameter of the calling method is not empty. 2076 * 2077 * <p> 2078 * We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument 2079 * the name of the parameter to enhance the exception message. 2080 * 2081 * @param map 2082 * a map which should not be empty 2083 * @param name 2084 * name of object reference (in source code) 2085 * @return the passed reference that is not empty 2086 * @throws IllegalNullArgumentException 2087 * if the given argument {@code map} is {@code null} 2088 * @throws IllegalEmptyArgumentException 2089 * if the given argument {@code map} is empty 2090 */ 2091 @ArgumentsChecked 2092 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 2093 public static <T extends Map<?, ?>> T notEmpty(@Nonnull final T map, @Nullable final String name) { 2094 notNull(map); 2095 notEmpty(map, map.isEmpty(), name); 2096 return map; 2097 } 2098 2099 /** 2100 * Ensures that a passed collection as a parameter of the calling method is not empty. 2101 * 2102 * <p> 2103 * The following example describes how to use it. 2104 * 2105 * <pre> 2106 * @ArgumentsChecked 2107 * public setCollection(Collection<String> collection) { 2108 * this.collection = Check.notEmpty(collection, "collection"); 2109 * } 2110 * </pre> 2111 * 2112 * @param collection 2113 * a collection which should not be empty 2114 * @param name 2115 * name of object reference (in source code) 2116 * @return the passed reference that is not empty 2117 * @throws IllegalNullArgumentException 2118 * if the given argument {@code collection} is {@code null} 2119 * @throws IllegalEmptyArgumentException 2120 * if the given argument {@code collection} is empty 2121 */ 2122 @ArgumentsChecked 2123 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 2124 public static <T extends Collection<?>> T notEmpty(@Nonnull final T collection, @Nullable final String name) { 2125 notNull(collection, name); 2126 notEmpty(collection, collection.isEmpty(), name); 2127 return collection; 2128 } 2129 2130 /** 2131 * Ensures that a passed map as a parameter of the calling method is not empty. 2132 * 2133 * <p> 2134 * We recommend to use the overloaded method {@link Check#notEmpty(Object[], String)} and pass as second argument 2135 * the name of the parameter to enhance the exception message. 2136 * 2137 * @param array 2138 * a map which should not be empty 2139 * @return the passed reference that is not empty 2140 * @throws IllegalNullArgumentException 2141 * if the given argument {@code array} is {@code null} 2142 * @throws IllegalEmptyArgumentException 2143 * if the given argument {@code array} is empty 2144 */ 2145 @ArgumentsChecked 2146 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 2147 public static <T> T[] notEmpty(@Nonnull final T[] array) { 2148 notNull(array); 2149 notEmpty(array, array.length == 0, EMPTY_ARGUMENT_NAME); 2150 return array; 2151 } 2152 2153 /** 2154 * Ensures that a passed map as a parameter of the calling method is not empty. 2155 * 2156 * @param array 2157 * a map which should not be empty 2158 * @param name 2159 * name of object reference (in source code) 2160 * @return the passed reference that is not empty 2161 * @throws IllegalNullArgumentException 2162 * if the given argument {@code array} is {@code null} 2163 * @throws IllegalEmptyArgumentException 2164 * if the given argument {@code array} is empty 2165 */ 2166 @ArgumentsChecked 2167 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) 2168 public static <T> T[] notEmpty(@Nonnull final T[] array, @Nullable final String name) { 2169 notNull(array); 2170 notEmpty(array, array.length == 0, EMPTY_ARGUMENT_NAME); 2171 return array; 2172 } 2173 2174 /** 2175 * Do not perform any check and just return {@code t}. 2176 * 2177 * This is useful if you have several checks on some arguments, but do not check other arguments on purpose. This 2178 * checks helps to document that a check was omitted on purpose instead of forgotten. 2179 * 2180 * @param t 2181 * any object 2182 * @return t 2183 */ 2184 public static <T> T nothing(final T t) { 2185 return t; 2186 } 2187 2188 /** 2189 * Ensures that a double argument is not NaN (not a number). 2190 * 2191 * <p> 2192 * We recommend to use the overloaded method {@link Check#notNaN(double, String)} and pass as second argument the 2193 * name of the parameter to enhance the exception message. 2194 * 2195 * @see java.lang.Double#NaN 2196 * 2197 * @param value 2198 * value which should not be NaN 2199 * @return the given double value 2200 * @throws IllegalNaNArgumentException 2201 * if the given argument {@code value} is NaN 2202 */ 2203 @Throws(IllegalNaNArgumentException.class) 2204 public static double notNaN(final double value) { 2205 return notNaN(value, EMPTY_ARGUMENT_NAME); 2206 } 2207 2208 /** 2209 * Ensures that a double argument is not NaN (not a number). 2210 * 2211 * @see java.lang.Double#NaN 2212 * 2213 * @param value 2214 * value which should not be NaN 2215 * @param name 2216 * name of object reference (in source code) 2217 * @return the given double value 2218 * @throws IllegalNaNArgumentException 2219 * if the given argument {@code value} is NaN 2220 */ 2221 @Throws(IllegalNaNArgumentException.class) 2222 public static double notNaN(final double value, @Nullable final String name) { 2223 // most efficient check for NaN, see Double.isNaN(value)) 2224 if (value != value) { 2225 throw new IllegalNaNArgumentException(name); 2226 } 2227 return value; 2228 } 2229 2230 /** 2231 * Ensures that a double argument is not NaN (not a number). 2232 * 2233 * <p> 2234 * We recommend to use the overloaded method {@link Check#notNaN(float, String)} and pass as second argument the 2235 * name of the parameter to enhance the exception message. 2236 * 2237 * @see java.lang.Float#NaN 2238 * 2239 * @param value 2240 * value which should not be NaN 2241 * @return the given double value 2242 * @throws IllegalNaNArgumentException 2243 * if the given argument {@code value} is NaN 2244 */ 2245 @Throws(IllegalNaNArgumentException.class) 2246 public static float notNaN(final float value) { 2247 return notNaN(value, EMPTY_ARGUMENT_NAME); 2248 } 2249 2250 /** 2251 * Ensures that a double argument is not NaN (not a number). 2252 * 2253 * @see java.lang.Float#NaN 2254 * 2255 * @param value 2256 * value which should not be NaN 2257 * @param name 2258 * name of object reference (in source code) 2259 * @return the given float value 2260 * @throws IllegalNaNArgumentException 2261 * if the given argument {@code value} is NaN 2262 */ 2263 @Throws(IllegalNaNArgumentException.class) 2264 public static float notNaN(final float value, @Nullable final String name) { 2265 // most efficient check for NaN, see Float.isNaN(value)) 2266 if (value != value) { 2267 throw new IllegalNaNArgumentException(name); 2268 } 2269 return value; 2270 } 2271 2272 /** 2273 * Ensures that an double reference passed as a parameter to the calling method is not smaller than {@code 0}. 2274 * 2275 * <p> 2276 * We recommend to use the overloaded method {@link Check#notNegative(double, String)} and pass as second argument 2277 * the name of the parameter to enhance the exception message. 2278 * 2279 * @param value 2280 * a number 2281 * @return the non-null reference that was validated 2282 * @throws IllegalNegativeArgumentException 2283 * if the given argument {@code reference} is smaller than {@code 0} 2284 */ 2285 @Throws(IllegalNegativeArgumentException.class) 2286 public static double notNegative(@Nonnull final double value) { 2287 if (value < 0.0) { 2288 throw new IllegalNegativeArgumentException(); 2289 } 2290 return value; 2291 } 2292 2293 /** 2294 * Ensures that an double reference passed as a parameter to the calling method is not smaller than {@code 0}. 2295 * 2296 * @param value 2297 * a number 2298 * @param name 2299 * name of the number reference (in source code) 2300 * @return the non-null reference that was validated 2301 * @throws IllegalNullArgumentException 2302 * if the given argument {@code reference} is smaller than {@code 0} 2303 */ 2304 @Throws(IllegalNegativeArgumentException.class) 2305 public static double notNegative(@Nonnull final double value, @Nullable final String name) { 2306 if (value < 0.0) { 2307 throw new IllegalNegativeArgumentException(name); 2308 } 2309 return value; 2310 } 2311 2312 /** 2313 * Ensures that an float reference passed as a parameter to the calling method is not smaller than {@code 0}. 2314 * 2315 * <p> 2316 * We recommend to use the overloaded method {@link Check#notNegative(float, String)} and pass as second argument 2317 * the name of the parameter to enhance the exception message. 2318 * 2319 * @param value 2320 * a number 2321 * @return the non-null reference that was validated 2322 * @throws IllegalNegativeArgumentException 2323 * if the given argument {@code reference} is smaller than {@code 0} 2324 */ 2325 @Throws(IllegalNegativeArgumentException.class) 2326 public static float notNegative(@Nonnull final float value) { 2327 if (value < 0.0f) { 2328 throw new IllegalNegativeArgumentException(); 2329 } 2330 return value; 2331 } 2332 2333 /** 2334 * Ensures that an float reference passed as a parameter to the calling method is not smaller than {@code 0}. 2335 * 2336 * @param value 2337 * a number 2338 * @param name 2339 * name of the number reference (in source code) 2340 * @return the non-null reference that was validated 2341 * @throws IllegalNullArgumentException 2342 * if the given argument {@code reference} is smaller than {@code 0} 2343 */ 2344 @Throws(IllegalNegativeArgumentException.class) 2345 public static float notNegative(@Nonnull final float value, @Nullable final String name) { 2346 if (value < 0.0f) { 2347 throw new IllegalNegativeArgumentException(name); 2348 } 2349 return value; 2350 } 2351 2352 /** 2353 * Ensures that an integer reference passed as a parameter to the calling method is not smaller than {@code 0}. 2354 * 2355 * <p> 2356 * We recommend to use the overloaded method {@link Check#notNegative(int, String)} and pass as second argument the 2357 * name of the parameter to enhance the exception message. 2358 * 2359 * @param value 2360 * a number 2361 * @return the non-null reference that was validated 2362 * @throws IllegalNegativeArgumentException 2363 * if the given argument {@code reference} is smaller than {@code 0} 2364 */ 2365 @Throws(IllegalNegativeArgumentException.class) 2366 public static int notNegative(@Nonnull final int value) { 2367 if (value < 0) { 2368 throw new IllegalNegativeArgumentException(); 2369 } 2370 return value; 2371 } 2372 2373 /** 2374 * Ensures that an integer reference passed as a parameter to the calling method is not smaller than {@code 0}. 2375 * 2376 * @param value 2377 * a number 2378 * @param name 2379 * name of the number reference (in source code) 2380 * @return the non-null reference that was validated 2381 * @throws IllegalNullArgumentException 2382 * if the given argument {@code reference} is smaller than {@code 0} 2383 */ 2384 @Throws(IllegalNegativeArgumentException.class) 2385 public static int notNegative(@Nonnull final int value, @Nullable final String name) { 2386 if (value < 0) { 2387 throw new IllegalNegativeArgumentException(name); 2388 } 2389 return value; 2390 } 2391 2392 /** 2393 * Ensures that an long reference passed as a parameter to the calling method is not smaller than {@code 0}. 2394 * 2395 * <p> 2396 * We recommend to use the overloaded method {@link Check#notNegative(long, String)} and pass as second argument the 2397 * name of the parameter to enhance the exception message. 2398 * 2399 * @param value 2400 * a number 2401 * @return the non-null reference that was validated 2402 * @throws IllegalNegativeArgumentException 2403 * if the given argument {@code reference} is smaller than {@code 0} 2404 */ 2405 @Throws(IllegalNegativeArgumentException.class) 2406 public static long notNegative(@Nonnull final long value) { 2407 if (value < 0L) { 2408 throw new IllegalNegativeArgumentException(); 2409 } 2410 return value; 2411 } 2412 2413 /** 2414 * Ensures that an long reference passed as a parameter to the calling method is not smaller than {@code 0}. 2415 * 2416 * @param value 2417 * a number 2418 * @param name 2419 * name of the number reference (in source code) 2420 * @return the non-null reference that was validated 2421 * @throws IllegalNullArgumentException 2422 * if the given argument {@code reference} is smaller than {@code 0} 2423 */ 2424 @Throws(IllegalNegativeArgumentException.class) 2425 public static long notNegative(@Nonnull final long value, @Nullable final String name) { 2426 if (value < 0L) { 2427 throw new IllegalNegativeArgumentException(name); 2428 } 2429 return value; 2430 } 2431 2432 /** 2433 * Ensures that an short reference passed as a parameter to the calling method is not smaller than {@code 0}. 2434 * 2435 * <p> 2436 * We recommend to use the overloaded method {@link Check#notNegative(short, String)} and pass as second argument 2437 * the name of the parameter to enhance the exception message. 2438 * 2439 * @param value 2440 * a number 2441 * @return the non-null reference that was validated 2442 * @throws IllegalNegativeArgumentException 2443 * if the given argument {@code reference} is smaller than {@code 0} 2444 */ 2445 @Throws(IllegalNegativeArgumentException.class) 2446 public static short notNegative(@Nonnull final short value) { 2447 if (value < (short) 0) { 2448 throw new IllegalNegativeArgumentException(); 2449 } 2450 return value; 2451 } 2452 2453 /** 2454 * Ensures that an short reference passed as a parameter to the calling method is not smaller than {@code 0}. 2455 * 2456 * @param value 2457 * a number 2458 * @param name 2459 * name of the number reference (in source code) 2460 * @return the non-null reference that was validated 2461 * @throws IllegalNullArgumentException 2462 * if the given argument {@code reference} is smaller than {@code 0} 2463 */ 2464 @Throws(IllegalNegativeArgumentException.class) 2465 public static short notNegative(@Nonnull final short value, @Nullable final String name) { 2466 if (value < (short) 0) { 2467 throw new IllegalNegativeArgumentException(name); 2468 } 2469 return value; 2470 } 2471 2472 /** 2473 * Ensures that an object reference passed as a parameter to the calling method is not {@code null}. 2474 * 2475 * <p> 2476 * We recommend to use the overloaded method {@link Check#notNull(Object, String)} and pass as second argument the 2477 * name of the parameter to enhance the exception message. 2478 * 2479 * @param reference 2480 * an object reference 2481 * @return the non-null reference that was validated 2482 * @throws IllegalNullArgumentException 2483 * if the given argument {@code reference} is {@code null} 2484 */ 2485 @Throws(IllegalNullArgumentException.class) 2486 public static <T> T notNull(@Nonnull final T reference) { 2487 if (reference == null) { 2488 throw new IllegalNullArgumentException(); 2489 } 2490 return reference; 2491 } 2492 2493 /** 2494 * Ensures that an object reference passed as a parameter to the calling method is not {@code null}. 2495 * 2496 * @param reference 2497 * an object reference 2498 * @param name 2499 * name of object reference (in source code) 2500 * @return the non-null reference that was validated 2501 * @throws IllegalNullArgumentException 2502 * if the given argument {@code reference} is {@code null} 2503 */ 2504 @Throws(IllegalNullArgumentException.class) 2505 public static <T> T notNull(@Nonnull final T reference, @Nullable final String name) { 2506 if (reference == null) { 2507 throw new IllegalNullArgumentException(name); 2508 } 2509 return reference; 2510 } 2511 2512 /** 2513 * Ensures that an double reference passed as a parameter to the calling method is not greater than {@code 0}. 2514 * 2515 * <p> 2516 * We recommend to use the overloaded method {@link Check#notPositive(double, String)} and pass as second argument 2517 * the name of the parameter to enhance the exception message. 2518 * 2519 * @param value 2520 * a number 2521 * @return the non-null reference that was validated 2522 * @throws IllegalPositiveArgumentException 2523 * if the given argument {@code reference} is smaller than {@code 0} 2524 */ 2525 @Throws(IllegalPositiveArgumentException.class) 2526 public static double notPositive(@Nonnull final double value) { 2527 if (value > 0.0) { 2528 throw new IllegalPositiveArgumentException(); 2529 } 2530 return value; 2531 } 2532 2533 /** 2534 * Ensures that an double reference passed as a parameter to the calling method is not greater than {@code 0}. 2535 * 2536 * @param value 2537 * a number 2538 * @param name 2539 * name of the number reference (in source code) 2540 * @return the non-null reference that was validated 2541 * @throws IllegalNullArgumentException 2542 * if the given argument {@code reference} is smaller than {@code 0} 2543 */ 2544 @Throws(IllegalPositiveArgumentException.class) 2545 public static double notPositive(@Nonnull final double value, @Nullable final String name) { 2546 if (value > 0.0) { 2547 throw new IllegalPositiveArgumentException(name); 2548 } 2549 return value; 2550 } 2551 2552 /** 2553 * Ensures that an float reference passed as a parameter to the calling method is not greater than {@code 0}. 2554 * 2555 * <p> 2556 * We recommend to use the overloaded method {@link Check#notPositive(float, String)} and pass as second argument 2557 * the name of the parameter to enhance the exception message. 2558 * 2559 * @param value 2560 * a number 2561 * @return the non-null reference that was validated 2562 * @throws IllegalPositiveArgumentException 2563 * if the given argument {@code reference} is smaller than {@code 0} 2564 */ 2565 @Throws(IllegalPositiveArgumentException.class) 2566 public static float notPositive(@Nonnull final float value) { 2567 if (value > 0.0f) { 2568 throw new IllegalPositiveArgumentException(); 2569 } 2570 return value; 2571 } 2572 2573 /** 2574 * Ensures that an float reference passed as a parameter to the calling method is not greater than {@code 0}. 2575 * 2576 * @param value 2577 * a number 2578 * @param name 2579 * name of the number reference (in source code) 2580 * @return the non-null reference that was validated 2581 * @throws IllegalNullArgumentException 2582 * if the given argument {@code reference} is smaller than {@code 0} 2583 */ 2584 @Throws(IllegalPositiveArgumentException.class) 2585 public static float notPositive(@Nonnull final float value, @Nullable final String name) { 2586 if (value > 0.0f) { 2587 throw new IllegalPositiveArgumentException(name); 2588 } 2589 return value; 2590 } 2591 2592 /** 2593 * Ensures that an integer reference passed as a parameter to the calling method is not greater than {@code 0}. 2594 * 2595 * <p> 2596 * We recommend to use the overloaded method {@link Check#notPositive(int, String)} and pass as second argument the 2597 * name of the parameter to enhance the exception message. 2598 * 2599 * @param value 2600 * a number 2601 * @return the non-null reference that was validated 2602 * @throws IllegalPositiveArgumentException 2603 * if the given argument {@code reference} is smaller than {@code 0} 2604 */ 2605 @Throws(IllegalPositiveArgumentException.class) 2606 public static int notPositive(@Nonnull final int value) { 2607 if (value > 0) { 2608 throw new IllegalPositiveArgumentException(); 2609 } 2610 return value; 2611 } 2612 2613 /** 2614 * Ensures that an integer reference passed as a parameter to the calling method is not greater than {@code 0}. 2615 * 2616 * @param value 2617 * a number 2618 * @param name 2619 * name of the number reference (in source code) 2620 * @return the non-null reference that was validated 2621 * @throws IllegalNullArgumentException 2622 * if the given argument {@code reference} is smaller than {@code 0} 2623 */ 2624 @Throws(IllegalPositiveArgumentException.class) 2625 public static int notPositive(@Nonnull final int value, @Nullable final String name) { 2626 if (value > 0) { 2627 throw new IllegalPositiveArgumentException(name); 2628 } 2629 return value; 2630 } 2631 2632 /** 2633 * Ensures that an long reference passed as a parameter to the calling method is not greater than {@code 0}. 2634 * 2635 * <p> 2636 * We recommend to use the overloaded method {@link Check#notPositive(long, String)} and pass as second argument the 2637 * name of the parameter to enhance the exception message. 2638 * 2639 * @param value 2640 * a number 2641 * @return the non-null reference that was validated 2642 * @throws IllegalPositiveArgumentException 2643 * if the given argument {@code reference} is smaller than {@code 0} 2644 */ 2645 @Throws(IllegalPositiveArgumentException.class) 2646 public static long notPositive(@Nonnull final long value) { 2647 if (value > 0L) { 2648 throw new IllegalPositiveArgumentException(); 2649 } 2650 return value; 2651 } 2652 2653 /** 2654 * Ensures that an long reference passed as a parameter to the calling method is not greater than {@code 0}. 2655 * 2656 * @param value 2657 * a number 2658 * @param name 2659 * name of the number reference (in source code) 2660 * @return the non-null reference that was validated 2661 * @throws IllegalNullArgumentException 2662 * if the given argument {@code reference} is smaller than {@code 0} 2663 */ 2664 @Throws(IllegalPositiveArgumentException.class) 2665 public static long notPositive(@Nonnull final long value, @Nullable final String name) { 2666 if (value > 0L) { 2667 throw new IllegalPositiveArgumentException(name); 2668 } 2669 return value; 2670 } 2671 2672 /** 2673 * Ensures that an short reference passed as a parameter to the calling method is not greater than {@code 0}. 2674 * 2675 * <p> 2676 * We recommend to use the overloaded method {@link Check#notPositive(short, String)} and pass as second argument 2677 * the name of the parameter to enhance the exception message. 2678 * 2679 * @param value 2680 * a number 2681 * @return the non-null reference that was validated 2682 * @throws IllegalPositiveArgumentException 2683 * if the given argument {@code reference} is smaller than {@code 0} 2684 */ 2685 @Throws(IllegalPositiveArgumentException.class) 2686 public static short notPositive(@Nonnull final short value) { 2687 if (value > (short) 0) { 2688 throw new IllegalPositiveArgumentException(); 2689 } 2690 return value; 2691 } 2692 2693 /** 2694 * Ensures that an short reference passed as a parameter to the calling method is not greater than {@code 0}. 2695 * 2696 * @param value 2697 * a number 2698 * @param name 2699 * name of the number reference (in source code) 2700 * @return the non-null reference that was validated 2701 * @throws IllegalNullArgumentException 2702 * if the given argument {@code reference} is smaller than {@code 0} 2703 */ 2704 @Throws(IllegalPositiveArgumentException.class) 2705 public static short notPositive(@Nonnull final short value, @Nullable final String name) { 2706 if (value > (short) 0) { 2707 throw new IllegalPositiveArgumentException(name); 2708 } 2709 return value; 2710 } 2711 2712 /** 2713 * Ensures that a given position index is valid within the size of an array, list or string ... 2714 * 2715 * @param index 2716 * index of an array, list or string 2717 * @param size 2718 * size of an array list or string 2719 * @return the index 2720 * 2721 * @throws IllegalPositionIndexException 2722 * if the index is not a valid position index within an array, list or string of size <em>size</em> 2723 * 2724 */ 2725 @Throws(IllegalPositionIndexException.class) 2726 public static int positionIndex(final int index, final int size) { 2727 final boolean isIndexValid = (size >= 0) && (index >= 0) && (index < size); 2728 2729 if (!isIndexValid) { 2730 throw new IllegalPositionIndexException(index, size); 2731 } 2732 2733 return index; 2734 } 2735 2736 /** 2737 * Ensures that the given arguments are a valid range. 2738 * 2739 * A range (<em>start</em>, <em>end</em>, <em>size</em>) is valid if the following conditions are {@code true}: 2740 * <ul> 2741 * <li>start <= size</li> 2742 * <li>end <= size</li> 2743 * <li>start <= end</li> 2744 * <li>size >= 0</li> 2745 * <li>start >= 0</li> 2746 * <li>end >= 0</li> 2747 * </ul> 2748 * 2749 * @param start 2750 * the start value of the range (must be a positive integer or 0) 2751 * @param end 2752 * the end value of the range (must be a positive integer or 0) 2753 * @param size 2754 * the size value of the range (must be a positive integer or 0) 2755 * 2756 * @throws IllegalRangeException 2757 * if the given arguments do not form a valid range 2758 */ 2759 @Throws(IllegalRangeException.class) 2760 public static void range(@Nonnegative final int start, @Nonnegative final int end, @Nonnegative final int size) { 2761 final boolean rangeIsValid = (start <= size) && (end <= size) && (start <= end); 2762 final boolean inputValuesAreValid = (size >= 0) && (start >= 0) && (end >= 0); 2763 2764 if (!rangeIsValid || !inputValuesAreValid) { 2765 throw new IllegalRangeException(start, end, size); 2766 } 2767 } 2768 2769 /** 2770 * Ensures that a given state is {@code true}. 2771 * 2772 * <p> 2773 * We recommend to use the overloaded method {@link Check#stateIsTrue(boolean, String)} and pass as second argument 2774 * the name of the parameter to enhance the exception message. A better way is to create specific exceptions (with a 2775 * good wording) for your case and to use the overloaded method {@link Check#stateIsTrue(boolean, Class)} and pass 2776 * as second argument your exception. 2777 * 2778 * @param expression 2779 * an expression that must be true to indicate a valid state 2780 * 2781 * @throws IllegalStateOfArgumentException 2782 * if the given arguments caused an invalid state 2783 */ 2784 @Throws(IllegalStateOfArgumentException.class) 2785 public static void stateIsTrue(final boolean expression) { 2786 if (!expression) { 2787 throw new IllegalStateOfArgumentException(); 2788 } 2789 } 2790 2791 /** 2792 * Ensures that a given state is {@code true} and allows to specify the class of exception which is thrown in case 2793 * the state is not {@code true}. 2794 * 2795 * @param expression 2796 * an expression that must be {@code true} to indicate a valid state 2797 * @param clazz 2798 * an subclass of {@link RuntimeException} which will be thrown if the given state is not valid 2799 * @throws clazz 2800 * a new instance of {@code clazz} if the given arguments caused an invalid state 2801 * @throws RuntimeInstantiationException 2802 * <strong>Attention</strong>: Be aware, that a {@code RuntimeInstantiationException} can be thrown when 2803 * the given {@code clazz} cannot be instantiated 2804 */ 2805 @ArgumentsChecked 2806 @Throws({ IllegalNullArgumentException.class, RuntimeInstantiationException.class }) 2807 public static void stateIsTrue(final boolean expression, final Class<? extends RuntimeException> clazz) { 2808 Check.notNull(clazz, "clazz"); 2809 2810 if (!expression) { 2811 RuntimeException re; 2812 try { 2813 re = clazz.newInstance(); 2814 } catch (final InstantiationException e) { 2815 throw new RuntimeInstantiationException(clazz.getSimpleName(), e); 2816 } catch (final IllegalAccessException e) { 2817 throw new RuntimeInstantiationException(clazz.getSimpleName(), e); 2818 } 2819 throw re; 2820 } 2821 } 2822 2823 /** 2824 * Ensures that a given state is {@code true}. 2825 * 2826 * @param expression 2827 * an expression that must be {@code true} to indicate a valid state 2828 * @param description 2829 * will be used in the error message to describe why the arguments caused an invalid state 2830 * @throws IllegalStateOfArgumentException 2831 * if the given arguments caused an invalid state 2832 */ 2833 @Throws(IllegalStateOfArgumentException.class) 2834 public static void stateIsTrue(final boolean expression, @Nonnull final String description) { 2835 if (!expression) { 2836 throw new IllegalStateOfArgumentException(description); 2837 } 2838 } 2839 2840 /** 2841 * Ensures that a given state is {@code true} 2842 * 2843 * @param expression 2844 * an expression that must be {@code true} to indicate a valid state 2845 * @param descriptionTemplate 2846 * format string template that explains why the state is invalid 2847 * @param descriptionTemplateArgs 2848 * format string template arguments to explain why the state is invalid 2849 * @throws IllegalStateOfArgumentException 2850 * if the given arguments caused an invalid state 2851 */ 2852 @Throws(IllegalStateOfArgumentException.class) 2853 public static void stateIsTrue(final boolean expression, @Nonnull final String descriptionTemplate, 2854 final Object... descriptionTemplateArgs) { 2855 if (!expression) { 2856 throw new IllegalStateOfArgumentException(descriptionTemplate, descriptionTemplateArgs); 2857 } 2858 } 2859 2860 /** 2861 * <strong>Attention:</strong> This class is not intended to create objects from it. 2862 */ 2863 private Check() { 2864 // This class is not intended to create objects from it. 2865 } 2866 2867 }