1 /* 2 * Copyright (C) 2011 The Android Open Source Project 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 17 package com.android.internal.util; 18 19 import android.annotation.IntRange; 20 import android.annotation.NonNull; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.os.Build; 23 import android.text.TextUtils; 24 25 import java.util.Arrays; 26 import java.util.Collection; 27 import java.util.Objects; 28 29 /** 30 * Simple static methods to be called at the start of your own methods to verify 31 * correct arguments and state. 32 */ 33 public class Preconditions { 34 35 /** 36 * Ensures that an expression checking an argument is true. 37 * 38 * @param expression the expression to check 39 * @throws IllegalArgumentException if {@code expression} is false 40 */ 41 @UnsupportedAppUsage checkArgument(boolean expression)42 public static void checkArgument(boolean expression) { 43 if (!expression) { 44 throw new IllegalArgumentException(); 45 } 46 } 47 48 /** 49 * Ensures that an expression checking an argument is true. 50 * 51 * @param expression the expression to check 52 * @param errorMessage the exception message to use if the check fails; will 53 * be converted to a string using {@link String#valueOf(Object)} 54 * @throws IllegalArgumentException if {@code expression} is false 55 */ 56 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) checkArgument(boolean expression, final Object errorMessage)57 public static void checkArgument(boolean expression, final Object errorMessage) { 58 if (!expression) { 59 throw new IllegalArgumentException(String.valueOf(errorMessage)); 60 } 61 } 62 63 /** 64 * Ensures that an expression checking an argument is true. 65 * 66 * @param expression the expression to check 67 * @param messageTemplate a printf-style message template to use if the check fails; will 68 * be converted to a string using {@link String#format(String, Object...)} 69 * @param messageArgs arguments for {@code messageTemplate} 70 * @throws IllegalArgumentException if {@code expression} is false 71 */ checkArgument( final boolean expression, final @NonNull String messageTemplate, final Object... messageArgs)72 public static void checkArgument( 73 final boolean expression, 74 final @NonNull String messageTemplate, 75 final Object... messageArgs) { 76 if (!expression) { 77 throw new IllegalArgumentException(String.format(messageTemplate, messageArgs)); 78 } 79 } 80 81 /** 82 * Ensures that an string reference passed as a parameter to the calling 83 * method is not empty. 84 * 85 * @param string an string reference 86 * @return the string reference that was validated 87 * @throws IllegalArgumentException if {@code string} is empty 88 */ checkStringNotEmpty(final T string)89 public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string) { 90 if (TextUtils.isEmpty(string)) { 91 throw new IllegalArgumentException(); 92 } 93 return string; 94 } 95 96 /** 97 * Ensures that an string reference passed as a parameter to the calling 98 * method is not empty. 99 * 100 * @param string an string reference 101 * @param errorMessage the exception message to use if the check fails; will 102 * be converted to a string using {@link String#valueOf(Object)} 103 * @return the string reference that was validated 104 * @throws IllegalArgumentException if {@code string} is empty 105 */ checkStringNotEmpty(final T string, final Object errorMessage)106 public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string, 107 final Object errorMessage) { 108 if (TextUtils.isEmpty(string)) { 109 throw new IllegalArgumentException(String.valueOf(errorMessage)); 110 } 111 return string; 112 } 113 114 /** 115 * Ensures that an string reference passed as a parameter to the calling method is not empty. 116 * 117 * @param string an string reference 118 * @param messageTemplate a printf-style message template to use if the check fails; will be 119 * converted to a string using {@link String#format(String, Object...)} 120 * @param messageArgs arguments for {@code messageTemplate} 121 * @return the string reference that was validated 122 * @throws IllegalArgumentException if {@code string} is empty 123 */ checkStringNotEmpty( final T string, final @NonNull String messageTemplate, final Object... messageArgs)124 public static @NonNull <T extends CharSequence> T checkStringNotEmpty( 125 final T string, 126 final @NonNull String messageTemplate, 127 final Object... messageArgs) { 128 if (TextUtils.isEmpty(string)) { 129 throw new IllegalArgumentException(String.format(messageTemplate, messageArgs)); 130 } 131 return string; 132 } 133 134 /** 135 * Ensures that an object reference passed as a parameter to the calling 136 * method is not null. 137 * 138 * @param reference an object reference 139 * @return the non-null reference that was validated 140 * @throws NullPointerException if {@code reference} is null 141 * @deprecated - use {@link java.util.Objects.requireNonNull} instead. 142 */ 143 @Deprecated 144 @UnsupportedAppUsage checkNotNull(final T reference)145 public static @NonNull <T> T checkNotNull(final T reference) { 146 if (reference == null) { 147 throw new NullPointerException(); 148 } 149 return reference; 150 } 151 152 /** 153 * Ensures that an object reference passed as a parameter to the calling 154 * method is not null. 155 * 156 * @param reference an object reference 157 * @param errorMessage the exception message to use if the check fails; will 158 * be converted to a string using {@link String#valueOf(Object)} 159 * @return the non-null reference that was validated 160 * @throws NullPointerException if {@code reference} is null 161 * @deprecated - use {@link java.util.Objects#requireNonNull} instead. 162 */ 163 @Deprecated 164 @UnsupportedAppUsage checkNotNull(final T reference, final Object errorMessage)165 public static @NonNull <T> T checkNotNull(final T reference, final Object errorMessage) { 166 if (reference == null) { 167 throw new NullPointerException(String.valueOf(errorMessage)); 168 } 169 return reference; 170 } 171 172 /** 173 * Ensures that an object reference passed as a parameter to the calling 174 * method is not null. 175 * 176 * @param messageTemplate a printf-style message template to use if the check fails; will 177 * be converted to a string using {@link String#format(String, Object...)} 178 * @param messageArgs arguments for {@code messageTemplate} 179 * @throws NullPointerException if {@code reference} is null 180 */ checkNotNull( final T reference, final @NonNull String messageTemplate, final Object... messageArgs)181 public static @NonNull <T> T checkNotNull( 182 final T reference, 183 final @NonNull String messageTemplate, 184 final Object... messageArgs) { 185 if (reference == null) { 186 throw new NullPointerException(String.format(messageTemplate, messageArgs)); 187 } 188 return reference; 189 } 190 191 /** 192 * Ensures the truth of an expression involving the state of the calling 193 * instance, but not involving any parameters to the calling method. 194 * 195 * @param expression a boolean expression 196 * @throws IllegalStateException if {@code expression} is false 197 */ 198 @UnsupportedAppUsage checkState(final boolean expression)199 public static void checkState(final boolean expression) { 200 checkState(expression, null); 201 } 202 203 /** 204 * Ensures the truth of an expression involving the state of the calling 205 * instance, but not involving any parameters to the calling method. 206 * 207 * @param expression a boolean expression 208 * @param errorMessage the exception message to use if the check fails; will 209 * be converted to a string using {@link String#valueOf(Object)} 210 * @throws IllegalStateException if {@code expression} is false 211 */ 212 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) checkState(final boolean expression, String errorMessage)213 public static void checkState(final boolean expression, String errorMessage) { 214 if (!expression) { 215 throw new IllegalStateException(errorMessage); 216 } 217 } 218 219 /** 220 * Ensures the truth of an expression involving the state of the calling 221 * instance, but not involving any parameters to the calling method. 222 * 223 * @param expression a boolean expression 224 * @param messageTemplate a printf-style message template to use if the check fails; will 225 * be converted to a string using {@link String#format(String, Object...)} 226 * @param messageArgs arguments for {@code messageTemplate} 227 * @throws IllegalStateException if {@code expression} is false 228 */ checkState( final boolean expression, final @NonNull String messageTemplate, final Object... messageArgs)229 public static void checkState( 230 final boolean expression, 231 final @NonNull String messageTemplate, 232 final Object... messageArgs) { 233 if (!expression) { 234 throw new IllegalStateException(String.format(messageTemplate, messageArgs)); 235 } 236 } 237 238 /** 239 * Ensures the truth of an expression involving whether the calling identity is authorized to 240 * call the calling method. 241 * 242 * @param expression a boolean expression 243 * @throws SecurityException if {@code expression} is false 244 */ checkCallAuthorization(final boolean expression)245 public static void checkCallAuthorization(final boolean expression) { 246 if (!expression) { 247 throw new SecurityException("Calling identity is not authorized"); 248 } 249 } 250 251 /** 252 * Ensures the truth of an expression involving whether the calling identity is authorized to 253 * call the calling method. 254 * 255 * @param expression a boolean expression 256 * @param message the message of the security exception to be thrown 257 * @throws SecurityException if {@code expression} is false 258 */ checkCallAuthorization(final boolean expression, final String message)259 public static void checkCallAuthorization(final boolean expression, final String message) { 260 if (!expression) { 261 throw new SecurityException(message); 262 } 263 } 264 265 /** 266 * Ensures the truth of an expression involving whether the calling identity is authorized to 267 * call the calling method. 268 * 269 * @param expression a boolean expression 270 * @param messageTemplate a printf-style message template to use if the check fails; will 271 * be converted to a string using {@link String#format(String, Object...)} 272 * @param messageArgs arguments for {@code messageTemplate} 273 * @throws SecurityException if {@code expression} is false 274 */ checkCallAuthorization( final boolean expression, final @NonNull String messageTemplate, final Object... messageArgs)275 public static void checkCallAuthorization( 276 final boolean expression, 277 final @NonNull String messageTemplate, 278 final Object... messageArgs) { 279 if (!expression) { 280 throw new SecurityException(String.format(messageTemplate, messageArgs)); 281 } 282 } 283 284 /** 285 * Ensures the truth of an expression involving whether the calling user is authorized to 286 * call the calling method. 287 * 288 * @param expression a boolean expression 289 * @throws SecurityException if {@code expression} is false 290 */ checkCallingUser(final boolean expression)291 public static void checkCallingUser(final boolean expression) { 292 if (!expression) { 293 throw new SecurityException("Calling user is not authorized"); 294 } 295 } 296 297 /** 298 * Check the requested flags, throwing if any requested flags are outside 299 * the allowed set. 300 * 301 * @return the validated requested flags. 302 */ checkFlagsArgument(final int requestedFlags, final int allowedFlags)303 public static int checkFlagsArgument(final int requestedFlags, final int allowedFlags) { 304 if ((requestedFlags & allowedFlags) != requestedFlags) { 305 throw new IllegalArgumentException("Requested flags 0x" 306 + Integer.toHexString(requestedFlags) + ", but only 0x" 307 + Integer.toHexString(allowedFlags) + " are allowed"); 308 } 309 310 return requestedFlags; 311 } 312 313 /** 314 * Ensures that that the argument numeric value is non-negative (greater than or equal to 0). 315 * 316 * @param value a numeric int value 317 * @param errorMessage the exception message to use if the check fails 318 * @return the validated numeric value 319 * @throws IllegalArgumentException if {@code value} was negative 320 */ checkArgumentNonnegative(final int value, final String errorMessage)321 public static @IntRange(from = 0) int checkArgumentNonnegative(final int value, 322 final String errorMessage) { 323 if (value < 0) { 324 throw new IllegalArgumentException(errorMessage); 325 } 326 327 return value; 328 } 329 330 /** 331 * Ensures that that the argument numeric value is non-negative (greater than or equal to 0). 332 * 333 * @param value a numeric int value 334 * 335 * @return the validated numeric value 336 * @throws IllegalArgumentException if {@code value} was negative 337 */ checkArgumentNonnegative(final int value)338 public static @IntRange(from = 0) int checkArgumentNonnegative(final int value) { 339 if (value < 0) { 340 throw new IllegalArgumentException(); 341 } 342 343 return value; 344 } 345 346 /** 347 * Ensures that that the argument numeric value is non-negative (greater than or equal to 0). 348 * 349 * @param value a numeric long value 350 * @return the validated numeric value 351 * @throws IllegalArgumentException if {@code value} was negative 352 */ checkArgumentNonnegative(final long value)353 public static long checkArgumentNonnegative(final long value) { 354 if (value < 0) { 355 throw new IllegalArgumentException(); 356 } 357 358 return value; 359 } 360 361 /** 362 * Ensures that that the argument numeric value is non-negative (greater than or equal to 0). 363 * 364 * @param value a numeric long value 365 * @param errorMessage the exception message to use if the check fails 366 * @return the validated numeric value 367 * @throws IllegalArgumentException if {@code value} was negative 368 */ checkArgumentNonnegative(final long value, final String errorMessage)369 public static long checkArgumentNonnegative(final long value, final String errorMessage) { 370 if (value < 0) { 371 throw new IllegalArgumentException(errorMessage); 372 } 373 374 return value; 375 } 376 377 /** 378 * Ensures that that the argument numeric value is positive (greater than 0). 379 * 380 * @param value a numeric int value 381 * @param errorMessage the exception message to use if the check fails 382 * @return the validated numeric value 383 * @throws IllegalArgumentException if {@code value} was not positive 384 */ checkArgumentPositive(final int value, final String errorMessage)385 public static int checkArgumentPositive(final int value, final String errorMessage) { 386 if (value <= 0) { 387 throw new IllegalArgumentException(errorMessage); 388 } 389 390 return value; 391 } 392 393 /** 394 * Ensures that the argument floating point value is non-negative (greater than or equal to 0). 395 * @param value a floating point value 396 * @param errorMessage the exteption message to use if the check fails 397 * @return the validated numeric value 398 * @throws IllegalArgumentException if {@code value} was negative 399 */ checkArgumentNonNegative(final float value, final String errorMessage)400 public static float checkArgumentNonNegative(final float value, final String errorMessage) { 401 if (value < 0) { 402 throw new IllegalArgumentException(errorMessage); 403 } 404 405 return value; 406 } 407 408 /** 409 * Ensures that the argument floating point value is positive (greater than 0). 410 * @param value a floating point value 411 * @param errorMessage the exteption message to use if the check fails 412 * @return the validated numeric value 413 * @throws IllegalArgumentException if {@code value} was not positive 414 */ checkArgumentPositive(final float value, final String errorMessage)415 public static float checkArgumentPositive(final float value, final String errorMessage) { 416 if (value <= 0) { 417 throw new IllegalArgumentException(errorMessage); 418 } 419 420 return value; 421 } 422 423 /** 424 * Ensures that the argument floating point value is a finite number. 425 * 426 * <p>A finite number is defined to be both representable (that is, not NaN) and 427 * not infinite (that is neither positive or negative infinity).</p> 428 * 429 * @param value a floating point value 430 * @param valueName the name of the argument to use if the check fails 431 * 432 * @return the validated floating point value 433 * 434 * @throws IllegalArgumentException if {@code value} was not finite 435 */ checkArgumentFinite(final float value, final String valueName)436 public static float checkArgumentFinite(final float value, final String valueName) { 437 if (Float.isNaN(value)) { 438 throw new IllegalArgumentException(valueName + " must not be NaN"); 439 } else if (Float.isInfinite(value)) { 440 throw new IllegalArgumentException(valueName + " must not be infinite"); 441 } 442 443 return value; 444 } 445 446 /** 447 * Ensures that the argument floating point value is within the inclusive range. 448 * 449 * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 450 * will always be out of range.</p> 451 * 452 * @param value a floating point value 453 * @param lower the lower endpoint of the inclusive range 454 * @param upper the upper endpoint of the inclusive range 455 * @param valueName the name of the argument to use if the check fails 456 * 457 * @return the validated floating point value 458 * 459 * @throws IllegalArgumentException if {@code value} was not within the range 460 */ checkArgumentInRange(float value, float lower, float upper, String valueName)461 public static float checkArgumentInRange(float value, float lower, float upper, 462 String valueName) { 463 if (Float.isNaN(value)) { 464 throw new IllegalArgumentException(valueName + " must not be NaN"); 465 } else if (value < lower) { 466 throw new IllegalArgumentException( 467 String.format( 468 "%s is out of range of [%f, %f] (too low)", valueName, lower, upper)); 469 } else if (value > upper) { 470 throw new IllegalArgumentException( 471 String.format( 472 "%s is out of range of [%f, %f] (too high)", valueName, lower, upper)); 473 } 474 475 return value; 476 } 477 478 /** 479 * Ensures that the argument floating point value is within the inclusive range. 480 * 481 * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 482 * will always be out of range.</p> 483 * 484 * @param value a floating point value 485 * @param lower the lower endpoint of the inclusive range 486 * @param upper the upper endpoint of the inclusive range 487 * @param valueName the name of the argument to use if the check fails 488 * 489 * @return the validated floating point value 490 * 491 * @throws IllegalArgumentException if {@code value} was not within the range 492 */ checkArgumentInRange(double value, double lower, double upper, String valueName)493 public static double checkArgumentInRange(double value, double lower, double upper, 494 String valueName) { 495 if (Double.isNaN(value)) { 496 throw new IllegalArgumentException(valueName + " must not be NaN"); 497 } else if (value < lower) { 498 throw new IllegalArgumentException( 499 String.format( 500 "%s is out of range of [%f, %f] (too low)", valueName, lower, upper)); 501 } else if (value > upper) { 502 throw new IllegalArgumentException( 503 String.format( 504 "%s is out of range of [%f, %f] (too high)", valueName, lower, upper)); 505 } 506 507 return value; 508 } 509 510 /** 511 * Ensures that the argument int value is within the inclusive range. 512 * 513 * @param value a int value 514 * @param lower the lower endpoint of the inclusive range 515 * @param upper the upper endpoint of the inclusive range 516 * @param valueName the name of the argument to use if the check fails 517 * 518 * @return the validated int value 519 * 520 * @throws IllegalArgumentException if {@code value} was not within the range 521 */ 522 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) checkArgumentInRange(int value, int lower, int upper, String valueName)523 public static int checkArgumentInRange(int value, int lower, int upper, 524 String valueName) { 525 if (value < lower) { 526 throw new IllegalArgumentException( 527 String.format( 528 "%s is out of range of [%d, %d] (too low)", valueName, lower, upper)); 529 } else if (value > upper) { 530 throw new IllegalArgumentException( 531 String.format( 532 "%s is out of range of [%d, %d] (too high)", valueName, lower, upper)); 533 } 534 535 return value; 536 } 537 538 /** 539 * Ensures that the argument long value is within the inclusive range. 540 * 541 * @param value a long value 542 * @param lower the lower endpoint of the inclusive range 543 * @param upper the upper endpoint of the inclusive range 544 * @param valueName the name of the argument to use if the check fails 545 * 546 * @return the validated long value 547 * 548 * @throws IllegalArgumentException if {@code value} was not within the range 549 */ checkArgumentInRange(long value, long lower, long upper, String valueName)550 public static long checkArgumentInRange(long value, long lower, long upper, 551 String valueName) { 552 if (value < lower) { 553 throw new IllegalArgumentException( 554 String.format( 555 "%s is out of range of [%d, %d] (too low)", valueName, lower, upper)); 556 } else if (value > upper) { 557 throw new IllegalArgumentException( 558 String.format( 559 "%s is out of range of [%d, %d] (too high)", valueName, lower, upper)); 560 } 561 562 return value; 563 } 564 565 /** 566 * Ensures that the array is not {@code null}, and none of its elements are {@code null}. 567 * 568 * @param value an array of boxed objects 569 * @param valueName the name of the argument to use if the check fails 570 * 571 * @return the validated array 572 * 573 * @throws NullPointerException if the {@code value} or any of its elements were {@code null} 574 */ checkArrayElementsNotNull(final T[] value, final String valueName)575 public static <T> T[] checkArrayElementsNotNull(final T[] value, final String valueName) { 576 if (value == null) { 577 throw new NullPointerException(valueName + " must not be null"); 578 } 579 580 for (int i = 0; i < value.length; ++i) { 581 if (value[i] == null) { 582 throw new NullPointerException( 583 String.format("%s[%d] must not be null", valueName, i)); 584 } 585 } 586 587 return value; 588 } 589 590 /** 591 * Ensures that the {@link Collection} is not {@code null}, and none of its elements are 592 * {@code null}. 593 * 594 * @param value a {@link Collection} of boxed objects 595 * @param valueName the name of the argument to use if the check fails 596 * 597 * @return the validated {@link Collection} 598 * 599 * @throws NullPointerException if the {@code value} or any of its elements were {@code null} 600 */ checkCollectionElementsNotNull( final C value, final String valueName)601 public static @NonNull <C extends Collection<T>, T> C checkCollectionElementsNotNull( 602 final C value, final String valueName) { 603 if (value == null) { 604 throw new NullPointerException(valueName + " must not be null"); 605 } 606 607 long ctr = 0; 608 for (T elem : value) { 609 if (elem == null) { 610 throw new NullPointerException( 611 String.format("%s[%d] must not be null", valueName, ctr)); 612 } 613 ++ctr; 614 } 615 616 return value; 617 } 618 619 /** 620 * Ensures that the {@link Collection} is not {@code null}, and contains at least one element. 621 * 622 * @param value a {@link Collection} of boxed elements. 623 * @param valueName the name of the argument to use if the check fails. 624 625 * @return the validated {@link Collection} 626 * 627 * @throws NullPointerException if the {@code value} was {@code null} 628 * @throws IllegalArgumentException if the {@code value} was empty 629 */ checkCollectionNotEmpty(final Collection<T> value, final String valueName)630 public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value, 631 final String valueName) { 632 if (value == null) { 633 throw new NullPointerException(valueName + " must not be null"); 634 } 635 if (value.isEmpty()) { 636 throw new IllegalArgumentException(valueName + " is empty"); 637 } 638 return value; 639 } 640 641 /** 642 * Ensures that the given byte array is not {@code null}, and contains at least one element. 643 * 644 * @param value an array of elements. 645 * @param valueName the name of the argument to use if the check fails. 646 647 * @return the validated array 648 * 649 * @throws NullPointerException if the {@code value} was {@code null} 650 * @throws IllegalArgumentException if the {@code value} was empty 651 */ 652 @NonNull checkByteArrayNotEmpty(final byte[] value, final String valueName)653 public static byte[] checkByteArrayNotEmpty(final byte[] value, final String valueName) { 654 if (value == null) { 655 throw new NullPointerException(valueName + " must not be null"); 656 } 657 if (value.length == 0) { 658 throw new IllegalArgumentException(valueName + " is empty"); 659 } 660 return value; 661 } 662 663 /** 664 * Ensures that argument {@code value} is one of {@code supportedValues}. 665 * 666 * @param supportedValues an array of string values 667 * @param value a string value 668 * 669 * @return the validated value 670 * 671 * @throws NullPointerException if either {@code value} or {@code supportedValues} is null 672 * @throws IllegalArgumentException if the {@code value} is not in {@code supportedValues} 673 */ 674 @NonNull checkArgumentIsSupported(final String[] supportedValues, final String value)675 public static String checkArgumentIsSupported(final String[] supportedValues, 676 final String value) { 677 checkNotNull(value); 678 checkNotNull(supportedValues); 679 680 if (!contains(supportedValues, value)) { 681 throw new IllegalArgumentException(value + "is not supported " 682 + Arrays.toString(supportedValues)); 683 } 684 return value; 685 } 686 contains(String[] values, String value)687 private static boolean contains(String[] values, String value) { 688 if (values == null) { 689 return false; 690 } 691 for (int i = 0; i < values.length; ++i) { 692 if (Objects.equals(value, values[i])) { 693 return true; 694 } 695 } 696 return false; 697 } 698 699 /** 700 * Ensures that all elements in the argument floating point array are within the inclusive range 701 * 702 * <p>While this can be used to range check against +/- infinity, note that all NaN numbers 703 * will always be out of range.</p> 704 * 705 * @param value a floating point array of values 706 * @param lower the lower endpoint of the inclusive range 707 * @param upper the upper endpoint of the inclusive range 708 * @param valueName the name of the argument to use if the check fails 709 * 710 * @return the validated floating point value 711 * 712 * @throws IllegalArgumentException if any of the elements in {@code value} were out of range 713 * @throws NullPointerException if the {@code value} was {@code null} 714 */ checkArrayElementsInRange(float[] value, float lower, float upper, String valueName)715 public static float[] checkArrayElementsInRange(float[] value, float lower, float upper, 716 String valueName) { 717 checkNotNull(value, "%s must not be null", valueName); 718 719 for (int i = 0; i < value.length; ++i) { 720 float v = value[i]; 721 722 if (Float.isNaN(v)) { 723 throw new IllegalArgumentException(valueName + "[" + i + "] must not be NaN"); 724 } else if (v < lower) { 725 throw new IllegalArgumentException( 726 String.format("%s[%d] is out of range of [%f, %f] (too low)", 727 valueName, i, lower, upper)); 728 } else if (v > upper) { 729 throw new IllegalArgumentException( 730 String.format("%s[%d] is out of range of [%f, %f] (too high)", 731 valueName, i, lower, upper)); 732 } 733 } 734 735 return value; 736 } 737 738 /** 739 * Ensures that all elements in the argument integer array are within the inclusive range 740 * 741 * @param value an integer array of values 742 * @param lower the lower endpoint of the inclusive range 743 * @param upper the upper endpoint of the inclusive range 744 * @param valueName the name of the argument to use if the check fails 745 * 746 * @return the validated integer array 747 * 748 * @throws IllegalArgumentException if any of the elements in {@code value} were out of range 749 * @throws NullPointerException if the {@code value} was {@code null} 750 */ checkArrayElementsInRange(int[] value, int lower, int upper, String valueName)751 public static int[] checkArrayElementsInRange(int[] value, int lower, int upper, 752 String valueName) { 753 checkNotNull(value, "%s must not be null", valueName); 754 755 for (int i = 0; i < value.length; ++i) { 756 int v = value[i]; 757 758 if (v < lower) { 759 throw new IllegalArgumentException( 760 String.format("%s[%d] is out of range of [%d, %d] (too low)", 761 valueName, i, lower, upper)); 762 } else if (v > upper) { 763 throw new IllegalArgumentException( 764 String.format("%s[%d] is out of range of [%d, %d] (too high)", 765 valueName, i, lower, upper)); 766 } 767 } 768 769 return value; 770 } 771 } 772