1 package net.sf.mavenhist.util;
2
3 import org.apache.commons.lang.StringUtils;
4
5 /**
6 * Utility class to check the parameters of a bean. Use the methods to check a bunch of parameters
7 * and generate an accumulated error message.
8 */
9 public final class ParamCheckUtil {
10
11 /**
12 * Private constructor to not instantiate {@link ParamCheckUtil}.
13 */
14 private ParamCheckUtil() { }
15
16 /**
17 * Checks a parameter and adds a message to the buffer if value was not valid.
18 * @param messageBuffer to append the error message, when validation fails.
19 * @param paramValue to validate.
20 * @param paramName to append in the error message when appropriate.
21 * @return validation result.
22 */
23 public static boolean checkParam(
24 StringBuffer messageBuffer,
25 String paramValue,
26 String paramName) {
27 if (StringUtils.isEmpty(paramValue)) {
28 writeToExceptionMessage(messageBuffer, paramName);
29 return false;
30 }
31 return true;
32 }
33
34 /**
35 * Checks an array of parameters and adds a message to the buffer if value was not valid.
36 * @param messageBuffer to append the error message, when validation fails.
37 * @param paramValues to validate.
38 * @param paramName to append in the error message when appropriate.
39 * @return validation result.
40 */
41 public static boolean checkParam(
42 StringBuffer messageBuffer,
43 Object[] paramValues,
44 String paramName) {
45 if (paramValues == null || paramValues.length <= 0) {
46 writeToExceptionMessage(messageBuffer, paramName);
47 return false;
48 }
49 return true;
50 }
51
52 /**
53 * Checks a parameter and adds a message to the buffer if value was not valid.
54 * @param messageBuffer to append the error message, when validation fails.
55 * @param paramValue to validate.
56 * @param paramName to append in the error message when appropriate.
57 * @return validation result.
58 */
59 public static boolean checkParam(
60 StringBuffer messageBuffer,
61 Object paramValue,
62 String paramName) {
63 if (paramValue == null) {
64 writeToExceptionMessage(messageBuffer, paramName);
65 return false;
66 }
67 return true;
68 }
69
70 /**
71 * Checks two parameters and adds a message to the buffer if the values were not valid. One of
72 * both paramters must be set, that the params are valid.
73 * @param messageBuffer to append the error message, when validation fails.
74 * @param paramValue1 to validate.
75 * @param paramName1 to append in the error message when appropriate.
76 * @param paramValue2 to validate.
77 * @param paramName2 to append in the error message when appropriate.
78 * @return validation result.
79 */
80 public static boolean checkParamOr(
81 StringBuffer messageBuffer,
82 Object paramValue1,
83 String paramName1,
84 Object paramValue2,
85 String paramName2) {
86 if (paramValue1 == null && paramValue2 == null) {
87 writeToExceptionMessage(messageBuffer, paramName1, paramName2);
88 return false;
89 }
90 return true;
91 }
92
93 /**
94 * Internal helper to add the error message for two parameters to the message buffer. The
95 * boolean operator between them is OR.
96 *
97 * @param messageBuffer to add the message.
98 * @param paramName1 that was missing.
99 * @param paramName2 that was missing.
100 */
101 private static void writeToExceptionMessage(
102 StringBuffer messageBuffer,
103 String paramName1,
104 String paramName2) {
105 messageBuffer.append(paramName1);
106 messageBuffer.append(" and ");
107 messageBuffer.append(paramName2);
108 messageBuffer.append(" are not set, one of both must be set. ");
109 }
110
111 /**
112 * Internal helper to add the error message for a parameter to the message buffer.
113 * @param messageBuffer to add the message.
114 * @param paramName that was missing.
115 */
116 private static void writeToExceptionMessage(StringBuffer messageBuffer, String paramName) {
117 messageBuffer.append(paramName);
118 messageBuffer.append(" not set. ");
119 }
120 }