001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.base; 016 017import com.google.common.annotations.GwtCompatible; 018import com.google.errorprone.annotations.CanIgnoreReturnValue; 019import javax.annotation.Nullable; 020 021/** 022 * Static convenience methods that help a method or constructor check whether it was invoked 023 * correctly (that is, whether its <i>preconditions</i> were met). 024 * 025 * <p>If the precondition is not met, the {@code Preconditions} method throws an unchecked exception 026 * of a specified type, which helps the method in which the exception was thrown communicate that 027 * its caller has made a mistake. This allows constructs such as 028 * 029 * <pre>{@code 030 * public static double sqrt(double value) { 031 * if (value < 0) { 032 * throw new IllegalArgumentException("input is negative: " + value); 033 * } 034 * // calculate square root 035 * } 036 * }</pre> 037 * 038 * <p>to be replaced with the more compact 039 * 040 * <pre>{@code 041 * public static double sqrt(double value) { 042 * checkArgument(value >= 0, "input is negative: %s", value); 043 * // calculate square root 044 * } 045 * }</pre> 046 * 047 * <p>so that a hypothetical bad caller of this method, such as: 048 * 049 * <pre>{@code 050 * void exampleBadCaller() { 051 * double d = sqrt(-1.0); 052 * } 053 * }</pre> 054 * 055 * <p>would be flagged as having called {@code sqrt()} with an illegal argument. 056 * 057 * <h3>Performance</h3> 058 * 059 * <p>Avoid passing message arguments that are expensive to compute; your code will always compute 060 * them, even though they usually won't be needed. If you have such arguments, use the conventional 061 * if/throw idiom instead. 062 * 063 * <p>Depending on your message arguments, memory may be allocated for boxing and varargs array 064 * creation. However, the methods of this class have a large number of overloads that prevent such 065 * allocations in many common cases. 066 * 067 * <p>The message string is not formatted unless the exception will be thrown, so the cost of the 068 * string formatting itself should not be a concern. 069 * 070 * <p>As with any performance concerns, you should consider profiling your code (in a production 071 * environment if possible) before spending a lot of effort on tweaking a particular element. 072 * 073 * <h3>Other types of preconditions</h3> 074 * 075 * <p>Not every type of precondition failure is supported by these methods. Continue to throw 076 * standard JDK exceptions such as {@link java.util.NoSuchElementException} or {@link 077 * UnsupportedOperationException} in the situations they are intended for. 078 * 079 * <h3>Non-preconditions</h3> 080 * 081 * <p>It is of course possible to use the methods of this class to check for invalid conditions 082 * which are <i>not the caller's fault</i>. Doing so is <b>not recommended</b> because it is 083 * misleading to future readers of the code and of stack traces. See <a 084 * href="https://github.com/google/guava/wiki/ConditionalFailuresExplained">Conditional failures 085 * explained</a> in the Guava User Guide for more advice. 086 * 087 * <h3>{@code java.util.Objects.requireNonNull()}</h3> 088 * 089 * <p>Projects which use {@code com.google.common} should generally avoid the use of {@link 090 * java.util.Objects#requireNonNull(Object)}. Instead, use whichever of {@link 091 * #checkNotNull(Object)} or {@link Verify#verifyNotNull(Object)} is appropriate to the situation. 092 * (The same goes for the message-accepting overloads.) 093 * 094 * <h3>Only {@code %s} is supported</h3> 095 * 096 * <p>In {@code Preconditions} error message template strings, only the {@code "%s"} specifier is 097 * supported, not the full range of {@link java.util.Formatter} specifiers. 098 * 099 * <h3>More information</h3> 100 * 101 * <p>See the Guava User Guide on <a 102 * href="https://github.com/google/guava/wiki/PreconditionsExplained">using {@code 103 * Preconditions}</a>. 104 * 105 * @author Kevin Bourrillion 106 * @since 2.0 107 */ 108@GwtCompatible 109public final class Preconditions { 110 private Preconditions() {} 111 112 /** 113 * Ensures the truth of an expression involving one or more parameters to the calling method. 114 * 115 * @param expression a boolean expression 116 * @throws IllegalArgumentException if {@code expression} is false 117 */ 118 public static void checkArgument(boolean expression) { 119 if (!expression) { 120 throw new IllegalArgumentException(); 121 } 122 } 123 124 /** 125 * Ensures the truth of an expression involving one or more parameters to the calling method. 126 * 127 * @param expression a boolean expression 128 * @param errorMessage the exception message to use if the check fails; will be converted to a 129 * string using {@link String#valueOf(Object)} 130 * @throws IllegalArgumentException if {@code expression} is false 131 */ 132 public static void checkArgument(boolean expression, @Nullable Object errorMessage) { 133 if (!expression) { 134 throw new IllegalArgumentException(String.valueOf(errorMessage)); 135 } 136 } 137 138 /** 139 * Ensures the truth of an expression involving one or more parameters to the calling method. 140 * 141 * @param expression a boolean expression 142 * @param errorMessageTemplate a template for the exception message should the check fail. The 143 * message is formed by replacing each {@code %s} placeholder in the template with an 144 * argument. These are matched by position - the first {@code %s} gets {@code 145 * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in 146 * square braces. Unmatched placeholders will be left as-is. 147 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 148 * are converted to strings using {@link String#valueOf(Object)}. 149 * @throws IllegalArgumentException if {@code expression} is false 150 * @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or 151 * {@code errorMessageArgs} is null (don't let this happen) 152 */ 153 public static void checkArgument( 154 boolean expression, 155 @Nullable String errorMessageTemplate, 156 @Nullable Object... errorMessageArgs) { 157 if (!expression) { 158 throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs)); 159 } 160 } 161 162 /** 163 * Ensures the truth of an expression involving one or more parameters to the calling method. 164 * 165 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 166 */ 167 public static void checkArgument(boolean b, @Nullable String errorMessageTemplate, char p1) { 168 if (!b) { 169 throw new IllegalArgumentException(format(errorMessageTemplate, p1)); 170 } 171 } 172 173 /** 174 * Ensures the truth of an expression involving one or more parameters to the calling method. 175 * 176 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 177 */ 178 public static void checkArgument(boolean b, @Nullable String errorMessageTemplate, int p1) { 179 if (!b) { 180 throw new IllegalArgumentException(format(errorMessageTemplate, p1)); 181 } 182 } 183 184 /** 185 * Ensures the truth of an expression involving one or more parameters to the calling method. 186 * 187 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 188 */ 189 public static void checkArgument(boolean b, @Nullable String errorMessageTemplate, long p1) { 190 if (!b) { 191 throw new IllegalArgumentException(format(errorMessageTemplate, p1)); 192 } 193 } 194 195 /** 196 * Ensures the truth of an expression involving one or more parameters to the calling method. 197 * 198 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 199 */ 200 public static void checkArgument( 201 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1) { 202 if (!b) { 203 throw new IllegalArgumentException(format(errorMessageTemplate, p1)); 204 } 205 } 206 207 /** 208 * Ensures the truth of an expression involving one or more parameters to the calling method. 209 * 210 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 211 */ 212 public static void checkArgument( 213 boolean b, @Nullable String errorMessageTemplate, char p1, char p2) { 214 if (!b) { 215 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 216 } 217 } 218 219 /** 220 * Ensures the truth of an expression involving one or more parameters to the calling method. 221 * 222 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 223 */ 224 public static void checkArgument( 225 boolean b, @Nullable String errorMessageTemplate, char p1, int p2) { 226 if (!b) { 227 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 228 } 229 } 230 231 /** 232 * Ensures the truth of an expression involving one or more parameters to the calling method. 233 * 234 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 235 */ 236 public static void checkArgument( 237 boolean b, @Nullable String errorMessageTemplate, char p1, long p2) { 238 if (!b) { 239 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 240 } 241 } 242 243 /** 244 * Ensures the truth of an expression involving one or more parameters to the calling method. 245 * 246 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 247 */ 248 public static void checkArgument( 249 boolean b, @Nullable String errorMessageTemplate, char p1, @Nullable Object p2) { 250 if (!b) { 251 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 252 } 253 } 254 255 /** 256 * Ensures the truth of an expression involving one or more parameters to the calling method. 257 * 258 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 259 */ 260 public static void checkArgument( 261 boolean b, @Nullable String errorMessageTemplate, int p1, char p2) { 262 if (!b) { 263 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 264 } 265 } 266 267 /** 268 * Ensures the truth of an expression involving one or more parameters to the calling method. 269 * 270 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 271 */ 272 public static void checkArgument( 273 boolean b, @Nullable String errorMessageTemplate, int p1, int p2) { 274 if (!b) { 275 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 276 } 277 } 278 279 /** 280 * Ensures the truth of an expression involving one or more parameters to the calling method. 281 * 282 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 283 */ 284 public static void checkArgument( 285 boolean b, @Nullable String errorMessageTemplate, int p1, long p2) { 286 if (!b) { 287 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 288 } 289 } 290 291 /** 292 * Ensures the truth of an expression involving one or more parameters to the calling method. 293 * 294 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 295 */ 296 public static void checkArgument( 297 boolean b, @Nullable String errorMessageTemplate, int p1, @Nullable Object p2) { 298 if (!b) { 299 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 300 } 301 } 302 303 /** 304 * Ensures the truth of an expression involving one or more parameters to the calling method. 305 * 306 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 307 */ 308 public static void checkArgument( 309 boolean b, @Nullable String errorMessageTemplate, long p1, char p2) { 310 if (!b) { 311 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 312 } 313 } 314 315 /** 316 * Ensures the truth of an expression involving one or more parameters to the calling method. 317 * 318 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 319 */ 320 public static void checkArgument( 321 boolean b, @Nullable String errorMessageTemplate, long p1, int p2) { 322 if (!b) { 323 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 324 } 325 } 326 327 /** 328 * Ensures the truth of an expression involving one or more parameters to the calling method. 329 * 330 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 331 */ 332 public static void checkArgument( 333 boolean b, @Nullable String errorMessageTemplate, long p1, long p2) { 334 if (!b) { 335 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 336 } 337 } 338 339 /** 340 * Ensures the truth of an expression involving one or more parameters to the calling method. 341 * 342 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 343 */ 344 public static void checkArgument( 345 boolean b, @Nullable String errorMessageTemplate, long p1, @Nullable Object p2) { 346 if (!b) { 347 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 348 } 349 } 350 351 /** 352 * Ensures the truth of an expression involving one or more parameters to the calling method. 353 * 354 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 355 */ 356 public static void checkArgument( 357 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, char p2) { 358 if (!b) { 359 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 360 } 361 } 362 363 /** 364 * Ensures the truth of an expression involving one or more parameters to the calling method. 365 * 366 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 367 */ 368 public static void checkArgument( 369 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, int p2) { 370 if (!b) { 371 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 372 } 373 } 374 375 /** 376 * Ensures the truth of an expression involving one or more parameters to the calling method. 377 * 378 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 379 */ 380 public static void checkArgument( 381 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, long p2) { 382 if (!b) { 383 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 384 } 385 } 386 387 /** 388 * Ensures the truth of an expression involving one or more parameters to the calling method. 389 * 390 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 391 */ 392 public static void checkArgument( 393 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, @Nullable Object p2) { 394 if (!b) { 395 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); 396 } 397 } 398 399 /** 400 * Ensures the truth of an expression involving one or more parameters to the calling method. 401 * 402 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 403 */ 404 public static void checkArgument( 405 boolean b, 406 @Nullable String errorMessageTemplate, 407 @Nullable Object p1, 408 @Nullable Object p2, 409 @Nullable Object p3) { 410 if (!b) { 411 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2, p3)); 412 } 413 } 414 415 /** 416 * Ensures the truth of an expression involving one or more parameters to the calling method. 417 * 418 * <p>See {@link #checkArgument(boolean, String, Object...)} for details. 419 */ 420 public static void checkArgument( 421 boolean b, 422 @Nullable String errorMessageTemplate, 423 @Nullable Object p1, 424 @Nullable Object p2, 425 @Nullable Object p3, 426 @Nullable Object p4) { 427 if (!b) { 428 throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2, p3, p4)); 429 } 430 } 431 432 /** 433 * Ensures the truth of an expression involving the state of the calling instance, but not 434 * involving any parameters to the calling method. 435 * 436 * @param expression a boolean expression 437 * @throws IllegalStateException if {@code expression} is false 438 */ 439 public static void checkState(boolean expression) { 440 if (!expression) { 441 throw new IllegalStateException(); 442 } 443 } 444 445 /** 446 * Ensures the truth of an expression involving the state of the calling instance, but not 447 * involving any parameters to the calling method. 448 * 449 * @param expression a boolean expression 450 * @param errorMessage the exception message to use if the check fails; will be converted to a 451 * string using {@link String#valueOf(Object)} 452 * @throws IllegalStateException if {@code expression} is false 453 */ 454 public static void checkState(boolean expression, @Nullable Object errorMessage) { 455 if (!expression) { 456 throw new IllegalStateException(String.valueOf(errorMessage)); 457 } 458 } 459 460 /** 461 * Ensures the truth of an expression involving the state of the calling instance, but not 462 * involving any parameters to the calling method. 463 * 464 * @param expression a boolean expression 465 * @param errorMessageTemplate a template for the exception message should the check fail. The 466 * message is formed by replacing each {@code %s} placeholder in the template with an 467 * argument. These are matched by position - the first {@code %s} gets {@code 468 * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in 469 * square braces. Unmatched placeholders will be left as-is. 470 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 471 * are converted to strings using {@link String#valueOf(Object)}. 472 * @throws IllegalStateException if {@code expression} is false 473 * @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or 474 * {@code errorMessageArgs} is null (don't let this happen) 475 */ 476 public static void checkState( 477 boolean expression, 478 @Nullable String errorMessageTemplate, 479 @Nullable Object... errorMessageArgs) { 480 if (!expression) { 481 throw new IllegalStateException(format(errorMessageTemplate, errorMessageArgs)); 482 } 483 } 484 485 /** 486 * Ensures the truth of an expression involving the state of the calling instance, but not 487 * involving any parameters to the calling method. 488 * 489 * <p>See {@link #checkState(boolean, String, Object...)} for details. 490 */ 491 public static void checkState(boolean b, @Nullable String errorMessageTemplate, char p1) { 492 if (!b) { 493 throw new IllegalStateException(format(errorMessageTemplate, p1)); 494 } 495 } 496 497 /** 498 * Ensures the truth of an expression involving the state of the calling instance, but not 499 * involving any parameters to the calling method. 500 * 501 * <p>See {@link #checkState(boolean, String, Object...)} for details. 502 */ 503 public static void checkState(boolean b, @Nullable String errorMessageTemplate, int p1) { 504 if (!b) { 505 throw new IllegalStateException(format(errorMessageTemplate, p1)); 506 } 507 } 508 509 /** 510 * Ensures the truth of an expression involving the state of the calling instance, but not 511 * involving any parameters to the calling method. 512 * 513 * <p>See {@link #checkState(boolean, String, Object...)} for details. 514 */ 515 public static void checkState(boolean b, @Nullable String errorMessageTemplate, long p1) { 516 if (!b) { 517 throw new IllegalStateException(format(errorMessageTemplate, p1)); 518 } 519 } 520 521 /** 522 * Ensures the truth of an expression involving the state of the calling instance, but not 523 * involving any parameters to the calling method. 524 * 525 * <p>See {@link #checkState(boolean, String, Object...)} for details. 526 */ 527 public static void checkState( 528 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1) { 529 if (!b) { 530 throw new IllegalStateException(format(errorMessageTemplate, p1)); 531 } 532 } 533 534 /** 535 * Ensures the truth of an expression involving the state of the calling instance, but not 536 * involving any parameters to the calling method. 537 * 538 * <p>See {@link #checkState(boolean, String, Object...)} for details. 539 */ 540 public static void checkState( 541 boolean b, @Nullable String errorMessageTemplate, char p1, char p2) { 542 if (!b) { 543 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 544 } 545 } 546 547 /** 548 * Ensures the truth of an expression involving the state of the calling instance, but not 549 * involving any parameters to the calling method. 550 * 551 * <p>See {@link #checkState(boolean, String, Object...)} for details. 552 */ 553 public static void checkState(boolean b, @Nullable String errorMessageTemplate, char p1, int p2) { 554 if (!b) { 555 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 556 } 557 } 558 559 /** 560 * Ensures the truth of an expression involving the state of the calling instance, but not 561 * involving any parameters to the calling method. 562 * 563 * <p>See {@link #checkState(boolean, String, Object...)} for details. 564 */ 565 public static void checkState( 566 boolean b, @Nullable String errorMessageTemplate, char p1, long p2) { 567 if (!b) { 568 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 569 } 570 } 571 572 /** 573 * Ensures the truth of an expression involving the state of the calling instance, but not 574 * involving any parameters to the calling method. 575 * 576 * <p>See {@link #checkState(boolean, String, Object...)} for details. 577 */ 578 public static void checkState( 579 boolean b, @Nullable String errorMessageTemplate, char p1, @Nullable Object p2) { 580 if (!b) { 581 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 582 } 583 } 584 585 /** 586 * Ensures the truth of an expression involving the state of the calling instance, but not 587 * involving any parameters to the calling method. 588 * 589 * <p>See {@link #checkState(boolean, String, Object...)} for details. 590 */ 591 public static void checkState(boolean b, @Nullable String errorMessageTemplate, int p1, char p2) { 592 if (!b) { 593 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 594 } 595 } 596 597 /** 598 * Ensures the truth of an expression involving the state of the calling instance, but not 599 * involving any parameters to the calling method. 600 * 601 * <p>See {@link #checkState(boolean, String, Object...)} for details. 602 */ 603 public static void checkState(boolean b, @Nullable String errorMessageTemplate, int p1, int p2) { 604 if (!b) { 605 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 606 } 607 } 608 609 /** 610 * Ensures the truth of an expression involving the state of the calling instance, but not 611 * involving any parameters to the calling method. 612 * 613 * <p>See {@link #checkState(boolean, String, Object...)} for details. 614 */ 615 public static void checkState(boolean b, @Nullable String errorMessageTemplate, int p1, long p2) { 616 if (!b) { 617 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 618 } 619 } 620 621 /** 622 * Ensures the truth of an expression involving the state of the calling instance, but not 623 * involving any parameters to the calling method. 624 * 625 * <p>See {@link #checkState(boolean, String, Object...)} for details. 626 */ 627 public static void checkState( 628 boolean b, @Nullable String errorMessageTemplate, int p1, @Nullable Object p2) { 629 if (!b) { 630 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 631 } 632 } 633 634 /** 635 * Ensures the truth of an expression involving the state of the calling instance, but not 636 * involving any parameters to the calling method. 637 * 638 * <p>See {@link #checkState(boolean, String, Object...)} for details. 639 */ 640 public static void checkState( 641 boolean b, @Nullable String errorMessageTemplate, long p1, char p2) { 642 if (!b) { 643 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 644 } 645 } 646 647 /** 648 * Ensures the truth of an expression involving the state of the calling instance, but not 649 * involving any parameters to the calling method. 650 * 651 * <p>See {@link #checkState(boolean, String, Object...)} for details. 652 */ 653 public static void checkState(boolean b, @Nullable String errorMessageTemplate, long p1, int p2) { 654 if (!b) { 655 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 656 } 657 } 658 659 /** 660 * Ensures the truth of an expression involving the state of the calling instance, but not 661 * involving any parameters to the calling method. 662 * 663 * <p>See {@link #checkState(boolean, String, Object...)} for details. 664 */ 665 public static void checkState( 666 boolean b, @Nullable String errorMessageTemplate, long p1, long p2) { 667 if (!b) { 668 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 669 } 670 } 671 672 /** 673 * Ensures the truth of an expression involving the state of the calling instance, but not 674 * involving any parameters to the calling method. 675 * 676 * <p>See {@link #checkState(boolean, String, Object...)} for details. 677 */ 678 public static void checkState( 679 boolean b, @Nullable String errorMessageTemplate, long p1, @Nullable Object p2) { 680 if (!b) { 681 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 682 } 683 } 684 685 /** 686 * Ensures the truth of an expression involving the state of the calling instance, but not 687 * involving any parameters to the calling method. 688 * 689 * <p>See {@link #checkState(boolean, String, Object...)} for details. 690 */ 691 public static void checkState( 692 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, char p2) { 693 if (!b) { 694 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 695 } 696 } 697 698 /** 699 * Ensures the truth of an expression involving the state of the calling instance, but not 700 * involving any parameters to the calling method. 701 * 702 * <p>See {@link #checkState(boolean, String, Object...)} for details. 703 */ 704 public static void checkState( 705 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, int p2) { 706 if (!b) { 707 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 708 } 709 } 710 711 /** 712 * Ensures the truth of an expression involving the state of the calling instance, but not 713 * involving any parameters to the calling method. 714 * 715 * <p>See {@link #checkState(boolean, String, Object...)} for details. 716 */ 717 public static void checkState( 718 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, long p2) { 719 if (!b) { 720 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 721 } 722 } 723 724 /** 725 * Ensures the truth of an expression involving the state of the calling instance, but not 726 * involving any parameters to the calling method. 727 * 728 * <p>See {@link #checkState(boolean, String, Object...)} for details. 729 */ 730 public static void checkState( 731 boolean b, @Nullable String errorMessageTemplate, @Nullable Object p1, @Nullable Object p2) { 732 if (!b) { 733 throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); 734 } 735 } 736 737 /** 738 * Ensures the truth of an expression involving the state of the calling instance, but not 739 * involving any parameters to the calling method. 740 * 741 * <p>See {@link #checkState(boolean, String, Object...)} for details. 742 */ 743 public static void checkState( 744 boolean b, 745 @Nullable String errorMessageTemplate, 746 @Nullable Object p1, 747 @Nullable Object p2, 748 @Nullable Object p3) { 749 if (!b) { 750 throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); 751 } 752 } 753 754 /** 755 * Ensures the truth of an expression involving the state of the calling instance, but not 756 * involving any parameters to the calling method. 757 * 758 * <p>See {@link #checkState(boolean, String, Object...)} for details. 759 */ 760 public static void checkState( 761 boolean b, 762 @Nullable String errorMessageTemplate, 763 @Nullable Object p1, 764 @Nullable Object p2, 765 @Nullable Object p3, 766 @Nullable Object p4) { 767 if (!b) { 768 throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3, p4)); 769 } 770 } 771 772 /** 773 * Ensures that an object reference passed as a parameter to the calling method is not null. 774 * 775 * @param reference an object reference 776 * @return the non-null reference that was validated 777 * @throws NullPointerException if {@code reference} is null 778 */ 779 @CanIgnoreReturnValue 780 public static <T> T checkNotNull(T reference) { 781 if (reference == null) { 782 throw new NullPointerException(); 783 } 784 return reference; 785 } 786 787 /** 788 * Ensures that an object reference passed as a parameter to the calling method is not null. 789 * 790 * @param reference an object reference 791 * @param errorMessage the exception message to use if the check fails; will be converted to a 792 * string using {@link String#valueOf(Object)} 793 * @return the non-null reference that was validated 794 * @throws NullPointerException if {@code reference} is null 795 */ 796 @CanIgnoreReturnValue 797 public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) { 798 if (reference == null) { 799 throw new NullPointerException(String.valueOf(errorMessage)); 800 } 801 return reference; 802 } 803 804 /** 805 * Ensures that an object reference passed as a parameter to the calling method is not null. 806 * 807 * @param reference an object reference 808 * @param errorMessageTemplate a template for the exception message should the check fail. The 809 * message is formed by replacing each {@code %s} placeholder in the template with an 810 * argument. These are matched by position - the first {@code %s} gets {@code 811 * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in 812 * square braces. Unmatched placeholders will be left as-is. 813 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 814 * are converted to strings using {@link String#valueOf(Object)}. 815 * @return the non-null reference that was validated 816 * @throws NullPointerException if {@code reference} is null 817 */ 818 @CanIgnoreReturnValue 819 public static <T> T checkNotNull( 820 T reference, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) { 821 if (reference == null) { 822 // If either of these parameters is null, the right thing happens anyway 823 throw new NullPointerException(format(errorMessageTemplate, errorMessageArgs)); 824 } 825 return reference; 826 } 827 828 /** 829 * Ensures that an object reference passed as a parameter to the calling method is not null. 830 * 831 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 832 */ 833 @CanIgnoreReturnValue 834 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, char p1) { 835 if (obj == null) { 836 throw new NullPointerException(format(errorMessageTemplate, p1)); 837 } 838 return obj; 839 } 840 841 /** 842 * Ensures that an object reference passed as a parameter to the calling method is not null. 843 * 844 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 845 */ 846 @CanIgnoreReturnValue 847 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, int p1) { 848 if (obj == null) { 849 throw new NullPointerException(format(errorMessageTemplate, p1)); 850 } 851 return obj; 852 } 853 854 /** 855 * Ensures that an object reference passed as a parameter to the calling method is not null. 856 * 857 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 858 */ 859 @CanIgnoreReturnValue 860 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, long p1) { 861 if (obj == null) { 862 throw new NullPointerException(format(errorMessageTemplate, p1)); 863 } 864 return obj; 865 } 866 867 /** 868 * Ensures that an object reference passed as a parameter to the calling method is not null. 869 * 870 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 871 */ 872 @CanIgnoreReturnValue 873 public static <T> T checkNotNull( 874 T obj, @Nullable String errorMessageTemplate, @Nullable Object p1) { 875 if (obj == null) { 876 throw new NullPointerException(format(errorMessageTemplate, p1)); 877 } 878 return obj; 879 } 880 881 /** 882 * Ensures that an object reference passed as a parameter to the calling method is not null. 883 * 884 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 885 */ 886 @CanIgnoreReturnValue 887 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, char p1, char p2) { 888 if (obj == null) { 889 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 890 } 891 return obj; 892 } 893 894 /** 895 * Ensures that an object reference passed as a parameter to the calling method is not null. 896 * 897 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 898 */ 899 @CanIgnoreReturnValue 900 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, char p1, int p2) { 901 if (obj == null) { 902 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 903 } 904 return obj; 905 } 906 907 /** 908 * Ensures that an object reference passed as a parameter to the calling method is not null. 909 * 910 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 911 */ 912 @CanIgnoreReturnValue 913 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, char p1, long p2) { 914 if (obj == null) { 915 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 916 } 917 return obj; 918 } 919 920 /** 921 * Ensures that an object reference passed as a parameter to the calling method is not null. 922 * 923 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 924 */ 925 @CanIgnoreReturnValue 926 public static <T> T checkNotNull( 927 T obj, @Nullable String errorMessageTemplate, char p1, @Nullable Object p2) { 928 if (obj == null) { 929 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 930 } 931 return obj; 932 } 933 934 /** 935 * Ensures that an object reference passed as a parameter to the calling method is not null. 936 * 937 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 938 */ 939 @CanIgnoreReturnValue 940 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, int p1, char p2) { 941 if (obj == null) { 942 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 943 } 944 return obj; 945 } 946 947 /** 948 * Ensures that an object reference passed as a parameter to the calling method is not null. 949 * 950 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 951 */ 952 @CanIgnoreReturnValue 953 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, int p1, int p2) { 954 if (obj == null) { 955 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 956 } 957 return obj; 958 } 959 960 /** 961 * Ensures that an object reference passed as a parameter to the calling method is not null. 962 * 963 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 964 */ 965 @CanIgnoreReturnValue 966 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, int p1, long p2) { 967 if (obj == null) { 968 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 969 } 970 return obj; 971 } 972 973 /** 974 * Ensures that an object reference passed as a parameter to the calling method is not null. 975 * 976 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 977 */ 978 @CanIgnoreReturnValue 979 public static <T> T checkNotNull( 980 T obj, @Nullable String errorMessageTemplate, int p1, @Nullable Object p2) { 981 if (obj == null) { 982 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 983 } 984 return obj; 985 } 986 987 /** 988 * Ensures that an object reference passed as a parameter to the calling method is not null. 989 * 990 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 991 */ 992 @CanIgnoreReturnValue 993 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, long p1, char p2) { 994 if (obj == null) { 995 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 996 } 997 return obj; 998 } 999 1000 /** 1001 * Ensures that an object reference passed as a parameter to the calling method is not null. 1002 * 1003 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1004 */ 1005 @CanIgnoreReturnValue 1006 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, long p1, int p2) { 1007 if (obj == null) { 1008 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1009 } 1010 return obj; 1011 } 1012 1013 /** 1014 * Ensures that an object reference passed as a parameter to the calling method is not null. 1015 * 1016 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1017 */ 1018 @CanIgnoreReturnValue 1019 public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, long p1, long p2) { 1020 if (obj == null) { 1021 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1022 } 1023 return obj; 1024 } 1025 1026 /** 1027 * Ensures that an object reference passed as a parameter to the calling method is not null. 1028 * 1029 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1030 */ 1031 @CanIgnoreReturnValue 1032 public static <T> T checkNotNull( 1033 T obj, @Nullable String errorMessageTemplate, long p1, @Nullable Object p2) { 1034 if (obj == null) { 1035 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1036 } 1037 return obj; 1038 } 1039 1040 /** 1041 * Ensures that an object reference passed as a parameter to the calling method is not null. 1042 * 1043 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1044 */ 1045 @CanIgnoreReturnValue 1046 public static <T> T checkNotNull( 1047 T obj, @Nullable String errorMessageTemplate, @Nullable Object p1, char p2) { 1048 if (obj == null) { 1049 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1050 } 1051 return obj; 1052 } 1053 1054 /** 1055 * Ensures that an object reference passed as a parameter to the calling method is not null. 1056 * 1057 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1058 */ 1059 @CanIgnoreReturnValue 1060 public static <T> T checkNotNull( 1061 T obj, @Nullable String errorMessageTemplate, @Nullable Object p1, int p2) { 1062 if (obj == null) { 1063 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1064 } 1065 return obj; 1066 } 1067 1068 /** 1069 * Ensures that an object reference passed as a parameter to the calling method is not null. 1070 * 1071 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1072 */ 1073 @CanIgnoreReturnValue 1074 public static <T> T checkNotNull( 1075 T obj, @Nullable String errorMessageTemplate, @Nullable Object p1, long p2) { 1076 if (obj == null) { 1077 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1078 } 1079 return obj; 1080 } 1081 1082 /** 1083 * Ensures that an object reference passed as a parameter to the calling method is not null. 1084 * 1085 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1086 */ 1087 @CanIgnoreReturnValue 1088 public static <T> T checkNotNull( 1089 T obj, @Nullable String errorMessageTemplate, @Nullable Object p1, @Nullable Object p2) { 1090 if (obj == null) { 1091 throw new NullPointerException(format(errorMessageTemplate, p1, p2)); 1092 } 1093 return obj; 1094 } 1095 1096 /** 1097 * Ensures that an object reference passed as a parameter to the calling method is not null. 1098 * 1099 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1100 */ 1101 @CanIgnoreReturnValue 1102 public static <T> T checkNotNull( 1103 T obj, 1104 @Nullable String errorMessageTemplate, 1105 @Nullable Object p1, 1106 @Nullable Object p2, 1107 @Nullable Object p3) { 1108 if (obj == null) { 1109 throw new NullPointerException(format(errorMessageTemplate, p1, p2, p3)); 1110 } 1111 return obj; 1112 } 1113 1114 /** 1115 * Ensures that an object reference passed as a parameter to the calling method is not null. 1116 * 1117 * <p>See {@link #checkNotNull(Object, String, Object...)} for details. 1118 */ 1119 @CanIgnoreReturnValue 1120 public static <T> T checkNotNull( 1121 T obj, 1122 @Nullable String errorMessageTemplate, 1123 @Nullable Object p1, 1124 @Nullable Object p2, 1125 @Nullable Object p3, 1126 @Nullable Object p4) { 1127 if (obj == null) { 1128 throw new NullPointerException(format(errorMessageTemplate, p1, p2, p3, p4)); 1129 } 1130 return obj; 1131 } 1132 1133 /* 1134 * All recent hotspots (as of 2009) *really* like to have the natural code 1135 * 1136 * if (guardExpression) { 1137 * throw new BadException(messageExpression); 1138 * } 1139 * 1140 * refactored so that messageExpression is moved to a separate String-returning method. 1141 * 1142 * if (guardExpression) { 1143 * throw new BadException(badMsg(...)); 1144 * } 1145 * 1146 * The alternative natural refactorings into void or Exception-returning methods are much slower. 1147 * This is a big deal - we're talking factors of 2-8 in microbenchmarks, not just 10-20%. (This is 1148 * a hotspot optimizer bug, which should be fixed, but that's a separate, big project). 1149 * 1150 * The coding pattern above is heavily used in java.util, e.g. in ArrayList. There is a 1151 * RangeCheckMicroBenchmark in the JDK that was used to test this. 1152 * 1153 * But the methods in this class want to throw different exceptions, depending on the args, so it 1154 * appears that this pattern is not directly applicable. But we can use the ridiculous, devious 1155 * trick of throwing an exception in the middle of the construction of another exception. Hotspot 1156 * is fine with that. 1157 */ 1158 1159 /** 1160 * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size 1161 * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive. 1162 * 1163 * @param index a user-supplied index identifying an element of an array, list or string 1164 * @param size the size of that array, list or string 1165 * @return the value of {@code index} 1166 * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size} 1167 * @throws IllegalArgumentException if {@code size} is negative 1168 */ 1169 @CanIgnoreReturnValue 1170 public static int checkElementIndex(int index, int size) { 1171 return checkElementIndex(index, size, "index"); 1172 } 1173 1174 /** 1175 * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size 1176 * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive. 1177 * 1178 * @param index a user-supplied index identifying an element of an array, list or string 1179 * @param size the size of that array, list or string 1180 * @param desc the text to use to describe this index in an error message 1181 * @return the value of {@code index} 1182 * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size} 1183 * @throws IllegalArgumentException if {@code size} is negative 1184 */ 1185 @CanIgnoreReturnValue 1186 public static int checkElementIndex(int index, int size, @Nullable String desc) { 1187 // Carefully optimized for execution by hotspot (explanatory comment above) 1188 if (index < 0 || index >= size) { 1189 throw new IndexOutOfBoundsException(badElementIndex(index, size, desc)); 1190 } 1191 return index; 1192 } 1193 1194 private static String badElementIndex(int index, int size, String desc) { 1195 if (index < 0) { 1196 return format("%s (%s) must not be negative", desc, index); 1197 } else if (size < 0) { 1198 throw new IllegalArgumentException("negative size: " + size); 1199 } else { // index >= size 1200 return format("%s (%s) must be less than size (%s)", desc, index, size); 1201 } 1202 } 1203 1204 /** 1205 * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of 1206 * size {@code size}. A position index may range from zero to {@code size}, inclusive. 1207 * 1208 * @param index a user-supplied index identifying a position in an array, list or string 1209 * @param size the size of that array, list or string 1210 * @return the value of {@code index} 1211 * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size} 1212 * @throws IllegalArgumentException if {@code size} is negative 1213 */ 1214 @CanIgnoreReturnValue 1215 public static int checkPositionIndex(int index, int size) { 1216 return checkPositionIndex(index, size, "index"); 1217 } 1218 1219 /** 1220 * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of 1221 * size {@code size}. A position index may range from zero to {@code size}, inclusive. 1222 * 1223 * @param index a user-supplied index identifying a position in an array, list or string 1224 * @param size the size of that array, list or string 1225 * @param desc the text to use to describe this index in an error message 1226 * @return the value of {@code index} 1227 * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size} 1228 * @throws IllegalArgumentException if {@code size} is negative 1229 */ 1230 @CanIgnoreReturnValue 1231 public static int checkPositionIndex(int index, int size, @Nullable String desc) { 1232 // Carefully optimized for execution by hotspot (explanatory comment above) 1233 if (index < 0 || index > size) { 1234 throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc)); 1235 } 1236 return index; 1237 } 1238 1239 private static String badPositionIndex(int index, int size, String desc) { 1240 if (index < 0) { 1241 return format("%s (%s) must not be negative", desc, index); 1242 } else if (size < 0) { 1243 throw new IllegalArgumentException("negative size: " + size); 1244 } else { // index > size 1245 return format("%s (%s) must not be greater than size (%s)", desc, index, size); 1246 } 1247 } 1248 1249 /** 1250 * Ensures that {@code start} and {@code end} specify a valid <i>positions</i> in an array, list 1251 * or string of size {@code size}, and are in order. A position index may range from zero to 1252 * {@code size}, inclusive. 1253 * 1254 * @param start a user-supplied index identifying a starting position in an array, list or string 1255 * @param end a user-supplied index identifying a ending position in an array, list or string 1256 * @param size the size of that array, list or string 1257 * @throws IndexOutOfBoundsException if either index is negative or is greater than {@code size}, 1258 * or if {@code end} is less than {@code start} 1259 * @throws IllegalArgumentException if {@code size} is negative 1260 */ 1261 public static void checkPositionIndexes(int start, int end, int size) { 1262 // Carefully optimized for execution by hotspot (explanatory comment above) 1263 if (start < 0 || end < start || end > size) { 1264 throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size)); 1265 } 1266 } 1267 1268 private static String badPositionIndexes(int start, int end, int size) { 1269 if (start < 0 || start > size) { 1270 return badPositionIndex(start, size, "start index"); 1271 } 1272 if (end < 0 || end > size) { 1273 return badPositionIndex(end, size, "end index"); 1274 } 1275 // end < start 1276 return format("end index (%s) must not be less than start index (%s)", end, start); 1277 } 1278 1279 /** 1280 * Substitutes each {@code %s} in {@code template} with an argument. These are matched by 1281 * position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than 1282 * placeholders, the unmatched arguments will be appended to the end of the formatted message in 1283 * square braces. 1284 * 1285 * @param template a non-null string containing 0 or more {@code %s} placeholders. 1286 * @param args the arguments to be substituted into the message template. Arguments are converted 1287 * to strings using {@link String#valueOf(Object)}. Arguments can be null. 1288 */ 1289 // Note that this is somewhat-improperly used from Verify.java as well. 1290 static String format(String template, @Nullable Object... args) { 1291 template = String.valueOf(template); // null -> "null" 1292 1293 // start substituting the arguments into the '%s' placeholders 1294 StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); 1295 int templateStart = 0; 1296 int i = 0; 1297 while (i < args.length) { 1298 int placeholderStart = template.indexOf("%s", templateStart); 1299 if (placeholderStart == -1) { 1300 break; 1301 } 1302 builder.append(template, templateStart, placeholderStart); 1303 builder.append(args[i++]); 1304 templateStart = placeholderStart + 2; 1305 } 1306 builder.append(template, templateStart, template.length()); 1307 1308 // if we run out of placeholders, append the extra args in square braces 1309 if (i < args.length) { 1310 builder.append(" ["); 1311 builder.append(args[i++]); 1312 while (i < args.length) { 1313 builder.append(", "); 1314 builder.append(args[i++]); 1315 } 1316 builder.append(']'); 1317 } 1318 1319 return builder.toString(); 1320 } 1321}