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