001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3; 018 019import java.util.Collection; 020import java.util.Map; 021import java.util.Objects; 022import java.util.concurrent.atomic.AtomicInteger; 023import java.util.function.Supplier; 024import java.util.regex.Pattern; 025 026/** 027 * This class assists in validating arguments. The validation methods are 028 * based along the following principles: 029 * <ul> 030 * <li>An invalid {@code null} argument causes a {@link NullPointerException}.</li> 031 * <li>A non-{@code null} argument causes an {@link IllegalArgumentException}.</li> 032 * <li>An invalid index into an array/collection/map/string causes an {@link IndexOutOfBoundsException}.</li> 033 * </ul> 034 * 035 * <p>All exceptions messages are 036 * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax">format strings</a> 037 * as defined by the Java platform. For example: 038 * 039 * <pre> 040 * Validate.isTrue(i > 0, "The value must be greater than zero: %d", i); 041 * Validate.notNull(surname, "The surname must not be %s", null); 042 * </pre> 043 * 044 * <p>#ThreadSafe#</p> 045 * 046 * @see String#format(String, Object...) 047 * @since 2.0 048 */ 049public class Validate { 050 051 private static final String DEFAULT_NOT_NAN_EX_MESSAGE = 052 "The validated value is not a number"; 053 private static final String DEFAULT_FINITE_EX_MESSAGE = 054 "The value is invalid: %f"; 055 private static final String DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE = 056 "The value %s is not in the specified exclusive range of %s to %s"; 057 private static final String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE = 058 "The value %s is not in the specified inclusive range of %s to %s"; 059 private static final String DEFAULT_MATCHES_PATTERN_EX = "The string %s does not match the pattern %s"; 060 private static final String DEFAULT_IS_NULL_EX_MESSAGE = "The validated object is null"; 061 private static final String DEFAULT_IS_TRUE_EX_MESSAGE = "The validated expression is false"; 062 private static final String DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE = 063 "The validated array contains null element at index: %d"; 064 private static final String DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE = 065 "The validated collection contains null element at index: %d"; 066 private static final String DEFAULT_NOT_BLANK_EX_MESSAGE = "The validated character sequence is blank"; 067 private static final String DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE = "The validated array is empty"; 068 private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE = 069 "The validated character sequence is empty"; 070 private static final String DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE = "The validated collection is empty"; 071 private static final String DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE = "The validated map is empty"; 072 private static final String DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE = "The validated array index is invalid: %d"; 073 private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = 074 "The validated character sequence index is invalid: %d"; 075 private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = 076 "The validated collection index is invalid: %d"; 077 private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false"; 078 private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = "Cannot assign a %s to a %s"; 079 private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "Expected type: %s, actual: %s"; 080 081 /** 082 * Validate that the specified primitive value falls between the two 083 * exclusive values specified; otherwise, throws an exception. 084 * 085 * <pre>Validate.exclusiveBetween(0.1, 2.1, 1.1);</pre> 086 * 087 * @param start the exclusive start value. 088 * @param end the exclusive end value. 089 * @param value the value to validate. 090 * @throws IllegalArgumentException if the value falls out of the boundaries. 091 * @since 3.3 092 */ 093 @SuppressWarnings("boxing") 094 public static void exclusiveBetween(final double start, final double end, final double value) { 095 // TODO when breaking BC, consider returning value 096 if (value <= start || value >= end) { 097 throw new IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); 098 } 099 } 100 101 /** 102 * Validate that the specified primitive value falls between the two 103 * exclusive values specified; otherwise, throws an exception with the 104 * specified message. 105 * 106 * <pre>Validate.exclusiveBetween(0.1, 2.1, 1.1, "Not in range");</pre> 107 * 108 * @param start the exclusive start value. 109 * @param end the exclusive end value. 110 * @param value the value to validate. 111 * @param message the exception message if invalid, not null. 112 * @throws IllegalArgumentException if the value falls outside the boundaries. 113 * @since 3.3 114 */ 115 public static void exclusiveBetween(final double start, final double end, final double value, final String message) { 116 // TODO when breaking BC, consider returning value 117 if (value <= start || value >= end) { 118 throw new IllegalArgumentException(message); 119 } 120 } 121 122 /** 123 * Validate that the specified primitive value falls between the two 124 * exclusive values specified; otherwise, throws an exception. 125 * 126 * <pre>Validate.exclusiveBetween(0, 2, 1);</pre> 127 * 128 * @param start the exclusive start value. 129 * @param end the exclusive end value. 130 * @param value the value to validate. 131 * @throws IllegalArgumentException if the value falls out of the boundaries. 132 * @since 3.3 133 */ 134 @SuppressWarnings("boxing") 135 public static void exclusiveBetween(final long start, final long end, final long value) { 136 // TODO when breaking BC, consider returning value 137 if (value <= start || value >= end) { 138 throw new IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); 139 } 140 } 141 142 /** 143 * Validate that the specified primitive value falls between the two 144 * exclusive values specified; otherwise, throws an exception with the 145 * specified message. 146 * 147 * <pre>Validate.exclusiveBetween(0, 2, 1, "Not in range");</pre> 148 * 149 * @param start the exclusive start value. 150 * @param end the exclusive end value. 151 * @param value the value to validate. 152 * @param message the exception message if invalid, not null. 153 * @throws IllegalArgumentException if the value falls outside the boundaries. 154 * @since 3.3 155 */ 156 public static void exclusiveBetween(final long start, final long end, final long value, final String message) { 157 // TODO when breaking BC, consider returning value 158 if (value <= start || value >= end) { 159 throw new IllegalArgumentException(message); 160 } 161 } 162 163 /** 164 * Validate that the specified argument object fall between the two 165 * exclusive values specified; otherwise, throws an exception. 166 * 167 * <pre>Validate.exclusiveBetween(0, 2, 1);</pre> 168 * 169 * @param <T> the type of the argument object. 170 * @param start the exclusive start value, not null. 171 * @param end the exclusive end value, not null. 172 * @param value the object to validate, not null. 173 * @throws IllegalArgumentException if the value falls outside the boundaries. 174 * @see #exclusiveBetween(Object, Object, Comparable, String, Object...) 175 * @since 3.0 176 */ 177 public static <T> void exclusiveBetween(final T start, final T end, final Comparable<T> value) { 178 // TODO when breaking BC, consider returning value 179 if (value.compareTo(start) <= 0 || value.compareTo(end) >= 0) { 180 throw new IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); 181 } 182 } 183 184 /** 185 * Validate that the specified argument object fall between the two 186 * exclusive values specified; otherwise, throws an exception with the 187 * specified message. 188 * 189 * <pre>Validate.exclusiveBetween(0, 2, 1, "Not in boundaries");</pre> 190 * 191 * @param <T> the type of the argument object. 192 * @param start the exclusive start value, not null. 193 * @param end the exclusive end value, not null. 194 * @param value the object to validate, not null. 195 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 196 * @param values the optional values for the formatted exception message, null array not recommended. 197 * @throws IllegalArgumentException if the value falls outside the boundaries. 198 * @see #exclusiveBetween(Object, Object, Comparable) 199 * @since 3.0 200 */ 201 public static <T> void exclusiveBetween(final T start, final T end, final Comparable<T> value, final String message, final Object... values) { 202 // TODO when breaking BC, consider returning value 203 if (value.compareTo(start) <= 0 || value.compareTo(end) >= 0) { 204 throw new IllegalArgumentException(getMessage(message, values)); 205 } 206 } 207 208 /** 209 * Validates that the specified argument is not infinite or Not-a-Number (NaN); 210 * otherwise throwing an exception. 211 * 212 * <pre>Validate.finite(myDouble);</pre> 213 * 214 * <p>The message of the exception is "The value is invalid: %f".</p> 215 * 216 * @param value the value to validate. 217 * @throws IllegalArgumentException if the value is infinite or Not-a-Number (NaN). 218 * @see #finite(double, String, Object...) 219 * @since 3.5 220 */ 221 public static void finite(final double value) { 222 finite(value, DEFAULT_FINITE_EX_MESSAGE, value); 223 } 224 225 /** 226 * Validates that the specified argument is not infinite or Not-a-Number (NaN); 227 * otherwise throwing an exception with the specified message. 228 * 229 * <pre>Validate.finite(myDouble, "The argument must contain a numeric value");</pre> 230 * 231 * @param value the value to validate. 232 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 233 * @param values the optional values for the formatted exception message. 234 * @throws IllegalArgumentException if the value is infinite or Not-a-Number (NaN). 235 * @see #finite(double) 236 * @since 3.5 237 */ 238 public static void finite(final double value, final String message, final Object... values) { 239 if (Double.isNaN(value) || Double.isInfinite(value)) { 240 throw new IllegalArgumentException(getMessage(message, values)); 241 } 242 } 243 244 /** 245 * Gets the message using {@link String#format(String, Object...) String.format(message, values)} if the values are not empty, otherwise return the message 246 * unformatted. This method exists to allow validation methods declaring a String message and varargs parameters to be used without any message parameters 247 * when the message contains special characters, e.g. {@code Validate.isTrue(false, "%Failed%")}. 248 * 249 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 250 * @param values the optional values for the formatted message. 251 * @return formatted message using {@link String#format(String, Object...) String.format(message, values)} if the values are not empty, otherwise return the 252 * unformatted message. 253 */ 254 private static String getMessage(final String message, final Object... values) { 255 return ArrayUtils.isEmpty(values) ? message : String.format(message, values); 256 } 257 258 /** 259 * Validate that the specified primitive value falls between the two 260 * inclusive values specified; otherwise, throws an exception. 261 * 262 * <pre>Validate.inclusiveBetween(0.1, 2.1, 1.1);</pre> 263 * 264 * @param start the inclusive start value. 265 * @param end the inclusive end value. 266 * @param value the value to validate. 267 * @throws IllegalArgumentException if the value falls outside the boundaries (inclusive). 268 * @since 3.3 269 */ 270 @SuppressWarnings("boxing") 271 public static void inclusiveBetween(final double start, final double end, final double value) { 272 // TODO when breaking BC, consider returning value 273 if (value < start || value > end) { 274 throw new IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); 275 } 276 } 277 278 /** 279 * Validate that the specified primitive value falls between the two 280 * inclusive values specified; otherwise, throws an exception with the 281 * specified message. 282 * 283 * <pre>Validate.inclusiveBetween(0.1, 2.1, 1.1, "Not in range");</pre> 284 * 285 * @param start the inclusive start value. 286 * @param end the inclusive end value. 287 * @param value the value to validate. 288 * @param message the exception message if invalid, not null. 289 * @throws IllegalArgumentException if the value falls outside the boundaries. 290 * @since 3.3 291 */ 292 public static void inclusiveBetween(final double start, final double end, final double value, final String message) { 293 // TODO when breaking BC, consider returning value 294 if (value < start || value > end) { 295 throw new IllegalArgumentException(message); 296 } 297 } 298 299 /** 300 * Validate that the specified primitive value falls between the two 301 * inclusive values specified; otherwise, throws an exception. 302 * 303 * <pre>Validate.inclusiveBetween(0, 2, 1);</pre> 304 * 305 * @param start the inclusive start value. 306 * @param end the inclusive end value. 307 * @param value the value to validate. 308 * @throws IllegalArgumentException if the value falls outside the boundaries (inclusive). 309 * @since 3.3 310 */ 311 @SuppressWarnings("boxing") 312 public static void inclusiveBetween(final long start, final long end, final long value) { 313 // TODO when breaking BC, consider returning value 314 if (value < start || value > end) { 315 throw new IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); 316 } 317 } 318 319 /** 320 * Validate that the specified primitive value falls between the two 321 * inclusive values specified; otherwise, throws an exception with the 322 * specified message. 323 * 324 * <pre>Validate.inclusiveBetween(0, 2, 1, "Not in range");</pre> 325 * 326 * @param start the inclusive start value. 327 * @param end the inclusive end value. 328 * @param value the value to validate. 329 * @param message the exception message if invalid, not null. 330 * @throws IllegalArgumentException if the value falls outside the boundaries. 331 * @since 3.3 332 */ 333 public static void inclusiveBetween(final long start, final long end, final long value, final String message) { 334 // TODO when breaking BC, consider returning value 335 if (value < start || value > end) { 336 throw new IllegalArgumentException(message); 337 } 338 } 339 340 /** 341 * Validate that the specified argument object fall between the two 342 * inclusive values specified; otherwise, throws an exception. 343 * 344 * <pre>Validate.inclusiveBetween(0, 2, 1);</pre> 345 * 346 * @param <T> the type of the argument object. 347 * @param start the inclusive start value, not null. 348 * @param end the inclusive end value, not null. 349 * @param value the object to validate, not null. 350 * @throws IllegalArgumentException if the value falls outside the boundaries. 351 * @see #inclusiveBetween(Object, Object, Comparable, String, Object...) 352 * @since 3.0 353 */ 354 public static <T> void inclusiveBetween(final T start, final T end, final Comparable<T> value) { 355 // TODO when breaking BC, consider returning value 356 if (value.compareTo(start) < 0 || value.compareTo(end) > 0) { 357 throw new IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); 358 } 359 } 360 361 /** 362 * Validate that the specified argument object fall between the two 363 * inclusive values specified; otherwise, throws an exception with the 364 * specified message. 365 * 366 * <pre>Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");</pre> 367 * 368 * @param <T> the type of the argument object. 369 * @param start the inclusive start value, not null. 370 * @param end the inclusive end value, not null. 371 * @param value the object to validate, not null. 372 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 373 * @param values the optional values for the formatted exception message, null array not recommended. 374 * @throws IllegalArgumentException if the value falls outside the boundaries. 375 * @see #inclusiveBetween(Object, Object, Comparable) 376 * @since 3.0 377 */ 378 public static <T> void inclusiveBetween(final T start, final T end, final Comparable<T> value, final String message, final Object... values) { 379 // TODO when breaking BC, consider returning value 380 if (value.compareTo(start) < 0 || value.compareTo(end) > 0) { 381 throw new IllegalArgumentException(getMessage(message, values)); 382 } 383 } 384 385 /** 386 * Validates that the argument can be converted to the specified class, if not, throws an exception. 387 * 388 * <p>This method is useful when validating that there will be no casting errors.</p> 389 * 390 * <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre> 391 * 392 * <p>The message format of the exception is "Cannot assign {type} to {superType}"</p> 393 * 394 * @param superType the class must be validated against, not null. 395 * @param type the class to check, not null. 396 * @throws IllegalArgumentException if type argument is not assignable to the specified superType. 397 * @see #isAssignableFrom(Class, Class, String, Object...) 398 * @since 3.0 399 */ 400 public static void isAssignableFrom(final Class<?> superType, final Class<?> type) { 401 // TODO when breaking BC, consider returning type 402 if (type == null || superType == null || !superType.isAssignableFrom(type)) { 403 throw new IllegalArgumentException( 404 String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, ClassUtils.getName(type, "null type"), ClassUtils.getName(superType, "null type"))); 405 } 406 } 407 408 /** 409 * Validates that the argument can be converted to the specified class, if not throws an exception. 410 * 411 * <p>This method is useful when validating if there will be no casting errors.</p> 412 * 413 * <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre> 414 * 415 * <p>The message of the exception is "The validated object cannot be converted to the" 416 * followed by the name of the class and "class"</p> 417 * 418 * @param superType the class must be validated against, not null. 419 * @param type the class to check, not null. 420 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 421 * @param values the optional values for the formatted exception message, null array not recommended. 422 * @throws IllegalArgumentException if argument cannot be converted to the specified class. 423 * @see #isAssignableFrom(Class, Class) 424 */ 425 public static void isAssignableFrom(final Class<?> superType, final Class<?> type, final String message, final Object... values) { 426 // TODO when breaking BC, consider returning type 427 if (!superType.isAssignableFrom(type)) { 428 throw new IllegalArgumentException(getMessage(message, values)); 429 } 430 } 431 432 /** 433 * Validates that the argument is an instance of the specified class, if not throws an exception. 434 * 435 * <p>This method is useful when validating according to an arbitrary class</p> 436 * 437 * <pre>Validate.isInstanceOf(OkClass.class, object);</pre> 438 * 439 * <p>The message of the exception is "Expected type: {type}, actual: {obj_type}"</p> 440 * 441 * @param type the class the object must be validated against, not null. 442 * @param obj the object to check, null throws an exception. 443 * @throws IllegalArgumentException if argument is not of specified class. 444 * @see #isInstanceOf(Class, Object, String, Object...) 445 * @since 3.0 446 */ 447 public static void isInstanceOf(final Class<?> type, final Object obj) { 448 // TODO when breaking BC, consider returning obj 449 if (!type.isInstance(obj)) { 450 throw new IllegalArgumentException(String.format(DEFAULT_IS_INSTANCE_OF_EX_MESSAGE, type.getName(), ClassUtils.getName(obj, "null"))); 451 } 452 } 453 454 /** 455 * Validate that the argument is an instance of the specified class; otherwise 456 * throwing an exception with the specified message. This method is useful when 457 * validating according to an arbitrary class 458 * 459 * <pre>Validate.isInstanceOf(OkClass.class, object, "Wrong class, object is of class %s", 460 * object.getClass().getName());</pre> 461 * 462 * @param type the class the object must be validated against, not null. 463 * @param obj the object to check, null throws an exception. 464 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 465 * @param values the optional values for the formatted exception message, null array not recommended. 466 * @throws IllegalArgumentException if argument is not of specified class. 467 * @see #isInstanceOf(Class, Object) 468 * @since 3.0 469 */ 470 public static void isInstanceOf(final Class<?> type, final Object obj, final String message, final Object... values) { 471 // TODO when breaking BC, consider returning obj 472 if (!type.isInstance(obj)) { 473 throw new IllegalArgumentException(getMessage(message, values)); 474 } 475 } 476 477 /** 478 * Validate that the argument condition is {@code true}; otherwise 479 * throwing an exception. This method is useful when validating according 480 * to an arbitrary boolean expression, such as validating a 481 * primitive number or using your own custom validation expression. 482 * 483 * <pre> 484 * Validate.isTrue(i > 0); 485 * Validate.isTrue(myObject.isOk());</pre> 486 * 487 * <p>The message of the exception is "The validated expression is 488 * false".</p> 489 * 490 * @param expression the boolean expression to check. 491 * @throws IllegalArgumentException if expression is {@code false}. 492 * @see #isTrue(boolean, String, long) 493 * @see #isTrue(boolean, String, double) 494 * @see #isTrue(boolean, String, Object...) 495 * @see #isTrue(boolean, Supplier) 496 */ 497 public static void isTrue(final boolean expression) { 498 if (!expression) { 499 throw new IllegalArgumentException(DEFAULT_IS_TRUE_EX_MESSAGE); 500 } 501 } 502 503 /** 504 * Validate that the argument condition is {@code true}; otherwise 505 * throwing an exception with the specified message. This method is useful when 506 * validating according to an arbitrary boolean expression, such as validating a 507 * primitive number or using your own custom validation expression. 508 * 509 * <pre>Validate.isTrue(d > 0.0, "The value must be greater than zero: %s", d);</pre> 510 * 511 * <p>For performance reasons, the double value is passed as a separate parameter and 512 * appended to the exception message only in the case of an error.</p> 513 * 514 * @param expression the boolean expression to check. 515 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 516 * @param value the value to append to the message when invalid. 517 * @throws IllegalArgumentException if expression is {@code false}. 518 * @see #isTrue(boolean) 519 * @see #isTrue(boolean, String, long) 520 * @see #isTrue(boolean, String, Object...) 521 * @see #isTrue(boolean, Supplier) 522 */ 523 public static void isTrue(final boolean expression, final String message, final double value) { 524 if (!expression) { 525 throw new IllegalArgumentException(String.format(message, Double.valueOf(value))); 526 } 527 } 528 529 /** 530 * Validate that the argument condition is {@code true}; otherwise 531 * throwing an exception with the specified message. This method is useful when 532 * validating according to an arbitrary boolean expression, such as validating a 533 * primitive number or using your own custom validation expression. 534 * 535 * <pre>Validate.isTrue(i > 0.0, "The value must be greater than zero: %d", i);</pre> 536 * 537 * <p>For performance reasons, the long value is passed as a separate parameter and 538 * appended to the exception message only in the case of an error.</p> 539 * 540 * @param expression the boolean expression to check. 541 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 542 * @param value the value to append to the message when invalid. 543 * @throws IllegalArgumentException if expression is {@code false}. 544 * @see #isTrue(boolean) 545 * @see #isTrue(boolean, String, double) 546 * @see #isTrue(boolean, String, Object...) 547 * @see #isTrue(boolean, Supplier) 548 */ 549 public static void isTrue(final boolean expression, final String message, final long value) { 550 if (!expression) { 551 throw new IllegalArgumentException(String.format(message, Long.valueOf(value))); 552 } 553 } 554 555 /** 556 * Validate that the argument condition is {@code true}; otherwise 557 * throwing an exception with the specified message. This method is useful when 558 * validating according to an arbitrary boolean expression, such as validating a 559 * primitive number or using your own custom validation expression. 560 * 561 * <pre>{@code 562 * Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max);}</pre> 563 * 564 * @param expression the boolean expression to check. 565 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 566 * @param values the optional values for the formatted exception message, null array not recommended. 567 * @throws IllegalArgumentException if expression is {@code false}. 568 * @see #isTrue(boolean) 569 * @see #isTrue(boolean, String, long) 570 * @see #isTrue(boolean, String, double) 571 * @see #isTrue(boolean, Supplier) 572 */ 573 public static void isTrue(final boolean expression, final String message, final Object... values) { 574 if (!expression) { 575 throw new IllegalArgumentException(getMessage(message, values)); 576 } 577 } 578 579 /** 580 * Validate that the argument condition is {@code true}; otherwise throwing an exception with the specified message. This method is useful when validating 581 * according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression. 582 * 583 * <pre>{@code 584 * Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max); 585 * }</pre> 586 * 587 * @param expression the boolean expression to check. 588 * @param messageSupplier the exception message supplier. 589 * @throws IllegalArgumentException if expression is {@code false}. 590 * @see #isTrue(boolean) 591 * @see #isTrue(boolean, String, long) 592 * @see #isTrue(boolean, String, double) 593 * @since 3.18.0 594 */ 595 public static void isTrue(final boolean expression, final Supplier<String> messageSupplier) { 596 if (!expression) { 597 throw new IllegalArgumentException(messageSupplier.get()); 598 } 599 } 600 601 /** 602 * Validate that the specified argument character sequence matches the specified regular 603 * expression pattern; otherwise throwing an exception. 604 * 605 * <pre>Validate.matchesPattern("hi", "[a-z]*");</pre> 606 * 607 * <p>The syntax of the pattern is the one used in the {@link Pattern} class.</p> 608 * 609 * @param input the character sequence to validate, not null. 610 * @param pattern the regular expression pattern, not null. 611 * @throws IllegalArgumentException if the character sequence does not match the pattern. 612 * @see #matchesPattern(CharSequence, String, String, Object...) 613 * @since 3.0 614 */ 615 public static void matchesPattern(final CharSequence input, final String pattern) { 616 // TODO when breaking BC, consider returning input 617 if (!Pattern.matches(pattern, input)) { 618 throw new IllegalArgumentException(String.format(DEFAULT_MATCHES_PATTERN_EX, input, pattern)); 619 } 620 } 621 622 /** 623 * Validate that the specified argument character sequence matches the specified regular 624 * expression pattern; otherwise throwing an exception with the specified message. 625 * 626 * <pre>Validate.matchesPattern("hi", "[a-z]*", "%s does not match %s", "hi" "[a-z]*");</pre> 627 * 628 * <p>The syntax of the pattern is the one used in the {@link Pattern} class.</p> 629 * 630 * @param input the character sequence to validate, not null. 631 * @param pattern the regular expression pattern, not null. 632 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 633 * @param values the optional values for the formatted exception message, null array not recommended. 634 * @throws IllegalArgumentException if the character sequence does not match the pattern. 635 * @see #matchesPattern(CharSequence, String) 636 * @since 3.0 637 */ 638 public static void matchesPattern(final CharSequence input, final String pattern, final String message, final Object... values) { 639 // TODO when breaking BC, consider returning input 640 if (!Pattern.matches(pattern, input)) { 641 throw new IllegalArgumentException(getMessage(message, values)); 642 } 643 } 644 645 /** 646 * Validate that the specified argument iterable is neither 647 * {@code null} nor contains any elements that are {@code null}; 648 * otherwise throwing an exception. 649 * 650 * <pre>Validate.noNullElements(myCollection);</pre> 651 * 652 * <p>If the iterable is {@code null}, then the message in the exception 653 * is "The validated object is null". 654 * 655 * <p>If the array has a {@code null} element, then the message in the 656 * exception is "The validated iterable contains null element at index: 657 * " followed by the index.</p> 658 * 659 * @param <T> the iterable type. 660 * @param iterable the iterable to check, validated not null by this method. 661 * @return the validated iterable (never {@code null} method for chaining). 662 * @throws NullPointerException if the array is {@code null}. 663 * @throws IllegalArgumentException if an element is {@code null}. 664 * @see #noNullElements(Iterable, String, Object...) 665 */ 666 public static <T extends Iterable<?>> T noNullElements(final T iterable) { 667 return noNullElements(iterable, DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE); 668 } 669 670 /** 671 * Validate that the specified argument iterable is neither 672 * {@code null} nor contains any elements that are {@code null}; 673 * otherwise throwing an exception with the specified message. 674 * 675 * <pre>Validate.noNullElements(myCollection, "The collection contains null at position %d");</pre> 676 * 677 * <p>If the iterable is {@code null}, then the message in the exception 678 * is "The validated object is null". 679 * 680 * <p>If the iterable has a {@code null} element, then the iteration 681 * index of the invalid element is appended to the {@code values} 682 * argument.</p> 683 * 684 * @param <T> the iterable type. 685 * @param iterable the iterable to check, validated not null by this method. 686 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 687 * @param values the optional values for the formatted exception message, null array not recommended. 688 * @return the validated iterable (never {@code null} method for chaining). 689 * @throws NullPointerException if the array is {@code null}. 690 * @throws IllegalArgumentException if an element is {@code null}. 691 * @see #noNullElements(Iterable) 692 */ 693 public static <T extends Iterable<?>> T noNullElements(final T iterable, final String message, final Object... values) { 694 Objects.requireNonNull(iterable, "iterable"); 695 final AtomicInteger ai = new AtomicInteger(); 696 iterable.forEach(e -> { 697 if (e == null) { 698 throw new IllegalArgumentException(getMessage(message, ArrayUtils.addAll(values, ai.getAndIncrement()))); 699 } 700 }); 701 return iterable; 702 } 703 704 /** 705 * Validate that the specified argument array is neither 706 * {@code null} nor contains any elements that are {@code null}; 707 * otherwise throwing an exception. 708 * 709 * <pre>Validate.noNullElements(myArray);</pre> 710 * 711 * <p>If the array is {@code null}, then the message in the exception 712 * is "The validated object is null".</p> 713 * 714 * <p>If the array has a {@code null} element, then the message in the 715 * exception is "The validated array contains null element at index: 716 * " followed by the index.</p> 717 * 718 * @param <T> the array type. 719 * @param array the array to check, validated not null by this method. 720 * @return the validated array (never {@code null} method for chaining). 721 * @throws NullPointerException if the array is {@code null}. 722 * @throws IllegalArgumentException if an element is {@code null}. 723 * @see #noNullElements(Object[], String, Object...) 724 */ 725 public static <T> T[] noNullElements(final T[] array) { 726 return noNullElements(array, DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE); 727 } 728 729 /** 730 * Validate that the specified argument array is neither 731 * {@code null} nor contains any elements that are {@code null}; 732 * otherwise throwing an exception with the specified message. 733 * 734 * <pre>Validate.noNullElements(myArray, "The array contain null at position %d");</pre> 735 * 736 * <p>If the array is {@code null}, then the message in the exception 737 * is "The validated object is null". 738 * 739 * <p>If the array has a {@code null} element, then the iteration 740 * index of the invalid element is appended to the {@code values} 741 * argument.</p> 742 * 743 * @param <T> the array type. 744 * @param array the array to check, validated not null by this method. 745 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 746 * @param values the optional values for the formatted exception message, null array not recommended. 747 * @return the validated array (never {@code null} method for chaining). 748 * @throws NullPointerException if the array is {@code null}. 749 * @throws IllegalArgumentException if an element is {@code null}. 750 * @see #noNullElements(Object[]) 751 */ 752 public static <T> T[] noNullElements(final T[] array, final String message, final Object... values) { 753 Objects.requireNonNull(array, "array"); 754 for (int i = 0; i < array.length; i++) { 755 if (array[i] == null) { 756 final Object[] values2 = ArrayUtils.add(values, Integer.valueOf(i)); 757 throw new IllegalArgumentException(getMessage(message, values2)); 758 } 759 } 760 return array; 761 } 762 763 /** 764 * <p>Validates that the specified argument character sequence is 765 * neither {@code null}, a length of zero (no characters), empty 766 * nor whitespace; otherwise throwing an exception. 767 * 768 * <pre>Validate.notBlank(myString);</pre> 769 * 770 * <p>The message in the exception is "The validated character 771 * sequence is blank". 772 * 773 * @param <T> the character sequence type. 774 * @param chars the character sequence to check, validated not null by this method. 775 * @return the validated character sequence (never {@code null} method for chaining). 776 * @throws NullPointerException if the character sequence is {@code null}. 777 * @throws IllegalArgumentException if the character sequence is blank. 778 * @see #notBlank(CharSequence, String, Object...) 779 * @since 3.0 780 */ 781 public static <T extends CharSequence> T notBlank(final T chars) { 782 return notBlank(chars, DEFAULT_NOT_BLANK_EX_MESSAGE); 783 } 784 785 /** 786 * Validates that the specified argument character sequence is not {@link StringUtils#isBlank(CharSequence) blank} (whitespaces, empty ({@code ""}) or 787 * {@code null}); otherwise throwing an exception with the specified message. 788 * 789 * <pre> 790 * Validate.notBlank(myString, "The string must not be blank"); 791 * </pre> 792 * 793 * @param <T> the character sequence type. 794 * @param chars the character sequence to check, validated not null by this method. 795 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 796 * @param values the optional values for the formatted exception message, null array not recommended. 797 * @return the validated character sequence (never {@code null} method for chaining). 798 * @throws NullPointerException if the character sequence is {@code null}. 799 * @throws IllegalArgumentException if the character sequence is blank. 800 * @see #notBlank(CharSequence) 801 * @see StringUtils#isBlank(CharSequence) 802 * @since 3.0 803 */ 804 public static <T extends CharSequence> T notBlank(final T chars, final String message, final Object... values) { 805 Objects.requireNonNull(chars, toSupplier(message, values)); 806 if (StringUtils.isBlank(chars)) { 807 throw new IllegalArgumentException(getMessage(message, values)); 808 } 809 return chars; 810 } 811 812 /** 813 * <p>Validates that the specified argument collection is neither {@code null} 814 * nor a size of zero (no elements); otherwise throwing an exception. 815 * 816 * <pre>Validate.notEmpty(myCollection);</pre> 817 * 818 * <p>The message in the exception is "The validated collection is 819 * empty". 820 * 821 * @param <T> the collection type. 822 * @param collection the collection to check, validated not null by this method. 823 * @return the validated collection (never {@code null} method for chaining). 824 * @throws NullPointerException if the collection is {@code null}. 825 * @throws IllegalArgumentException if the collection is empty. 826 * @see #notEmpty(Collection, String, Object...) 827 */ 828 public static <T extends Collection<?>> T notEmpty(final T collection) { 829 return notEmpty(collection, DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE); 830 } 831 832 /** 833 * <p>Validates that the specified argument map is neither {@code null} 834 * nor a size of zero (no elements); otherwise throwing an exception. 835 * 836 * <pre>Validate.notEmpty(myMap);</pre> 837 * 838 * <p>The message in the exception is "The validated map is 839 * empty". 840 * 841 * @param <T> the map type. 842 * @param map the map to check, validated not null by this method. 843 * @return the validated map (never {@code null} method for chaining). 844 * @throws NullPointerException if the map is {@code null}. 845 * @throws IllegalArgumentException if the map is empty. 846 * @see #notEmpty(Map, String, Object...) 847 */ 848 public static <T extends Map<?, ?>> T notEmpty(final T map) { 849 return notEmpty(map, DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE); 850 } 851 852 /** 853 * <p>Validates that the specified argument character sequence is 854 * neither {@code null} nor a length of zero (no characters); 855 * otherwise throwing an exception with the specified message. 856 * 857 * <pre>Validate.notEmpty(myString);</pre> 858 * 859 * <p>The message in the exception is "The validated 860 * character sequence is empty". 861 * 862 * @param <T> the character sequence type. 863 * @param chars the character sequence to check, validated not null by this method. 864 * @return the validated character sequence (never {@code null} method for chaining). 865 * @throws NullPointerException if the character sequence is {@code null}. 866 * @throws IllegalArgumentException if the character sequence is empty. 867 * @see #notEmpty(CharSequence, String, Object...) 868 */ 869 public static <T extends CharSequence> T notEmpty(final T chars) { 870 return notEmpty(chars, DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE); 871 } 872 873 /** 874 * <p>Validates that the specified argument collection is neither {@code null} 875 * nor a size of zero (no elements); otherwise throwing an exception 876 * with the specified message. 877 * 878 * <pre>Validate.notEmpty(myCollection, "The collection must not be empty");</pre> 879 * 880 * @param <T> the collection type. 881 * @param collection the collection to check, validated not null by this method. 882 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 883 * @param values the optional values for the formatted exception message, null array not recommended. 884 * @return the validated collection (never {@code null} method for chaining). 885 * @throws NullPointerException if the collection is {@code null}. 886 * @throws IllegalArgumentException if the collection is empty. 887 * @see #notEmpty(Object[]) 888 */ 889 public static <T extends Collection<?>> T notEmpty(final T collection, final String message, final Object... values) { 890 Objects.requireNonNull(collection, toSupplier(message, values)); 891 if (collection.isEmpty()) { 892 throw new IllegalArgumentException(getMessage(message, values)); 893 } 894 return collection; 895 } 896 897 /** 898 * Validate that the specified argument map is neither {@code null} 899 * nor a size of zero (no elements); otherwise throwing an exception 900 * with the specified message. 901 * 902 * <pre>Validate.notEmpty(myMap, "The map must not be empty");</pre> 903 * 904 * @param <T> the map type. 905 * @param map the map to check, validated not null by this method. 906 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 907 * @param values the optional values for the formatted exception message, null array not recommended. 908 * @return the validated map (never {@code null} method for chaining). 909 * @throws NullPointerException if the map is {@code null}. 910 * @throws IllegalArgumentException if the map is empty. 911 * @see #notEmpty(Object[]) 912 */ 913 public static <T extends Map<?, ?>> T notEmpty(final T map, final String message, final Object... values) { 914 Objects.requireNonNull(map, toSupplier(message, values)); 915 if (map.isEmpty()) { 916 throw new IllegalArgumentException(getMessage(message, values)); 917 } 918 return map; 919 } 920 921 /** 922 * Validate that the specified argument character sequence is 923 * neither {@code null} nor a length of zero (no characters); 924 * otherwise throwing an exception with the specified message. 925 * 926 * <pre>Validate.notEmpty(myString, "The string must not be empty");</pre> 927 * 928 * @param <T> the character sequence type. 929 * @param chars the character sequence to check, validated not null by this method. 930 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 931 * @param values the optional values for the formatted exception message, null array not recommended. 932 * @return the validated character sequence (never {@code null} method for chaining). 933 * @throws NullPointerException if the character sequence is {@code null}. 934 * @throws IllegalArgumentException if the character sequence is empty. 935 * @see #notEmpty(CharSequence) 936 */ 937 public static <T extends CharSequence> T notEmpty(final T chars, final String message, final Object... values) { 938 Objects.requireNonNull(chars, toSupplier(message, values)); 939 if (chars.length() == 0) { 940 throw new IllegalArgumentException(getMessage(message, values)); 941 } 942 return chars; 943 } 944 945 /** 946 * <p>Validates that the specified argument array is neither {@code null} 947 * nor a length of zero (no elements); otherwise throwing an exception. 948 * 949 * <pre>Validate.notEmpty(myArray);</pre> 950 * 951 * <p>The message in the exception is "The validated array is 952 * empty". 953 * 954 * @param <T> the array type. 955 * @param array the array to check, validated not null by this method. 956 * @return the validated array (never {@code null} method for chaining). 957 * @throws NullPointerException if the array is {@code null}. 958 * @throws IllegalArgumentException if the array is empty. 959 * @see #notEmpty(Object[], String, Object...) 960 */ 961 public static <T> T[] notEmpty(final T[] array) { 962 return notEmpty(array, DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE); 963 } 964 965 /** 966 * <p>Validates that the specified argument array is neither {@code null} 967 * nor a length of zero (no elements); otherwise throwing an exception 968 * with the specified message. 969 * 970 * <pre>Validate.notEmpty(myArray, "The array must not be empty");</pre> 971 * 972 * @param <T> the array type. 973 * @param array the array to check, validated not null by this method. 974 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 975 * @param values the optional values for the formatted exception message, null array not recommended. 976 * @return the validated array (never {@code null} method for chaining). 977 * @throws NullPointerException if the array is {@code null} 978 * @throws IllegalArgumentException if the array is empty 979 * @see #notEmpty(Object[]) 980 */ 981 public static <T> T[] notEmpty(final T[] array, final String message, final Object... values) { 982 Objects.requireNonNull(array, toSupplier(message, values)); 983 if (array.length == 0) { 984 throw new IllegalArgumentException(getMessage(message, values)); 985 } 986 return array; 987 } 988 989 /** 990 * Validates that the specified argument is not Not-a-Number (NaN); otherwise 991 * throwing an exception. 992 * 993 * <pre>Validate.notNaN(myDouble);</pre> 994 * 995 * <p>The message of the exception is "The validated value is not a 996 * number".</p> 997 * 998 * @param value the value to validate. 999 * @throws IllegalArgumentException if the value is not a number. 1000 * @see #notNaN(double, String, Object...) 1001 * @since 3.5 1002 */ 1003 public static void notNaN(final double value) { 1004 notNaN(value, DEFAULT_NOT_NAN_EX_MESSAGE); 1005 } 1006 1007 /** 1008 * Validates that the specified argument is not Not-a-Number (NaN); otherwise 1009 * throwing an exception with the specified message. 1010 * 1011 * <pre>Validate.notNaN(myDouble, "The value must be a number");</pre> 1012 * 1013 * @param value the value to validate. 1014 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 1015 * @param values the optional values for the formatted exception message. 1016 * @throws IllegalArgumentException if the value is not a number. 1017 * @see #notNaN(double) 1018 * @since 3.5 1019 */ 1020 public static void notNaN(final double value, final String message, final Object... values) { 1021 if (Double.isNaN(value)) { 1022 throw new IllegalArgumentException(getMessage(message, values)); 1023 } 1024 } 1025 1026 /** 1027 * Validate that the specified argument is not {@code null}; 1028 * otherwise throwing an exception. 1029 * 1030 * <pre>Validate.notNull(myObject, "The object must not be null");</pre> 1031 * 1032 * <p>The message of the exception is "The validated object is 1033 * null". 1034 * 1035 * @param <T> the object type. 1036 * @param object the object to check. 1037 * @return the validated object (never {@code null} for method chaining). 1038 * @throws NullPointerException if the object is {@code null}. 1039 * @see #notNull(Object, String, Object...) 1040 * @deprecated Use {@link Objects#requireNonNull(Object)}. 1041 */ 1042 @Deprecated 1043 public static <T> T notNull(final T object) { 1044 return notNull(object, DEFAULT_IS_NULL_EX_MESSAGE); 1045 } 1046 1047 /** 1048 * Validate that the specified argument is not {@code null}; 1049 * otherwise throwing an exception with the specified message. 1050 * 1051 * <pre>Validate.notNull(myObject, "The object must not be null");</pre> 1052 * 1053 * @param <T> the object type. 1054 * @param object the object to check. 1055 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 1056 * @param values the optional values for the formatted exception message. 1057 * @return the validated object (never {@code null} for method chaining). 1058 * @throws NullPointerException if the object is {@code null}. 1059 * @see Objects#requireNonNull(Object) 1060 */ 1061 public static <T> T notNull(final T object, final String message, final Object... values) { 1062 return Objects.requireNonNull(object, toSupplier(message, values)); 1063 } 1064 1065 private static Supplier<String> toSupplier(final String message, final Object... values) { 1066 return () -> getMessage(message, values); 1067 } 1068 1069 /** 1070 * Validates that the index is within the bounds of the argument 1071 * collection; otherwise throwing an exception. 1072 * 1073 * <pre>Validate.validIndex(myCollection, 2);</pre> 1074 * 1075 * <p>If the index is invalid, then the message of the exception 1076 * is "The validated collection index is invalid: " 1077 * followed by the index.</p> 1078 * 1079 * @param <T> the collection type. 1080 * @param collection the collection to check, validated not null by this method. 1081 * @param index the index to check. 1082 * @return the validated collection (never {@code null} for method chaining). 1083 * @throws NullPointerException if the collection is {@code null}. 1084 * @throws IndexOutOfBoundsException if the index is invalid. 1085 * @see #validIndex(Collection, int, String, Object...) 1086 * @since 3.0 1087 */ 1088 public static <T extends Collection<?>> T validIndex(final T collection, final int index) { 1089 return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index)); 1090 } 1091 1092 /** 1093 * Validates that the index is within the bounds of the argument 1094 * character sequence; otherwise throwing an exception. 1095 * 1096 * <pre>Validate.validIndex(myStr, 2);</pre> 1097 * 1098 * <p>If the character sequence is {@code null}, then the message 1099 * of the exception is "The validated object is 1100 * null".</p> 1101 * 1102 * <p>If the index is invalid, then the message of the exception 1103 * is "The validated character sequence index is invalid: " 1104 * followed by the index.</p> 1105 * 1106 * @param <T> the character sequence type. 1107 * @param chars the character sequence to check, validated not null by this method. 1108 * @param index the index to check. 1109 * @return the validated character sequence (never {@code null} for method chaining). 1110 * @throws NullPointerException if the character sequence is {@code null}. 1111 * @throws IndexOutOfBoundsException if the index is invalid. 1112 * @see #validIndex(CharSequence, int, String, Object...) 1113 * @since 3.0 1114 */ 1115 public static <T extends CharSequence> T validIndex(final T chars, final int index) { 1116 return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index)); 1117 } 1118 1119 /** 1120 * Validates that the index is within the bounds of the argument 1121 * collection; otherwise throwing an exception with the specified message. 1122 * 1123 * <pre>Validate.validIndex(myCollection, 2, "The collection index is invalid: ");</pre> 1124 * 1125 * <p>If the collection is {@code null}, then the message of the 1126 * exception is "The validated object is null".</p> 1127 * 1128 * @param <T> the collection type. 1129 * @param collection the collection to check, validated not null by this method. 1130 * @param index the index to check. 1131 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 1132 * @param values the optional values for the formatted exception message, null array not recommended. 1133 * @return the validated collection (never {@code null} for chaining). 1134 * @throws NullPointerException if the collection is {@code null}. 1135 * @throws IndexOutOfBoundsException if the index is invalid. 1136 * @see #validIndex(Collection, int) 1137 * @since 3.0 1138 */ 1139 public static <T extends Collection<?>> T validIndex(final T collection, final int index, final String message, final Object... values) { 1140 Objects.requireNonNull(collection, "collection"); 1141 if (index < 0 || index >= collection.size()) { 1142 throw new IndexOutOfBoundsException(getMessage(message, values)); 1143 } 1144 return collection; 1145 } 1146 1147 /** 1148 * Validates that the index is within the bounds of the argument 1149 * character sequence; otherwise throwing an exception with the 1150 * specified message. 1151 * 1152 * <pre>Validate.validIndex(myStr, 2, "The string index is invalid: ");</pre> 1153 * 1154 * <p>If the character sequence is {@code null}, then the message 1155 * of the exception is "The validated object is null".</p> 1156 * 1157 * @param <T> the character sequence type. 1158 * @param chars the character sequence to check, validated not null by this method. 1159 * @param index the index to check. 1160 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 1161 * @param values the optional values for the formatted exception message, null array not recommended. 1162 * @return the validated character sequence (never {@code null} for method chaining). 1163 * @throws NullPointerException if the character sequence is {@code null}. 1164 * @throws IndexOutOfBoundsException if the index is invalid. 1165 * @see #validIndex(CharSequence, int) 1166 * @since 3.0 1167 */ 1168 public static <T extends CharSequence> T validIndex(final T chars, final int index, final String message, final Object... values) { 1169 Objects.requireNonNull(chars, "chars"); 1170 if (index < 0 || index >= chars.length()) { 1171 throw new IndexOutOfBoundsException(getMessage(message, values)); 1172 } 1173 return chars; 1174 } 1175 1176 /** 1177 * Validates that the index is within the bounds of the argument 1178 * array; otherwise throwing an exception. 1179 * 1180 * <pre>Validate.validIndex(myArray, 2);</pre> 1181 * 1182 * <p>If the array is {@code null}, then the message of the exception 1183 * is "The validated object is null".</p> 1184 * 1185 * <p>If the index is invalid, then the message of the exception is 1186 * "The validated array index is invalid: " followed by the 1187 * index.</p> 1188 * 1189 * @param <T> the array type. 1190 * @param array the array to check, validated not null by this method. 1191 * @param index the index to check. 1192 * @return the validated array (never {@code null} for method chaining). 1193 * @throws NullPointerException if the array is {@code null}. 1194 * @throws IndexOutOfBoundsException if the index is invalid. 1195 * @see #validIndex(Object[], int, String, Object...) 1196 * @since 3.0 1197 */ 1198 public static <T> T[] validIndex(final T[] array, final int index) { 1199 return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index)); 1200 } 1201 1202 /** 1203 * Validates that the index is within the bounds of the argument 1204 * array; otherwise throwing an exception with the specified message. 1205 * 1206 * <pre>Validate.validIndex(myArray, 2, "The array index is invalid: ");</pre> 1207 * 1208 * <p>If the array is {@code null}, then the message of the exception 1209 * is "The validated object is null".</p> 1210 * 1211 * @param <T> the array type. 1212 * @param array the array to check, validated not null by this method. 1213 * @param index the index to check. 1214 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 1215 * @param values the optional values for the formatted exception message, null array not recommended. 1216 * @return the validated array (never {@code null} for method chaining). 1217 * @throws NullPointerException if the array is {@code null}. 1218 * @throws IndexOutOfBoundsException if the index is invalid. 1219 * @see #validIndex(Object[], int) 1220 * @since 3.0 1221 */ 1222 public static <T> T[] validIndex(final T[] array, final int index, final String message, final Object... values) { 1223 Objects.requireNonNull(array, "array"); 1224 if (index < 0 || index >= array.length) { 1225 throw new IndexOutOfBoundsException(getMessage(message, values)); 1226 } 1227 return array; 1228 } 1229 1230 /** 1231 * Validate that the stateful condition is {@code true}; otherwise 1232 * throwing an exception. This method is useful when validating according 1233 * to an arbitrary boolean expression, such as validating a 1234 * primitive number or using your own custom validation expression. 1235 * 1236 * <pre> 1237 * Validate.validState(field > 0); 1238 * Validate.validState(this.isOk());</pre> 1239 * 1240 * <p>The message of the exception is "The validated state is 1241 * false".</p> 1242 * 1243 * @param expression the boolean expression to check. 1244 * @throws IllegalStateException if expression is {@code false}. 1245 * @see #validState(boolean, String, Object...) 1246 * @since 3.0 1247 */ 1248 public static void validState(final boolean expression) { 1249 if (!expression) { 1250 throw new IllegalStateException(DEFAULT_VALID_STATE_EX_MESSAGE); 1251 } 1252 } 1253 1254 /** 1255 * Validate that the stateful condition is {@code true}; otherwise 1256 * throwing an exception with the specified message. This method is useful when 1257 * validating according to an arbitrary boolean expression, such as validating a 1258 * primitive number or using your own custom validation expression. 1259 * 1260 * <pre>Validate.validState(this.isOk(), "The state is not OK: %s", myObject);</pre> 1261 * 1262 * @param expression the boolean expression to check. 1263 * @param message the {@link String#format(String, Object...)} exception message if invalid, not null. 1264 * @param values the optional values for the formatted exception message, null array not recommended. 1265 * @throws IllegalStateException if expression is {@code false}. 1266 * @see #validState(boolean) 1267 * @since 3.0 1268 */ 1269 public static void validState(final boolean expression, final String message, final Object... values) { 1270 if (!expression) { 1271 throw new IllegalStateException(getMessage(message, values)); 1272 } 1273 } 1274 1275 /** 1276 * Constructs a new instance. This class should not normally be instantiated. 1277 */ 1278 public Validate() { 1279 } 1280}