1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util;
18
19 /***
20 * <p>
21 * ArgUtil
22 * </p>
23 *
24 * Misc. utilities for rudimentary argument validation
25 *
26 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
27 * @version $Id: ArgUtil.java 516448 2007-03-09 16:25:47Z ate $
28 *
29 */
30 public final class ArgUtil
31 {
32 private static final String MSG_1 = "Argument \"";
33 private static final String MSG_2 = "\" cannot be null for method ";
34
35 /***
36 *
37 * @throws java.lang.IllegalArgumentException If ANY of the arguments are <code>null</code>
38 * @param args array of arguments to validate as not nul
39 * @param argNames array of arguments names, idexes should match with args.
40 * @param methodName Name of method we are validating arguments for.
41 */
42 public static void notNull(Object[] args, String[] argNames, String methodName)
43 {
44 for (int i = 0; i < args.length; i++)
45 {
46 Object arg = args[i];
47 if (arg == null)
48 {
49 StringBuffer buf = new StringBuffer(150);
50 String argName = null;
51 if (i < argNames.length)
52 {
53 argName = argNames[i];
54 }
55 else
56 {
57 argName = String.valueOf(i);
58 }
59
60 buf.append(MSG_1 + argName + MSG_2 + methodName);
61 throw new IllegalArgumentException(buf.toString());
62 }
63 }
64 }
65
66 /***
67 *
68 * <p>
69 * notNull
70 * </p>
71 *
72 * @param nonNullObject
73 * @param thisObject
74 * @throws IllegalArgumentException
75 */
76 public static final void assertNotNull(Class nonNullClass, Object nonNullObject, Object thisObject) throws IllegalArgumentException
77 {
78 if(nonNullObject == null)
79 {
80 throw new IllegalArgumentException(thisObject.getClass().getName()+" requires a non-null "+nonNullClass.getName()+" as an argument.");
81 }
82 }
83
84 public static final void assertNotNull(Class nonNullClass, Object nonNullObject, Object thisObject, String methodName) throws IllegalArgumentException
85 {
86 if(nonNullObject == null)
87 {
88 throw new IllegalArgumentException(thisObject.getClass().getName()+"."+methodName+" requires a non-null "+nonNullClass.getName()+" as an argument.");
89 }
90 }
91
92 public static final void assertPropertyNotNull(Object nonNullObject, Object thisObject, String methodName, String property) throws IllegalArgumentException
93 {
94 if(nonNullObject == null)
95 {
96 throw new IllegalStateException(thisObject.getClass().getName()+"."+methodName+" cannot be invoked until the property "+property+" has been set.");
97 }
98 }
99 }