001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.util; 017 018import java.util.Collection; 019import java.util.Iterator; 020import java.util.Map; 021import org.modeshape.common.CommonI18n; 022import org.modeshape.common.annotation.Immutable; 023 024/** 025 * Utility class that checks arguments to methods. This class is to be used only in API methods, where failure to supply correct 026 * arguments should result in a useful error message. In all cases, use the <code>assert</code> statement. 027 */ 028@Immutable 029public final class CheckArg { 030 031 // ########################## int METHODS ################################### 032 033 /** 034 * Check that the argument is not less than the supplied value 035 * 036 * @param argument The argument 037 * @param notLessThanValue the value that is to be used to check the value 038 * @param name The name of the argument 039 * @throws IllegalArgumentException If argument greater than or equal to the supplied vlaue 040 */ 041 public static void isNotLessThan( int argument, 042 int notLessThanValue, 043 String name ) { 044 if (argument < notLessThanValue) { 045 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeLessThan.text(name, argument, notLessThanValue)); 046 } 047 } 048 049 /** 050 * Check that the argument is not greater than the supplied value 051 * 052 * @param argument The argument 053 * @param notGreaterThanValue the value that is to be used to check the value 054 * @param name The name of the argument 055 * @throws IllegalArgumentException If argument is less than or equal to the supplied value 056 */ 057 public static void isNotGreaterThan( int argument, 058 int notGreaterThanValue, 059 String name ) { 060 if (argument > notGreaterThanValue) { 061 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeGreaterThan.text(name, argument, notGreaterThanValue)); 062 } 063 } 064 065 /** 066 * Check that the argument is greater than the supplied value 067 * 068 * @param argument The argument 069 * @param greaterThanValue the value that is to be used to check the value 070 * @param name The name of the argument 071 * @throws IllegalArgumentException If argument is not greater than the supplied value 072 */ 073 public static void isGreaterThan( int argument, 074 int greaterThanValue, 075 String name ) { 076 if (argument <= greaterThanValue) { 077 throw new IllegalArgumentException(CommonI18n.argumentMustBeGreaterThan.text(name, argument, greaterThanValue)); 078 } 079 } 080 081 /** 082 * Check that the argument is greater than the supplied value 083 * 084 * @param argument The argument 085 * @param greaterThanValue the value that is to be used to check the value 086 * @param name The name of the argument 087 * @throws IllegalArgumentException If argument is not greater than the supplied value 088 */ 089 public static void isGreaterThan( double argument, 090 double greaterThanValue, 091 String name ) { 092 if (argument <= greaterThanValue) { 093 throw new IllegalArgumentException(CommonI18n.argumentMustBeGreaterThan.text(name, argument, greaterThanValue)); 094 } 095 } 096 097 /** 098 * Check that the argument is less than the supplied value 099 * 100 * @param argument The argument 101 * @param lessThanValue the value that is to be used to check the value 102 * @param name The name of the argument 103 * @throws IllegalArgumentException If argument is not less than the supplied value 104 */ 105 public static void isLessThan( int argument, 106 int lessThanValue, 107 String name ) { 108 if (argument >= lessThanValue) { 109 throw new IllegalArgumentException(CommonI18n.argumentMustBeLessThan.text(name, argument, lessThanValue)); 110 } 111 } 112 113 /** 114 * Check that the argument is greater than or equal to the supplied value 115 * 116 * @param argument The argument 117 * @param greaterThanOrEqualToValue the value that is to be used to check the value 118 * @param name The name of the argument 119 * @throws IllegalArgumentException If argument is not greater than or equal to the supplied value 120 */ 121 public static void isGreaterThanOrEqualTo( int argument, 122 int greaterThanOrEqualToValue, 123 String name ) { 124 if (argument < greaterThanOrEqualToValue) { 125 throw new IllegalArgumentException(CommonI18n.argumentMustBeGreaterThanOrEqualTo.text(name, argument, 126 greaterThanOrEqualToValue)); 127 } 128 } 129 130 /** 131 * Check that the argument is less than or equal to the supplied value 132 * 133 * @param argument The argument 134 * @param lessThanOrEqualToValue the value that is to be used to check the value 135 * @param name The name of the argument 136 * @throws IllegalArgumentException If argument is not less than or equal to the supplied value 137 */ 138 public static void isLessThanOrEqualTo( int argument, 139 int lessThanOrEqualToValue, 140 String name ) { 141 if (argument > lessThanOrEqualToValue) { 142 throw new IllegalArgumentException(CommonI18n.argumentMustBeLessThanOrEqualTo.text(name, argument, 143 lessThanOrEqualToValue)); 144 } 145 } 146 147 /** 148 * Check that the argument is non-negative (>=0). 149 * 150 * @param argument The argument 151 * @param name The name of the argument 152 * @throws IllegalArgumentException If argument is negative (<0) 153 */ 154 public static void isNonNegative( int argument, 155 String name ) { 156 if (argument < 0) { 157 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNegative.text(name, argument)); 158 } 159 } 160 161 /** 162 * Check that the argument is non-positive (<=0). 163 * 164 * @param argument The argument 165 * @param name The name of the argument 166 * @throws IllegalArgumentException If argument is positive (>0) 167 */ 168 public static void isNonPositive( int argument, 169 String name ) { 170 if (argument > 0) { 171 throw new IllegalArgumentException(CommonI18n.argumentMayNotBePositive.text(name, argument)); 172 } 173 } 174 175 /** 176 * Check that the argument is negative (<0). 177 * 178 * @param argument The argument 179 * @param name The name of the argument 180 * @throws IllegalArgumentException If argument is non-negative (>=0) 181 */ 182 public static void isNegative( int argument, 183 String name ) { 184 if (argument >= 0) { 185 throw new IllegalArgumentException(CommonI18n.argumentMustBeNegative.text(name, argument)); 186 } 187 } 188 189 /** 190 * Check that the argument is positive (>0). 191 * 192 * @param argument The argument 193 * @param name The name of the argument 194 * @throws IllegalArgumentException If argument is non-positive (<=0) 195 */ 196 public static void isPositive( int argument, 197 String name ) { 198 if (argument <= 0) { 199 throw new IllegalArgumentException(CommonI18n.argumentMustBePositive.text(name, argument)); 200 } 201 } 202 203 /** 204 * Check that the argument is a power of 2. 205 * 206 * @param argument The argument 207 * @param name The name of the argument 208 * @throws IllegalArgumentException If argument is not a power of 2 209 */ 210 public static void isPowerOfTwo( int argument, 211 String name ) { 212 if (Integer.bitCount(argument) != 1) { 213 throw new IllegalArgumentException(CommonI18n.argumentMustBePowerOfTwo.text(name, argument)); 214 } 215 } 216 217 // ########################## long METHODS ################################### 218 219 /** 220 * Check that the argument is non-negative (>=0). 221 * 222 * @param argument The argument 223 * @param name The name of the argument 224 * @throws IllegalArgumentException If argument is negative (<0) 225 */ 226 public static void isNonNegative( long argument, 227 String name ) { 228 if (argument < 0) { 229 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNegative.text(name, argument)); 230 } 231 } 232 233 /** 234 * Check that the argument is non-positive (<=0). 235 * 236 * @param argument The argument 237 * @param name The name of the argument 238 * @throws IllegalArgumentException If argument is positive (>0) 239 */ 240 public static void isNonPositive( long argument, 241 String name ) { 242 if (argument > 0) { 243 throw new IllegalArgumentException(CommonI18n.argumentMayNotBePositive.text(name, argument)); 244 } 245 } 246 247 /** 248 * Check that the argument is negative (<0). 249 * 250 * @param argument The argument 251 * @param name The name of the argument 252 * @throws IllegalArgumentException If argument is non-negative (>=0) 253 */ 254 public static void isNegative( long argument, 255 String name ) { 256 if (argument >= 0) { 257 throw new IllegalArgumentException(CommonI18n.argumentMustBeNegative.text(name, argument)); 258 } 259 } 260 261 /** 262 * Check that the argument is positive (>0). 263 * 264 * @param argument The argument 265 * @param name The name of the argument 266 * @throws IllegalArgumentException If argument is non-positive (<=0) 267 */ 268 public static void isPositive( long argument, 269 String name ) { 270 if (argument <= 0) { 271 throw new IllegalArgumentException(CommonI18n.argumentMustBePositive.text(name, argument)); 272 } 273 } 274 275 // ########################## double METHODS ################################### 276 277 /** 278 * Check that the argument is non-negative (>=0). 279 * 280 * @param argument The argument 281 * @param name The name of the argument 282 * @throws IllegalArgumentException If argument is negative (<0) 283 */ 284 public static void isNonNegative( double argument, 285 String name ) { 286 if (argument < 0.0) { 287 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNegative.text(name, argument)); 288 } 289 } 290 291 /** 292 * Check that the argument is non-positive (<=0). 293 * 294 * @param argument The argument 295 * @param name The name of the argument 296 * @throws IllegalArgumentException If argument is positive (>0) 297 */ 298 public static void isNonPositive( double argument, 299 String name ) { 300 if (argument > 0.0) { 301 throw new IllegalArgumentException(CommonI18n.argumentMayNotBePositive.text(name, argument)); 302 } 303 } 304 305 /** 306 * Check that the argument is negative (<0). 307 * 308 * @param argument The argument 309 * @param name The name of the argument 310 * @throws IllegalArgumentException If argument is non-negative (>=0) 311 */ 312 public static void isNegative( double argument, 313 String name ) { 314 if (argument >= 0.0) { 315 throw new IllegalArgumentException(CommonI18n.argumentMustBeNegative.text(name, argument)); 316 } 317 } 318 319 /** 320 * Check that the argument is positive (>0). 321 * 322 * @param argument The argument 323 * @param name The name of the argument 324 * @throws IllegalArgumentException If argument is non-positive (<=0) 325 */ 326 public static void isPositive( double argument, 327 String name ) { 328 if (argument <= 0.0) { 329 throw new IllegalArgumentException(CommonI18n.argumentMustBePositive.text(name, argument)); 330 } 331 } 332 333 /** 334 * Check that the argument is not NaN. 335 * 336 * @param argument The argument 337 * @param name The name of the argument 338 * @throws IllegalArgumentException If argument is NaN 339 */ 340 public static void isNotNan( double argument, 341 String name ) { 342 if (Double.isNaN(argument)) { 343 throw new IllegalArgumentException(CommonI18n.argumentMustBeNumber.text(name)); 344 } 345 } 346 347 // ########################## String METHODS ################################### 348 349 /** 350 * Check that the string is non-null and has length > 0 351 * 352 * @param argument The argument 353 * @param name The name of the argument 354 * @throws IllegalArgumentException If value is null or length == 0 355 */ 356 public static void isNotZeroLength( String argument, 357 String name ) { 358 isNotNull(argument, name); 359 if (argument.length() <= 0) { 360 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNullOrZeroLength.text(name)); 361 } 362 } 363 364 /** 365 * Check that the string is not empty, is not null, and does not contain only whitespace. 366 * 367 * @param argument String 368 * @param name The name of the argument 369 * @throws IllegalArgumentException If string is null or empty 370 */ 371 public static void isNotEmpty( String argument, 372 String name ) { 373 isNotZeroLength(argument, name); 374 if (argument != null && argument.trim().length() == 0) { 375 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNullOrZeroLengthOrEmpty.text(name)); 376 } 377 } 378 379 // ########################## Object METHODS ################################### 380 381 /** 382 * Check that the specified argument is non-null 383 * 384 * @param argument The argument 385 * @param name The name of the argument 386 * @throws IllegalArgumentException If argument is null 387 */ 388 public static void isNotNull( Object argument, 389 String name ) { 390 if (argument == null) { 391 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNull.text(name)); 392 } 393 } 394 395 /** 396 * Returns the specified argument if it is not <code>null</code>. 397 * 398 * @param <T> 399 * @param argument The argument 400 * @param name The name of the argument 401 * @return The argument 402 * @throws IllegalArgumentException If argument is <code>null</code> 403 */ 404 public static <T> T getNotNull( T argument, 405 String name ) { 406 isNotNull(argument, name); 407 return argument; 408 } 409 410 /** 411 * Check that the argument is null 412 * 413 * @param argument The argument 414 * @param name The name of the argument 415 * @throws IllegalArgumentException If value is non-null 416 */ 417 public static void isNull( Object argument, 418 String name ) { 419 if (argument != null) { 420 throw new IllegalArgumentException(CommonI18n.argumentMustBeNull.text(name)); 421 } 422 } 423 424 /** 425 * Check that the object is an instance of the specified Class 426 * 427 * @param argument Value 428 * @param expectedClass Class 429 * @param name The name of the argument 430 * @throws IllegalArgumentException If value is null 431 */ 432 public static void isInstanceOf( Object argument, 433 Class<?> expectedClass, 434 String name ) { 435 isNotNull(argument, name); 436 if (!expectedClass.isInstance(argument)) { 437 throw new IllegalArgumentException(CommonI18n.argumentMustBeInstanceOf.text(name, argument.getClass(), 438 expectedClass.getName())); 439 } 440 } 441 442 /** 443 * Checks that the object is an instance of the specified Class and then returns the object cast to the specified Class 444 * 445 * @param <C> the class type 446 * @param argument Value 447 * @param expectedClass Class 448 * @param name The name of the argument 449 * @return value cast to the specified Class 450 * @throws IllegalArgumentException If value is not an instance of theClass. 451 */ 452 // due to cast in return 453 public static <C> C getInstanceOf( Object argument, 454 Class<C> expectedClass, 455 String name ) { 456 isInstanceOf(argument, expectedClass, name); 457 return expectedClass.cast(argument); 458 } 459 460 /** 461 * Asserts that the specified first object is the same as (==) the specified second object. 462 * 463 * @param <T> 464 * @param argument The argument to assert as the same as <code>object</code>. 465 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown 466 * @param object The object to assert as the same as <code>argument</code>. 467 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be 468 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will 469 * be used. 470 * @throws IllegalArgumentException If the specified objects are not the same. 471 */ 472 public static <T> void isSame( final T argument, 473 String argumentName, 474 final T object, 475 String objectName ) { 476 if (argument != object) { 477 if (objectName == null) objectName = getObjectName(object); 478 throw new IllegalArgumentException(CommonI18n.argumentMustBeSameAs.text(argumentName, objectName)); 479 } 480 } 481 482 /** 483 * Asserts that the specified first object is not the same as (==) the specified second object. 484 * 485 * @param <T> 486 * @param argument The argument to assert as not the same as <code>object</code>. 487 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown 488 * @param object The object to assert as not the same as <code>argument</code>. 489 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be 490 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will 491 * be used. 492 * @throws IllegalArgumentException If the specified objects are the same. 493 */ 494 public static <T> void isNotSame( final T argument, 495 String argumentName, 496 final T object, 497 String objectName ) { 498 if (argument == object) { 499 if (objectName == null) objectName = getObjectName(object); 500 throw new IllegalArgumentException(CommonI18n.argumentMustNotBeSameAs.text(argumentName, objectName)); 501 } 502 } 503 504 /** 505 * Asserts that the specified first object is {@link Object#equals(Object) equal to} the specified second object. This method 506 * does take null references into consideration. 507 * 508 * @param <T> 509 * @param argument The argument to assert equal to <code>object</code>. 510 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown 511 * @param object The object to assert as equal to <code>argument</code>. 512 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be 513 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will 514 * be used. 515 * @throws IllegalArgumentException If the specified objects are not equal. 516 */ 517 public static <T> void isEquals( final T argument, 518 String argumentName, 519 final T object, 520 String objectName ) { 521 if (argument == null) { 522 if (object == null) return; 523 // fall through ... one is null 524 } else { 525 if (argument.equals(object)) return; 526 // fall through ... they are not equal 527 } 528 if (objectName == null) objectName = getObjectName(object); 529 throw new IllegalArgumentException(CommonI18n.argumentMustBeEquals.text(argumentName, objectName)); 530 } 531 532 /** 533 * Asserts that the specified first object is not {@link Object#equals(Object) equal to} the specified second object. This 534 * method does take null references into consideration. 535 * 536 * @param <T> 537 * @param argument The argument to assert equal to <code>object</code>. 538 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown 539 * @param object The object to assert as equal to <code>argument</code>. 540 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be 541 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will 542 * be used. 543 * @throws IllegalArgumentException If the specified objects are equals. 544 */ 545 public static <T> void isNotEquals( final T argument, 546 String argumentName, 547 final T object, 548 String objectName ) { 549 if (argument == null) { 550 if (object != null) return; 551 // fall through ... both are null 552 } else { 553 if (!argument.equals(object)) return; // handles object==null 554 // fall through ... they are equal 555 } 556 if (objectName == null) objectName = getObjectName(object); 557 throw new IllegalArgumentException(CommonI18n.argumentMustNotBeEquals.text(argumentName, objectName)); 558 } 559 560 // ########################## ITERATOR METHODS ################################### 561 562 /** 563 * Checks that the iterator is not empty, and throws an exception if it is. 564 * 565 * @param argument the iterator to check 566 * @param name The name of the argument 567 * @throws IllegalArgumentException If iterator is empty (i.e., iterator.hasNext() returns false) 568 */ 569 public static void isNotEmpty( Iterator<?> argument, 570 String name ) { 571 isNotNull(argument, name); 572 if (!argument.hasNext()) { 573 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name)); 574 } 575 } 576 577 // ########################## COLLECTION METHODS ################################### 578 579 /** 580 * Check that the collection is not empty 581 * 582 * @param argument Collection 583 * @param name The name of the argument 584 * @throws IllegalArgumentException If collection is null or empty 585 */ 586 public static void isNotEmpty( Collection<?> argument, 587 String name ) { 588 isNotNull(argument, name); 589 if (argument.isEmpty()) { 590 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name)); 591 } 592 } 593 594 /** 595 * Check that the map is not empty 596 * 597 * @param argument Map 598 * @param name The name of the argument 599 * @throws IllegalArgumentException If map is null or empty 600 */ 601 public static void isNotEmpty( Map<?, ?> argument, 602 String name ) { 603 isNotNull(argument, name); 604 if (argument.isEmpty()) { 605 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name)); 606 } 607 } 608 609 /** 610 * Check that the array is empty 611 * 612 * @param argument Array 613 * @param name The name of the argument 614 * @throws IllegalArgumentException If array is not empty 615 */ 616 public static void isEmpty( Object[] argument, 617 String name ) { 618 isNotNull(argument, name); 619 if (argument.length > 0) { 620 throw new IllegalArgumentException(CommonI18n.argumentMustBeEmpty.text(name)); 621 } 622 } 623 624 /** 625 * Check that the array is not empty 626 * 627 * @param argument Array 628 * @param name The name of the argument 629 * @throws IllegalArgumentException If array is null or empty 630 */ 631 public static void isNotEmpty( Object[] argument, 632 String name ) { 633 isNotNull(argument, name); 634 if (argument.length == 0) { 635 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name)); 636 } 637 } 638 639 protected static String getObjectName( Object obj ) { 640 return obj == null ? null : "'" + obj.toString() + "'"; 641 } 642 643 /** 644 * Check that the collection contains the value 645 * 646 * @param argument Collection to check 647 * @param value Value to check for, may be null 648 * @param name The name of the argument 649 * @throws IllegalArgumentException If collection is null or doesn't contain value 650 */ 651 public static void contains( Collection<?> argument, 652 Object value, 653 String name ) { 654 isNotNull(argument, name); 655 if (!argument.contains(value)) { 656 throw new IllegalArgumentException(CommonI18n.argumentDidNotContainObject.text(name, getObjectName(value))); 657 } 658 } 659 660 /** 661 * Check that the map contains the key 662 * 663 * @param argument Map to check 664 * @param key Key to check for, may be null 665 * @param name The name of the argument 666 * @throws IllegalArgumentException If map is null or doesn't contain key 667 */ 668 public static void containsKey( Map<?, ?> argument, 669 Object key, 670 String name ) { 671 isNotNull(argument, name); 672 if (!argument.containsKey(key)) { 673 throw new IllegalArgumentException(CommonI18n.argumentDidNotContainKey.text(name, getObjectName(key))); 674 } 675 } 676 677 /** 678 * Check that the collection is not null and contains no nulls 679 * 680 * @param argument Array 681 * @param name The name of the argument 682 * @throws IllegalArgumentException If array is null or has null values 683 */ 684 public static void containsNoNulls( Iterable<?> argument, 685 String name ) { 686 isNotNull(argument, name); 687 int i = 0; 688 for (Object object : argument) { 689 if (object == null) { 690 throw new IllegalArgumentException(CommonI18n.argumentMayNotContainNullValue.text(name, i)); 691 } 692 ++i; 693 } 694 } 695 696 /** 697 * Check that the array is not null and contains no nulls 698 * 699 * @param argument Array 700 * @param name The name of the argument 701 * @throws IllegalArgumentException If array is null or has null values 702 */ 703 public static void containsNoNulls( Object[] argument, 704 String name ) { 705 isNotNull(argument, name); 706 int i = 0; 707 for (Object object : argument) { 708 if (object == null) { 709 throw new IllegalArgumentException(CommonI18n.argumentMayNotContainNullValue.text(name, i)); 710 } 711 ++i; 712 } 713 } 714 715 /** 716 * Check that the collection contains at least the supplied number of elements 717 * 718 * @param argument Collection 719 * @param minimumSize the minimum size 720 * @param name The name of the argument 721 * @throws IllegalArgumentException If collection has a size smaller than the supplied value 722 */ 723 public static void hasSizeOfAtLeast( Collection<?> argument, 724 int minimumSize, 725 String name ) { 726 isNotNull(argument, name); 727 if (argument.size() < minimumSize) { 728 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name, 729 Collection.class.getSimpleName(), 730 argument.size(), minimumSize)); 731 } 732 } 733 734 /** 735 * Check that the collection contains no more than the supplied number of elements 736 * 737 * @param argument Collection 738 * @param maximumSize the maximum size 739 * @param name The name of the argument 740 * @throws IllegalArgumentException If collection has a size smaller than the supplied value 741 */ 742 public static void hasSizeOfAtMost( Collection<?> argument, 743 int maximumSize, 744 String name ) { 745 isNotNull(argument, name); 746 if (argument.size() > maximumSize) { 747 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name, 748 Collection.class.getSimpleName(), 749 argument.size(), maximumSize)); 750 } 751 } 752 753 /** 754 * Check that the map contains at least the supplied number of entries 755 * 756 * @param argument the map 757 * @param minimumSize the minimum size 758 * @param name The name of the argument 759 * @throws IllegalArgumentException If the map has a size smaller than the supplied value 760 */ 761 public static void hasSizeOfAtLeast( Map<?, ?> argument, 762 int minimumSize, 763 String name ) { 764 isNotNull(argument, name); 765 if (argument.size() < minimumSize) { 766 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name, Map.class.getSimpleName(), 767 argument.size(), minimumSize)); 768 } 769 } 770 771 /** 772 * Check that the map contains no more than the supplied number of entries 773 * 774 * @param argument the map 775 * @param maximumSize the maximum size 776 * @param name The name of the argument 777 * @throws IllegalArgumentException If the map has a size smaller than the supplied value 778 */ 779 public static void hasSizeOfAtMost( Map<?, ?> argument, 780 int maximumSize, 781 String name ) { 782 isNotNull(argument, name); 783 if (argument.size() > maximumSize) { 784 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name, Map.class.getSimpleName(), 785 argument.size(), maximumSize)); 786 } 787 } 788 789 /** 790 * Check that the array contains at least the supplied number of elements 791 * 792 * @param argument the array 793 * @param minimumSize the minimum size 794 * @param name The name of the argument 795 * @throws IllegalArgumentException If the array has a size smaller than the supplied value 796 */ 797 public static void hasSizeOfAtLeast( Object[] argument, 798 int minimumSize, 799 String name ) { 800 isNotNull(argument, name); 801 if (argument.length < minimumSize) { 802 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name, Object[].class.getSimpleName(), 803 argument.length, minimumSize)); 804 } 805 } 806 807 /** 808 * Check that the array contains no more than the supplied number of elements 809 * 810 * @param argument the array 811 * @param maximumSize the maximum size 812 * @param name The name of the argument 813 * @throws IllegalArgumentException If the array has a size smaller than the supplied value 814 */ 815 public static void hasSizeOfAtMost( Object[] argument, 816 int maximumSize, 817 String name ) { 818 isNotNull(argument, name); 819 if (argument.length > maximumSize) { 820 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name, Object[].class.getSimpleName(), 821 argument.length, maximumSize)); 822 } 823 } 824 825 private CheckArg() { 826 // prevent construction 827 } 828}