1 package com.zmicer.utils;
2
3 import org.apache.log4j.Logger;
4
5 /**
6 * This class provides the functionality for checking the input argument utils. It could be used for the checkfing the input
7 * argument of the method on the validness
8 *
9 * $Author:: zmicer $<br/>
10 * $Rev:: 57 $<br/> * $Date:: 2007-08-23 09:16:37 #$<br/>
11 */
12 public class InputArgumentUtils
13 {
14 /**
15 * Logger instance.
16 */
17 final public static Logger LOG = Logger.getLogger(InputArgumentUtils.class);
18
19 /**
20 * Check the all the input arguments of the provided var args are not null. Throw <code>IllegalArgumentException</code>
21 * in the case some of these params is null.
22 *
23 * @param objects the var arg of the objects to be checked.
24 * Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
25 *
26 * $Rev:: 57 $<br/> * $Date:: 2007-08-23 09:16:37 #$<br/>
27 */
28 public static void checkObjects(final Object... objects)
29 {
30 if (null == objects)
31 {
32 throw new IllegalArgumentException("The var arg list of input arguments to be checked, can not be null.");
33 }
34 boolean someOfParamsNull = false;
35 // on default is initialized with false;
36 boolean[] info = new boolean[objects.length];
37
38 int counter = 0;
39 for (Object object : objects)
40 {
41 if (null == object)
42 {
43 info[counter] = true;
44 someOfParamsNull = true;
45 }
46 counter++;
47 }
48 if (someOfParamsNull)
49 {
50 String toOutStr = "";
51 for (int i = 0; i < info.length; i++)
52 {
53 if (info[i])
54 {
55 toOutStr += " " + i;
56 }
57 }
58 throw new IllegalArgumentException("All the params passed to the checkObjects method can not be null - " + toOutStr +
59 " are null");
60 }
61 }
62
63 /**
64 * Check Stiring objects provided.
65 *
66 * @param checkOnEmpty flag indicating that we need to check the String on emptyness too. In the case this is <code>true</code>,
67 * all the String objects would be checked on the emptyness
68 * @param strings The var args of String object to be checked
69 */
70 public static void checkStrings(final boolean checkOnEmpty, final String... strings)
71 {
72 checkObjects((Object[])strings);
73 if (checkOnEmpty)
74 {
75 for (final String str : strings)
76 {
77 if ("".equals(str.trim()))
78 {
79 throw new IllegalArgumentException("The provided var args should contains not null and not empty String objects");
80 }
81 }
82 }
83 }
84 }