View Javadoc

1   package org.ocltf.utils;
2   
3   import org.apache.commons.lang.StringUtils;
4   
5   /***
6    * Contains Exception handling utilities.
7    * 
8    * @author Chad Brandon
9    */
10  public class ExceptionUtils {
11      
12  	/***
13  	 * Checks if the argument is null, and if so, 
14  	 * throws an IllegalArgumentException, does nothing if not.
15  	 * @param methodExecuteName the name of the method we are currently executing
16  	 * @param argumentName - the name of the argument we are checking for null
17  	 * @param argument - the argument we are checking
18  	 */
19      public static void checkNull(
20      	String methodExecuteName, 
21      	String argumentName,  
22      	Object argument) {
23      	String methodName = "checkNull";
24      	if (StringUtils.isEmpty(methodExecuteName)) {
25      		throw new IllegalArgumentException(methodName + 
26      			" - methodExecuteName can not be null or an empty String");
27      	}
28  		if (StringUtils.isEmpty(argumentName)) {
29  			throw new IllegalArgumentException("methodName: " + methodName + 
30  				" - argumentName can not be null or an empty Stringl");
31  		}
32  		
33  		//this is what the method is actually for
34  		if (argument == null) {
35  			throw new IllegalArgumentException(methodExecuteName + 
36  				" - " + argumentName + " can not be null");
37  		}
38      }
39      
40  	/***
41  	 * Checks if the argument is null or an empty String
42  	 * throws an IllegalArgumentException if it is, does nothing if not
43  	 * @param methodExecuteName the name of the method we are currently executing
44  	 * @param argumentName the name of the argument we are checking for null
45  	 * @param argument the argument we are checking
46  	 */
47  	public static void checkEmpty(
48  		String methodExecuteName, 
49  		String argumentName,
50  		String argument) {
51  		String methodName = "checkEmpty";
52  		if (StringUtils.isEmpty(methodExecuteName)) {
53  			throw new IllegalArgumentException(methodName + 
54  				" - methodExecuteName can not be null or an empty String");
55  		}
56  		if (StringUtils.isEmpty(argumentName)) {
57  			throw new IllegalArgumentException(methodName + 
58  				" - argumentName can not be null or an empty String");
59  		}
60  		
61  		//this is what the method is actually for
62  		if (StringUtils.isEmpty(argument)) {
63  			throw new IllegalArgumentException("methodName: " + methodExecuteName + 
64  				" - " + argumentName + " can not be null or an empty String");
65  		}
66  	}
67  	
68  	/***
69  	 * Checks if the argumentClass is assignable to assignableToClass, and if
70  	 * not throws an IllegalArgumentException, otherwise does nothing.
71  	 * @param methodExecuteName - the method name of the method, 
72  	 * 		   this method is being executed within
73  	 * @param assignableToClass - the Class that argumentClass must be assignable to
74  	 * @param argumentClass - the argumentClass we are checking
75  	 * @param argumentName - the name of the argument we are checking
76  	 */
77  	public static void checkAssignable(
78  		String methodExecuteName,
79  		Class assignableToClass,
80  		String argumentName,
81  		Class argumentClass) {
82  		String methodName = "checkAssignable";
83          if (StringUtils.isEmpty(methodExecuteName)) {
84              throw new IllegalArgumentException(methodName + 
85                  " - methodExecuteName can not be null or an empty String");
86          }
87          if (assignableToClass == null) {
88              throw new IllegalArgumentException(methodName + 
89                  " - assignableToClass can not be null");
90          }  
91          if (argumentClass == null) {
92              throw new IllegalArgumentException(methodName + 
93                  " - argumentClass can not be null");
94          } 
95          if (StringUtils.isEmpty(argumentName)) {
96              throw new IllegalArgumentException(methodName + 
97                  " - argumentName can not be null or an empty String");
98          }
99  		
100         
101         //this is what the method is for
102 		if (!assignableToClass.isAssignableFrom(argumentClass)) {
103 			throw new IllegalArgumentException("methodName: " + methodExecuteName +
104             " - " + argumentName + " (" + argumentClass + ") must be assignable to class (" 
105             + assignableToClass + ")"); 
106 		}
107 	}
108 	
109     /***
110      * Checks if the passed in String argument is a valid Integer, if not
111      * throws an IllegalArgumentException, otherwise returns the converted
112      * argument to a converted Integer
113      * @param methodExecuteName - the method name of the method, 
114      * 		   this method is being executed within
115      * @param argumentName - the name of the argument we are checking
116      * @param argument - the actual argument we are checking to see if its a valid Integer
117      * @return Integer
118      */
119 	public static Integer checkValidInteger(
120 		String methodExecuteName,
121 		String argumentName,
122 		String argument) {
123         String methodName = "checkAssignable";
124         if (StringUtils.isEmpty(methodExecuteName)) {
125             throw new IllegalArgumentException(methodName + 
126                 " - methodExecuteName can not be null or an empty String");
127         }
128         if (StringUtils.isEmpty(argumentName)) {
129             throw new IllegalArgumentException(methodName + 
130                 " - argumentName can not be null or an empty String");
131         }
132         if (StringUtils.isEmpty(argument)) {
133             throw new IllegalArgumentException(methodName + 
134                 " - argument can not be null or an empty String");
135         }
136         Integer integer = null;
137 		try {
138 		    integer = Integer.valueOf(argument);
139 		} catch (NumberFormatException ex) {
140             throw new IllegalArgumentException("methodName: " + methodExecuteName
141                 + " - " + argumentName + " (" + argument + ") is not a valid Integer");			    
142 		}	    
143 		return integer;
144 	}
145    
146 }