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 less than the supplied value
087 *
088 * @param argument The argument
089 * @param lessThanValue 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 less than the supplied value
092 */
093 public static void isLessThan( int argument,
094 int lessThanValue,
095 String name ) {
096 if (argument >= lessThanValue) {
097 throw new IllegalArgumentException(CommonI18n.argumentMustBeLessThan.text(name, argument, lessThanValue));
098 }
099 }
100
101 /**
102 * Check that the argument is greater than or equal to the supplied value
103 *
104 * @param argument The argument
105 * @param greaterThanOrEqualToValue 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 greater than or equal to the supplied value
108 */
109 public static void isGreaterThanOrEqualTo( int argument,
110 int greaterThanOrEqualToValue,
111 String name ) {
112 if (argument < greaterThanOrEqualToValue) {
113 throw new IllegalArgumentException(CommonI18n.argumentMustBeGreaterThanOrEqualTo.text(name,
114 argument,
115 greaterThanOrEqualToValue));
116 }
117 }
118
119 /**
120 * Check that the argument is less than or equal to the supplied value
121 *
122 * @param argument The argument
123 * @param lessThanOrEqualToValue the value that is to be used to check the value
124 * @param name The name of the argument
125 * @throws IllegalArgumentException If argument is not less than or equal to the supplied value
126 */
127 public static void isLessThanOrEqualTo( int argument,
128 int lessThanOrEqualToValue,
129 String name ) {
130 if (argument > lessThanOrEqualToValue) {
131 throw new IllegalArgumentException(CommonI18n.argumentMustBeLessThanOrEqualTo.text(name,
132 argument,
133 lessThanOrEqualToValue));
134 }
135 }
136
137 /**
138 * Check that the argument is non-negative (>=0).
139 *
140 * @param argument The argument
141 * @param name The name of the argument
142 * @throws IllegalArgumentException If argument is negative (<0)
143 */
144 public static void isNonNegative( int argument,
145 String name ) {
146 if (argument < 0) {
147 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNegative.text(name, argument));
148 }
149 }
150
151 /**
152 * Check that the argument is non-positive (<=0).
153 *
154 * @param argument The argument
155 * @param name The name of the argument
156 * @throws IllegalArgumentException If argument is positive (>0)
157 */
158 public static void isNonPositive( int argument,
159 String name ) {
160 if (argument > 0) {
161 throw new IllegalArgumentException(CommonI18n.argumentMayNotBePositive.text(name, argument));
162 }
163 }
164
165 /**
166 * Check that the argument is negative (<0).
167 *
168 * @param argument The argument
169 * @param name The name of the argument
170 * @throws IllegalArgumentException If argument is non-negative (>=0)
171 */
172 public static void isNegative( int argument,
173 String name ) {
174 if (argument >= 0) {
175 throw new IllegalArgumentException(CommonI18n.argumentMustBeNegative.text(name, argument));
176 }
177 }
178
179 /**
180 * Check that the argument is positive (>0).
181 *
182 * @param argument The argument
183 * @param name The name of the argument
184 * @throws IllegalArgumentException If argument is non-positive (<=0)
185 */
186 public static void isPositive( int argument,
187 String name ) {
188 if (argument <= 0) {
189 throw new IllegalArgumentException(CommonI18n.argumentMustBePositive.text(name, argument));
190 }
191 }
192
193 // ########################## long METHODS ###################################
194
195 /**
196 * Check that the argument is non-negative (>=0).
197 *
198 * @param argument The argument
199 * @param name The name of the argument
200 * @throws IllegalArgumentException If argument is negative (<0)
201 */
202 public static void isNonNegative( long argument,
203 String name ) {
204 if (argument < 0) {
205 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNegative.text(name, argument));
206 }
207 }
208
209 /**
210 * Check that the argument is non-positive (<=0).
211 *
212 * @param argument The argument
213 * @param name The name of the argument
214 * @throws IllegalArgumentException If argument is positive (>0)
215 */
216 public static void isNonPositive( long argument,
217 String name ) {
218 if (argument > 0) {
219 throw new IllegalArgumentException(CommonI18n.argumentMayNotBePositive.text(name, argument));
220 }
221 }
222
223 /**
224 * Check that the argument is negative (<0).
225 *
226 * @param argument The argument
227 * @param name The name of the argument
228 * @throws IllegalArgumentException If argument is non-negative (>=0)
229 */
230 public static void isNegative( long argument,
231 String name ) {
232 if (argument >= 0) {
233 throw new IllegalArgumentException(CommonI18n.argumentMustBeNegative.text(name, argument));
234 }
235 }
236
237 /**
238 * Check that the argument is positive (>0).
239 *
240 * @param argument The argument
241 * @param name The name of the argument
242 * @throws IllegalArgumentException If argument is non-positive (<=0)
243 */
244 public static void isPositive( long argument,
245 String name ) {
246 if (argument <= 0) {
247 throw new IllegalArgumentException(CommonI18n.argumentMustBePositive.text(name, argument));
248 }
249 }
250
251 // ########################## double METHODS ###################################
252
253 /**
254 * Check that the argument is non-negative (>=0).
255 *
256 * @param argument The argument
257 * @param name The name of the argument
258 * @throws IllegalArgumentException If argument is negative (<0)
259 */
260 public static void isNonNegative( double argument,
261 String name ) {
262 if (argument < 0.0) {
263 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNegative.text(name, argument));
264 }
265 }
266
267 /**
268 * Check that the argument is non-positive (<=0).
269 *
270 * @param argument The argument
271 * @param name The name of the argument
272 * @throws IllegalArgumentException If argument is positive (>0)
273 */
274 public static void isNonPositive( double argument,
275 String name ) {
276 if (argument > 0.0) {
277 throw new IllegalArgumentException(CommonI18n.argumentMayNotBePositive.text(name, argument));
278 }
279 }
280
281 /**
282 * Check that the argument is negative (<0).
283 *
284 * @param argument The argument
285 * @param name The name of the argument
286 * @throws IllegalArgumentException If argument is non-negative (>=0)
287 */
288 public static void isNegative( double argument,
289 String name ) {
290 if (argument >= 0.0) {
291 throw new IllegalArgumentException(CommonI18n.argumentMustBeNegative.text(name, argument));
292 }
293 }
294
295 /**
296 * Check that the argument is positive (>0).
297 *
298 * @param argument The argument
299 * @param name The name of the argument
300 * @throws IllegalArgumentException If argument is non-positive (<=0)
301 */
302 public static void isPositive( double argument,
303 String name ) {
304 if (argument <= 0.0) {
305 throw new IllegalArgumentException(CommonI18n.argumentMustBePositive.text(name, argument));
306 }
307 }
308
309 /**
310 * Check that the argument is not NaN.
311 *
312 * @param argument The argument
313 * @param name The name of the argument
314 * @throws IllegalArgumentException If argument is NaN
315 */
316 public static void isNotNan( double argument,
317 String name ) {
318 if (Double.isNaN(argument)) {
319 throw new IllegalArgumentException(CommonI18n.argumentMustBeNumber.text(name));
320 }
321 }
322
323 // ########################## String METHODS ###################################
324
325 /**
326 * Check that the string is non-null and has length > 0
327 *
328 * @param argument The argument
329 * @param name The name of the argument
330 * @throws IllegalArgumentException If value is null or length == 0
331 */
332 public static void isNotZeroLength( String argument,
333 String name ) {
334 isNotNull(argument, name);
335 if (argument.length() <= 0) {
336 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNullOrZeroLength.text(name));
337 }
338 }
339
340 /**
341 * Check that the string is not empty, is not null, and does not contain only whitespace.
342 *
343 * @param argument String
344 * @param name The name of the argument
345 * @throws IllegalArgumentException If string is null or empty
346 */
347 public static void isNotEmpty( String argument,
348 String name ) {
349 isNotZeroLength(argument, name);
350 if (argument != null && argument.trim().length() == 0) {
351 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNullOrZeroLengthOrEmpty.text(name));
352 }
353 }
354
355 // ########################## Object METHODS ###################################
356
357 /**
358 * Check that the specified argument is non-null
359 *
360 * @param argument The argument
361 * @param name The name of the argument
362 * @throws IllegalArgumentException If argument is null
363 */
364 public static void isNotNull( Object argument,
365 String name ) {
366 if (argument == null) {
367 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeNull.text(name));
368 }
369 }
370
371 /**
372 * Returns the specified argument if it is not <code>null</code>.
373 *
374 * @param <T>
375 * @param argument The argument
376 * @param name The name of the argument
377 * @return The argument
378 * @throws IllegalArgumentException If argument is <code>null</code>
379 */
380 public static <T> T getNotNull( T argument,
381 String name ) {
382 isNotNull(argument, name);
383 return argument;
384 }
385
386 /**
387 * Check that the argument is null
388 *
389 * @param argument The argument
390 * @param name The name of the argument
391 * @throws IllegalArgumentException If value is non-null
392 */
393 public static void isNull( Object argument,
394 String name ) {
395 if (argument != null) {
396 throw new IllegalArgumentException(CommonI18n.argumentMustBeNull.text(name));
397 }
398 }
399
400 /**
401 * Check that the object is an instance of the specified Class
402 *
403 * @param argument Value
404 * @param expectedClass Class
405 * @param name The name of the argument
406 * @throws IllegalArgumentException If value is null
407 */
408 public static void isInstanceOf( Object argument,
409 Class<?> expectedClass,
410 String name ) {
411 isNotNull(argument, name);
412 if (!expectedClass.isInstance(argument)) {
413 throw new IllegalArgumentException(CommonI18n.argumentMustBeInstanceOf.text(name,
414 argument.getClass(),
415 expectedClass.getName()));
416 }
417 }
418
419 /**
420 * Checks that the object is an instance of the specified Class and then returns the object cast to the specified Class
421 *
422 * @param <C> the class type
423 * @param argument Value
424 * @param expectedClass Class
425 * @param name The name of the argument
426 * @return value cast to the specified Class
427 * @throws IllegalArgumentException If value is not an instance of theClass.
428 */
429 // due to cast in return
430 public static <C> C getInstanceOf( Object argument,
431 Class<C> expectedClass,
432 String name ) {
433 isInstanceOf(argument, expectedClass, name);
434 return expectedClass.cast(argument);
435 }
436
437 /**
438 * Asserts that the specified first object is the same as (==) the specified second object.
439 *
440 * @param <T>
441 * @param argument The argument to assert as the same as <code>object</code>.
442 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown
443 * @param object The object to assert as the same as <code>argument</code>.
444 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be
445 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will
446 * be used.
447 * @throws IllegalArgumentException If the specified objects are not the same.
448 */
449 public static <T> void isSame( final T argument,
450 String argumentName,
451 final T object,
452 String objectName ) {
453 if (argument != object) {
454 if (objectName == null) objectName = getObjectName(object);
455 throw new IllegalArgumentException(CommonI18n.argumentMustBeSameAs.text(argumentName, objectName));
456 }
457 }
458
459 /**
460 * Asserts that the specified first object is not the same as (==) the specified second object.
461 *
462 * @param <T>
463 * @param argument The argument to assert as not the same as <code>object</code>.
464 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown
465 * @param object The object to assert as not the same as <code>argument</code>.
466 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be
467 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will
468 * be used.
469 * @throws IllegalArgumentException If the specified objects are the same.
470 */
471 public static <T> void isNotSame( final T argument,
472 String argumentName,
473 final T object,
474 String objectName ) {
475 if (argument == object) {
476 if (objectName == null) objectName = getObjectName(object);
477 throw new IllegalArgumentException(CommonI18n.argumentMustNotBeSameAs.text(argumentName, objectName));
478 }
479 }
480
481 /**
482 * Asserts that the specified first object is {@link Object#equals(Object) equal to} the specified second object.
483 *
484 * @param <T>
485 * @param argument The argument to assert equal to <code>object</code>.
486 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown
487 * @param object The object to assert as equal to <code>argument</code>.
488 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be
489 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will
490 * be used.
491 * @throws IllegalArgumentException If the specified objects are not equal.
492 */
493 public static <T> void isEquals( final T argument,
494 String argumentName,
495 final T object,
496 String objectName ) {
497 if (!argument.equals(object)) {
498 if (objectName == null) objectName = getObjectName(object);
499 throw new IllegalArgumentException(CommonI18n.argumentMustBeEquals.text(argumentName, objectName));
500 }
501 }
502
503 /**
504 * Asserts that the specified first object is not {@link Object#equals(Object) equal to} the specified second object.
505 *
506 * @param <T>
507 * @param argument The argument to assert equal to <code>object</code>.
508 * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown
509 * @param object The object to assert as equal to <code>argument</code>.
510 * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be
511 * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will
512 * be used.
513 * @throws IllegalArgumentException If the specified objects are equals.
514 */
515 public static <T> void isNotEquals( final T argument,
516 String argumentName,
517 final T object,
518 String objectName ) {
519 if (argument.equals(object)) {
520 if (objectName == null) objectName = getObjectName(object);
521 throw new IllegalArgumentException(CommonI18n.argumentMustNotBeEquals.text(argumentName, objectName));
522 }
523 }
524
525 // ########################## ITERATOR METHODS ###################################
526
527 /**
528 * Checks that the iterator is not empty, and throws an exception if it is.
529 *
530 * @param argument the iterator to check
531 * @param name The name of the argument
532 * @throws IllegalArgumentException If iterator is empty (i.e., iterator.hasNext() returns false)
533 */
534 public static void isNotEmpty( Iterator<?> argument,
535 String name ) {
536 isNotNull(argument, name);
537 if (!argument.hasNext()) {
538 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name));
539 }
540 }
541
542 // ########################## COLLECTION METHODS ###################################
543
544 /**
545 * Check that the collection is not empty
546 *
547 * @param argument Collection
548 * @param name The name of the argument
549 * @throws IllegalArgumentException If collection is null or empty
550 */
551 public static void isNotEmpty( Collection<?> argument,
552 String name ) {
553 isNotNull(argument, name);
554 if (argument.isEmpty()) {
555 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name));
556 }
557 }
558
559 /**
560 * Check that the map is not empty
561 *
562 * @param argument Map
563 * @param name The name of the argument
564 * @throws IllegalArgumentException If map is null or empty
565 */
566 public static void isNotEmpty( Map<?, ?> argument,
567 String name ) {
568 isNotNull(argument, name);
569 if (argument.isEmpty()) {
570 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name));
571 }
572 }
573
574 /**
575 * Check that the array is not empty
576 *
577 * @param argument Array
578 * @param name The name of the argument
579 * @throws IllegalArgumentException If array is null or empty
580 */
581 public static void isNotEmpty( Object[] argument,
582 String name ) {
583 isNotNull(argument, name);
584 if (argument.length == 0) {
585 throw new IllegalArgumentException(CommonI18n.argumentMayNotBeEmpty.text(name));
586 }
587 }
588
589 protected static String getObjectName( Object obj ) {
590 return obj == null ? null : "'" + obj.toString() + "'";
591 }
592
593 /**
594 * Check that the collection contains the value
595 *
596 * @param argument Collection to check
597 * @param value Value to check for, may be null
598 * @param name The name of the argument
599 * @throws IllegalArgumentException If collection is null or doesn't contain value
600 */
601 public static void contains( Collection<?> argument,
602 Object value,
603 String name ) {
604 isNotNull(argument, name);
605 if (!argument.contains(value)) {
606 throw new IllegalArgumentException(CommonI18n.argumentDidNotContainObject.text(name, getObjectName(value)));
607 }
608 }
609
610 /**
611 * Check that the map contains the key
612 *
613 * @param argument Map to check
614 * @param key Key to check for, may be null
615 * @param name The name of the argument
616 * @throws IllegalArgumentException If map is null or doesn't contain key
617 */
618 public static void containsKey( Map<?, ?> argument,
619 Object key,
620 String name ) {
621 isNotNull(argument, name);
622 if (!argument.containsKey(key)) {
623 throw new IllegalArgumentException(CommonI18n.argumentDidNotContainKey.text(name, getObjectName(key)));
624 }
625 }
626
627 /**
628 * Check that the collection is not null and contains no nulls
629 *
630 * @param argument Array
631 * @param name The name of the argument
632 * @throws IllegalArgumentException If array is null or has null values
633 */
634 public static void containsNoNulls( Iterable<?> argument,
635 String name ) {
636 isNotNull(argument, name);
637 int i = 0;
638 for (Object object : argument) {
639 if (object == null) {
640 throw new IllegalArgumentException(CommonI18n.argumentMayNotContainNullValue.text(name, i));
641 }
642 ++i;
643 }
644 }
645
646 /**
647 * Check that the array is not null and contains no nulls
648 *
649 * @param argument Array
650 * @param name The name of the argument
651 * @throws IllegalArgumentException If array is null or has null values
652 */
653 public static void containsNoNulls( Object[] argument,
654 String name ) {
655 isNotNull(argument, name);
656 int i = 0;
657 for (Object object : argument) {
658 if (object == null) {
659 throw new IllegalArgumentException(CommonI18n.argumentMayNotContainNullValue.text(name, i));
660 }
661 ++i;
662 }
663 }
664
665 /**
666 * Check that the collection contains at least the supplied number of elements
667 *
668 * @param argument Collection
669 * @param minimumSize the minimum size
670 * @param name The name of the argument
671 * @throws IllegalArgumentException If collection has a size smaller than the supplied value
672 */
673 public static void hasSizeOfAtLeast( Collection<?> argument,
674 int minimumSize,
675 String name ) {
676 isNotNull(argument, name);
677 if (argument.size() < minimumSize) {
678 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name,
679 Collection.class.getSimpleName(),
680 argument.size(),
681 minimumSize));
682 }
683 }
684
685 /**
686 * Check that the collection contains no more than the supplied number of elements
687 *
688 * @param argument Collection
689 * @param maximumSize the maximum size
690 * @param name The name of the argument
691 * @throws IllegalArgumentException If collection has a size smaller than the supplied value
692 */
693 public static void hasSizeOfAtMost( Collection<?> argument,
694 int maximumSize,
695 String name ) {
696 isNotNull(argument, name);
697 if (argument.size() > maximumSize) {
698 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name,
699 Collection.class.getSimpleName(),
700 argument.size(),
701 maximumSize));
702 }
703 }
704
705 /**
706 * Check that the map contains at least the supplied number of entries
707 *
708 * @param argument the map
709 * @param minimumSize the minimum size
710 * @param name The name of the argument
711 * @throws IllegalArgumentException If the map has a size smaller than the supplied value
712 */
713 public static void hasSizeOfAtLeast( Map<?, ?> argument,
714 int minimumSize,
715 String name ) {
716 isNotNull(argument, name);
717 if (argument.size() < minimumSize) {
718 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name,
719 Map.class.getSimpleName(),
720 argument.size(),
721 minimumSize));
722 }
723 }
724
725 /**
726 * Check that the map contains no more than the supplied number of entries
727 *
728 * @param argument the map
729 * @param maximumSize the maximum size
730 * @param name The name of the argument
731 * @throws IllegalArgumentException If the map has a size smaller than the supplied value
732 */
733 public static void hasSizeOfAtMost( Map<?, ?> argument,
734 int maximumSize,
735 String name ) {
736 isNotNull(argument, name);
737 if (argument.size() > maximumSize) {
738 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name,
739 Map.class.getSimpleName(),
740 argument.size(),
741 maximumSize));
742 }
743 }
744
745 /**
746 * Check that the array contains at least the supplied number of elements
747 *
748 * @param argument the array
749 * @param minimumSize the minimum size
750 * @param name The name of the argument
751 * @throws IllegalArgumentException If the array has a size smaller than the supplied value
752 */
753 public static void hasSizeOfAtLeast( Object[] argument,
754 int minimumSize,
755 String name ) {
756 isNotNull(argument, name);
757 if (argument.length < minimumSize) {
758 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name,
759 Object[].class.getSimpleName(),
760 argument.length,
761 minimumSize));
762 }
763 }
764
765 /**
766 * Check that the array contains no more than the supplied number of elements
767 *
768 * @param argument the array
769 * @param maximumSize the maximum size
770 * @param name The name of the argument
771 * @throws IllegalArgumentException If the array has a size smaller than the supplied value
772 */
773 public static void hasSizeOfAtMost( Object[] argument,
774 int maximumSize,
775 String name ) {
776 isNotNull(argument, name);
777 if (argument.length > maximumSize) {
778 throw new IllegalArgumentException(CommonI18n.argumentMustBeOfMinimumSize.text(name,
779 Object[].class.getSimpleName(),
780 argument.length,
781 maximumSize));
782 }
783 }
784
785 private CheckArg() {
786 // prevent construction
787 }
788 }