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