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