add SqlManagerImpl#setValueTypes
to make it easier to configure valueTypes using Spring framework
@@ -27,6 +27,7 @@ | ||
27 | 27 | import jp.sf.amateras.mirage.type.ValueType; |
28 | 28 | import jp.sf.amateras.mirage.util.IOUtil; |
29 | 29 | import jp.sf.amateras.mirage.util.MirageUtil; |
30 | +import jp.sf.amateras.mirage.util.Validate; | |
30 | 31 | |
31 | 32 | public class SqlManagerImpl implements SqlManager { |
32 | 33 |
@@ -323,6 +324,19 @@ | ||
323 | 324 | String executeSql = MirageUtil.buildSelectSQL(clazz, nameConverter); |
324 | 325 | return sqlExecutor.getSingleResult(clazz, executeSql, id); |
325 | 326 | } |
327 | + | |
328 | + /** | |
329 | + * | |
330 | + * @param valueTypes | |
331 | + * @throws IllegalArgumentException if the {@code valueTypes} is {@code null} or | |
332 | + * an element in the {@code valueTypes} is {@code null} | |
333 | + * @author daisuke | |
334 | + */ | |
335 | + public void setValueTypes(List<ValueType> valueTypes) { | |
336 | + Validate.noNullElements(valueTypes); | |
337 | + this.sqlExecutor.setValueTypes(valueTypes); | |
338 | + this.callExecutor.setValueTypes(valueTypes); | |
339 | + } | |
326 | 340 | |
327 | 341 | // @Override |
328 | 342 | public void addValueType(ValueType valueType) { |
@@ -22,6 +22,7 @@ | ||
22 | 22 | import jp.sf.amateras.mirage.type.ValueType; |
23 | 23 | import jp.sf.amateras.mirage.util.JdbcUtil; |
24 | 24 | import jp.sf.amateras.mirage.util.MirageUtil; |
25 | +import jp.sf.amateras.mirage.util.Validate; | |
25 | 26 | |
26 | 27 | public class SqlExecutor { |
27 | 28 |
@@ -41,6 +42,18 @@ | ||
41 | 42 | this.nameConverter = nameConverter; |
42 | 43 | } |
43 | 44 | |
45 | + /** | |
46 | + * | |
47 | + * @param valueTypes | |
48 | + * @throws IllegalArgumentException if the {@code valueTypes} is {@code null} or | |
49 | + * an element in the {@code valueTypes} is {@code null} | |
50 | + * @author daisuke | |
51 | + */ | |
52 | + public void setValueTypes(List<ValueType> valueTypes) { | |
53 | + Validate.notNull(valueTypes); | |
54 | + this.valueTypes = valueTypes; | |
55 | + } | |
56 | + | |
44 | 57 | public void addValueType(ValueType valueType){ |
45 | 58 | this.valueTypes.add(valueType); |
46 | 59 | } |
@@ -23,6 +23,7 @@ | ||
23 | 23 | import jp.sf.amateras.mirage.util.JdbcUtil; |
24 | 24 | import jp.sf.amateras.mirage.util.ModifierUtil; |
25 | 25 | import jp.sf.amateras.mirage.util.ReflectionUtil; |
26 | +import jp.sf.amateras.mirage.util.Validate; | |
26 | 27 | |
27 | 28 | public class CallExecutor { |
28 | 29 |
@@ -46,6 +47,17 @@ | ||
46 | 47 | this.dialect = dialect; |
47 | 48 | } |
48 | 49 | |
50 | + /** | |
51 | + * | |
52 | + * @param valueTypes | |
53 | + * @throws IllegalArgumentException if the {@code valueTypes} is {@code null} or | |
54 | + * an element in the {@code valueTypes} is {@code null} | |
55 | + */ | |
56 | + public void setValueTypes(List<ValueType> valueTypes) { | |
57 | + Validate.notNull(valueTypes); | |
58 | + this.valueTypes = valueTypes; | |
59 | + } | |
60 | + | |
49 | 61 | public void addValueType(ValueType valueType){ |
50 | 62 | this.valueTypes.add(valueType); |
51 | 63 | } |
@@ -0,0 +1,452 @@ | ||
1 | +/* | |
2 | + * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | + * contributor license agreements. See the NOTICE file distributed with | |
4 | + * this work for additional information regarding copyright ownership. | |
5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | + * (the "License"); you may not use this file except in compliance with | |
7 | + * the License. You may obtain a copy of the License at | |
8 | + * | |
9 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | + * | |
11 | + * Unless required by applicable law or agreed to in writing, software | |
12 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | + * See the License for the specific language governing permissions and | |
15 | + * limitations under the License. | |
16 | + */ | |
17 | +package jp.sf.amateras.mirage.util; | |
18 | + | |
19 | +import java.util.Collection; | |
20 | +import java.util.Iterator; | |
21 | +import java.util.Map; | |
22 | + | |
23 | +/** | |
24 | + * <p>This class assists in validating arguments.</p> | |
25 | + * | |
26 | + * <p>The class is based along the lines of JUnit. If an argument value is | |
27 | + * deemed invalid, an IllegalArgumentException is thrown. For example:</p> | |
28 | + * | |
29 | + * <pre> | |
30 | + * Validate.isTrue( i > 0, "The value must be greater than zero: ", i); | |
31 | + * Validate.notNull( surname, "The surname must not be null"); | |
32 | + * </pre> | |
33 | + * | |
34 | + * @author Apache Software Foundation | |
35 | + * @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a> | |
36 | + * @author Gary Gregory | |
37 | + * @author Norm Deane | |
38 | + * @since 2.0 | |
39 | + * @version $Id$ | |
40 | + */ | |
41 | +public class Validate { | |
42 | + // Validate has no dependencies on other classes in Commons Lang at present | |
43 | + | |
44 | + /** | |
45 | + * Constructor. This class should not normally be instantiated. | |
46 | + */ | |
47 | + public Validate() { | |
48 | + super(); | |
49 | + } | |
50 | + | |
51 | + // isTrue | |
52 | + //--------------------------------------------------------------------------------- | |
53 | + /** | |
54 | + * <p>Validate that the argument condition is <code>true</code>; otherwise | |
55 | + * throwing an exception with the specified message. This method is useful when | |
56 | + * validating according to an arbitrary boolean expression, such as validating an | |
57 | + * object or using your own custom validation expression.</p> | |
58 | + * | |
59 | + * <pre>Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);</pre> | |
60 | + * | |
61 | + * <p>For performance reasons, the object value is passed as a separate parameter and | |
62 | + * appended to the exception message only in the case of an error.</p> | |
63 | + * | |
64 | + * @param expression the boolean expression to check | |
65 | + * @param message the exception message if invalid | |
66 | + * @param value the value to append to the message when invalid | |
67 | + * @throws IllegalArgumentException if expression is <code>false</code> | |
68 | + */ | |
69 | + public static void isTrue(boolean expression, String message, Object value) { | |
70 | + if (expression == false) { | |
71 | + throw new IllegalArgumentException(message + value); | |
72 | + } | |
73 | + } | |
74 | + | |
75 | + /** | |
76 | + * <p>Validate that the argument condition is <code>true</code>; otherwise | |
77 | + * throwing an exception with the specified message. This method is useful when | |
78 | + * validating according to an arbitrary boolean expression, such as validating a | |
79 | + * primitive number or using your own custom validation expression.</p> | |
80 | + * | |
81 | + * <pre>Validate.isTrue(i > 0.0, "The value must be greater than zero: ", i);</pre> | |
82 | + * | |
83 | + * <p>For performance reasons, the long value is passed as a separate parameter and | |
84 | + * appended to the exception message only in the case of an error.</p> | |
85 | + * | |
86 | + * @param expression the boolean expression to check | |
87 | + * @param message the exception message if invalid | |
88 | + * @param value the value to append to the message when invalid | |
89 | + * @throws IllegalArgumentException if expression is <code>false</code> | |
90 | + */ | |
91 | + public static void isTrue(boolean expression, String message, long value) { | |
92 | + if (expression == false) { | |
93 | + throw new IllegalArgumentException(message + value); | |
94 | + } | |
95 | + } | |
96 | + | |
97 | + /** | |
98 | + * <p>Validate that the argument condition is <code>true</code>; otherwise | |
99 | + * throwing an exception with the specified message. This method is useful when | |
100 | + * validating according to an arbitrary boolean expression, such as validating a | |
101 | + * primitive number or using your own custom validation expression.</p> | |
102 | + * | |
103 | + * <pre>Validate.isTrue(d > 0.0, "The value must be greater than zero: ", d);</pre> | |
104 | + * | |
105 | + * <p>For performance reasons, the double value is passed as a separate parameter and | |
106 | + * appended to the exception message only in the case of an error.</p> | |
107 | + * | |
108 | + * @param expression the boolean expression to check | |
109 | + * @param message the exception message if invalid | |
110 | + * @param value the value to append to the message when invalid | |
111 | + * @throws IllegalArgumentException if expression is <code>false</code> | |
112 | + */ | |
113 | + public static void isTrue(boolean expression, String message, double value) { | |
114 | + if (expression == false) { | |
115 | + throw new IllegalArgumentException(message + value); | |
116 | + } | |
117 | + } | |
118 | + | |
119 | + /** | |
120 | + * <p>Validate that the argument condition is <code>true</code>; otherwise | |
121 | + * throwing an exception with the specified message. This method is useful when | |
122 | + * validating according to an arbitrary boolean expression, such as validating a | |
123 | + * primitive number or using your own custom validation expression.</p> | |
124 | + * | |
125 | + * <pre> | |
126 | + * Validate.isTrue( (i > 0), "The value must be greater than zero"); | |
127 | + * Validate.isTrue( myObject.isOk(), "The object is not OK"); | |
128 | + * </pre> | |
129 | + * | |
130 | + * @param expression the boolean expression to check | |
131 | + * @param message the exception message if invalid | |
132 | + * @throws IllegalArgumentException if expression is <code>false</code> | |
133 | + */ | |
134 | + public static void isTrue(boolean expression, String message) { | |
135 | + if (expression == false) { | |
136 | + throw new IllegalArgumentException(message); | |
137 | + } | |
138 | + } | |
139 | + | |
140 | + /** | |
141 | + * <p>Validate that the argument condition is <code>true</code>; otherwise | |
142 | + * throwing an exception. This method is useful when validating according | |
143 | + * to an arbitrary boolean expression, such as validating a | |
144 | + * primitive number or using your own custom validation expression.</p> | |
145 | + * | |
146 | + * <pre> | |
147 | + * Validate.isTrue(i > 0); | |
148 | + * Validate.isTrue(myObject.isOk());</pre> | |
149 | + * | |
150 | + * <p>The message of the exception is "The validated expression is | |
151 | + * false".</p> | |
152 | + * | |
153 | + * @param expression the boolean expression to check | |
154 | + * @throws IllegalArgumentException if expression is <code>false</code> | |
155 | + */ | |
156 | + public static void isTrue(boolean expression) { | |
157 | + if (expression == false) { | |
158 | + throw new IllegalArgumentException("The validated expression is false"); | |
159 | + } | |
160 | + } | |
161 | + | |
162 | + // notNull | |
163 | + //--------------------------------------------------------------------------------- | |
164 | + | |
165 | + /** | |
166 | + * <p>Validate that the specified argument is not <code>null</code>; | |
167 | + * otherwise throwing an exception. | |
168 | + * | |
169 | + * <pre>Validate.notNull(myObject);</pre> | |
170 | + * | |
171 | + * <p>The message of the exception is "The validated object is | |
172 | + * null".</p> | |
173 | + * | |
174 | + * @param object the object to check | |
175 | + * @throws IllegalArgumentException if the object is <code>null</code> | |
176 | + */ | |
177 | + public static void notNull(Object object) { | |
178 | + notNull(object, "The validated object is null"); | |
179 | + } | |
180 | + | |
181 | + /** | |
182 | + * <p>Validate that the specified argument is not <code>null</code>; | |
183 | + * otherwise throwing an exception with the specified message. | |
184 | + * | |
185 | + * <pre>Validate.notNull(myObject, "The object must not be null");</pre> | |
186 | + * | |
187 | + * @param object the object to check | |
188 | + * @param message the exception message if invalid | |
189 | + */ | |
190 | + public static void notNull(Object object, String message) { | |
191 | + if (object == null) { | |
192 | + throw new IllegalArgumentException(message); | |
193 | + } | |
194 | + } | |
195 | + | |
196 | + // notEmpty array | |
197 | + //--------------------------------------------------------------------------------- | |
198 | + | |
199 | + /** | |
200 | + * <p>Validate that the specified argument array is neither <code>null</code> | |
201 | + * nor a length of zero (no elements); otherwise throwing an exception | |
202 | + * with the specified message. | |
203 | + * | |
204 | + * <pre>Validate.notEmpty(myArray, "The array must not be empty");</pre> | |
205 | + * | |
206 | + * @param array the array to check | |
207 | + * @param message the exception message if invalid | |
208 | + * @throws IllegalArgumentException if the array is empty | |
209 | + */ | |
210 | + public static void notEmpty(Object[] array, String message) { | |
211 | + if (array == null || array.length == 0) { | |
212 | + throw new IllegalArgumentException(message); | |
213 | + } | |
214 | + } | |
215 | + | |
216 | + /** | |
217 | + * <p>Validate that the specified argument array is neither <code>null</code> | |
218 | + * nor a length of zero (no elements); otherwise throwing an exception. | |
219 | + * | |
220 | + * <pre>Validate.notEmpty(myArray);</pre> | |
221 | + * | |
222 | + * <p>The message in the exception is "The validated array is | |
223 | + * empty". | |
224 | + * | |
225 | + * @param array the array to check | |
226 | + * @throws IllegalArgumentException if the array is empty | |
227 | + */ | |
228 | + public static void notEmpty(Object[] array) { | |
229 | + notEmpty(array, "The validated array is empty"); | |
230 | + } | |
231 | + | |
232 | + // notEmpty collection | |
233 | + //--------------------------------------------------------------------------------- | |
234 | + | |
235 | + /** | |
236 | + * <p>Validate that the specified argument collection is neither <code>null</code> | |
237 | + * nor a size of zero (no elements); otherwise throwing an exception | |
238 | + * with the specified message. | |
239 | + * | |
240 | + * <pre>Validate.notEmpty(myCollection, "The collection must not be empty");</pre> | |
241 | + * | |
242 | + * @param collection the collection to check | |
243 | + * @param message the exception message if invalid | |
244 | + * @throws IllegalArgumentException if the collection is empty | |
245 | + */ | |
246 | + public static void notEmpty(Collection<?> collection, String message) { | |
247 | + if (collection == null || collection.size() == 0) { | |
248 | + throw new IllegalArgumentException(message); | |
249 | + } | |
250 | + } | |
251 | + | |
252 | + /** | |
253 | + * <p>Validate that the specified argument collection is neither <code>null</code> | |
254 | + * nor a size of zero (no elements); otherwise throwing an exception. | |
255 | + * | |
256 | + * <pre>Validate.notEmpty(myCollection);</pre> | |
257 | + * | |
258 | + * <p>The message in the exception is "The validated collection is | |
259 | + * empty".</p> | |
260 | + * | |
261 | + * @param collection the collection to check | |
262 | + * @throws IllegalArgumentException if the collection is empty | |
263 | + */ | |
264 | + public static void notEmpty(Collection<?> collection) { | |
265 | + notEmpty(collection, "The validated collection is empty"); | |
266 | + } | |
267 | + | |
268 | + // notEmpty map | |
269 | + //--------------------------------------------------------------------------------- | |
270 | + | |
271 | + /** | |
272 | + * <p>Validate that the specified argument map is neither <code>null</code> | |
273 | + * nor a size of zero (no elements); otherwise throwing an exception | |
274 | + * with the specified message. | |
275 | + * | |
276 | + * <pre>Validate.notEmpty(myMap, "The map must not be empty");</pre> | |
277 | + * | |
278 | + * @param map the map to check | |
279 | + * @param message the exception message if invalid | |
280 | + * @throws IllegalArgumentException if the map is empty | |
281 | + */ | |
282 | + public static void notEmpty(Map<?,?> map, String message) { | |
283 | + if (map == null || map.size() == 0) { | |
284 | + throw new IllegalArgumentException(message); | |
285 | + } | |
286 | + } | |
287 | + | |
288 | + /** | |
289 | + * <p>Validate that the specified argument map is neither <code>null</code> | |
290 | + * nor a size of zero (no elements); otherwise throwing an exception. | |
291 | + * | |
292 | + * <pre>Validate.notEmpty(myMap);</pre> | |
293 | + * | |
294 | + * <p>The message in the exception is "The validated map is | |
295 | + * empty".</p> | |
296 | + * | |
297 | + * @param map the map to check | |
298 | + * @throws IllegalArgumentException if the map is empty | |
299 | + * @see #notEmpty(Map, String) | |
300 | + */ | |
301 | + public static void notEmpty(Map<?,?> map) { | |
302 | + notEmpty(map, "The validated map is empty"); | |
303 | + } | |
304 | + | |
305 | + // notEmpty string | |
306 | + //--------------------------------------------------------------------------------- | |
307 | + | |
308 | + /** | |
309 | + * <p>Validate that the specified argument string is | |
310 | + * neither <code>null</code> nor a length of zero (no characters); | |
311 | + * otherwise throwing an exception with the specified message. | |
312 | + * | |
313 | + * <pre>Validate.notEmpty(myString, "The string must not be empty");</pre> | |
314 | + * | |
315 | + * @param string the string to check | |
316 | + * @param message the exception message if invalid | |
317 | + * @throws IllegalArgumentException if the string is empty | |
318 | + */ | |
319 | + public static void notEmpty(String string, String message) { | |
320 | + if (string == null || string.length() == 0) { | |
321 | + throw new IllegalArgumentException(message); | |
322 | + } | |
323 | + } | |
324 | + | |
325 | + /** | |
326 | + * <p>Validate that the specified argument string is | |
327 | + * neither <code>null</code> nor a length of zero (no characters); | |
328 | + * otherwise throwing an exception with the specified message. | |
329 | + * | |
330 | + * <pre>Validate.notEmpty(myString);</pre> | |
331 | + * | |
332 | + * <p>The message in the exception is "The validated | |
333 | + * string is empty".</p> | |
334 | + * | |
335 | + * @param string the string to check | |
336 | + * @throws IllegalArgumentException if the string is empty | |
337 | + */ | |
338 | + public static void notEmpty(String string) { | |
339 | + notEmpty(string, "The validated string is empty"); | |
340 | + } | |
341 | + | |
342 | + // notNullElements array | |
343 | + //--------------------------------------------------------------------------------- | |
344 | + | |
345 | + /** | |
346 | + * <p>Validate that the specified argument array is neither | |
347 | + * <code>null</code> nor contains any elements that are <code>null</code>; | |
348 | + * otherwise throwing an exception with the specified message. | |
349 | + * | |
350 | + * <pre>Validate.noNullElements(myArray, "The array contain null at position %d");</pre> | |
351 | + * | |
352 | + * <p>If the array is <code>null</code>, then the message in the exception | |
353 | + * is "The validated object is null".</p> | |
354 | + * | |
355 | + * @param array the array to check | |
356 | + * @param message the exception message if the collection has <code>null</code> elements | |
357 | + * @throws IllegalArgumentException if the array is <code>null</code> or | |
358 | + * an element in the array is <code>null</code> | |
359 | + */ | |
360 | + public static void noNullElements(Object[] array, String message) { | |
361 | + Validate.notNull(array); | |
362 | + for (int i = 0; i < array.length; i++) { | |
363 | + if (array[i] == null) { | |
364 | + throw new IllegalArgumentException(message); | |
365 | + } | |
366 | + } | |
367 | + } | |
368 | + | |
369 | + /** | |
370 | + * <p>Validate that the specified argument array is neither | |
371 | + * <code>null</code> nor contains any elements that are <code>null</code>; | |
372 | + * otherwise throwing an exception. | |
373 | + * | |
374 | + * <pre>Validate.noNullElements(myArray);</pre> | |
375 | + * | |
376 | + * <p>If the array is <code>null</code>, then the message in the exception | |
377 | + * is "The validated object is null".</p> | |
378 | + * | |
379 | + * <p>If the array has a <code>null</code> element, then the message in the | |
380 | + * exception is "The validated array contains null element at index: | |
381 | + * " followed by the index.</p> | |
382 | + * | |
383 | + * @param array the array to check | |
384 | + * @throws IllegalArgumentException if the array is <code>null</code> or | |
385 | + * an element in the array is <code>null</code> | |
386 | + */ | |
387 | + public static void noNullElements(Object[] array) { | |
388 | + Validate.notNull(array); | |
389 | + for (int i = 0; i < array.length; i++) { | |
390 | + if (array[i] == null) { | |
391 | + throw new IllegalArgumentException("The validated array contains null element at index: " + i); | |
392 | + } | |
393 | + } | |
394 | + } | |
395 | + | |
396 | + // notNullElements collection | |
397 | + //--------------------------------------------------------------------------------- | |
398 | + | |
399 | + /** | |
400 | + * <p>Validate that the specified argument collection is neither | |
401 | + * <code>null</code> nor contains any elements that are <code>null</code>; | |
402 | + * otherwise throwing an exception with the specified message. | |
403 | + * | |
404 | + * <pre>Validate.noNullElements(myCollection, "The collection contains null elements");</pre> | |
405 | + * | |
406 | + * <p>If the collection is <code>null</code>, then the message in the exception | |
407 | + * is "The validated object is null".</p> | |
408 | + * | |
409 | + * | |
410 | + * @param collection the collection to check | |
411 | + * @param message the exception message if the collection has | |
412 | + * @throws IllegalArgumentException if the collection is <code>null</code> or | |
413 | + * an element in the collection is <code>null</code> | |
414 | + */ | |
415 | + public static void noNullElements(Collection<?> collection, String message) { | |
416 | + Validate.notNull(collection); | |
417 | + for (Iterator<?> it = collection.iterator(); it.hasNext();) { | |
418 | + if (it.next() == null) { | |
419 | + throw new IllegalArgumentException(message); | |
420 | + } | |
421 | + } | |
422 | + } | |
423 | + | |
424 | + /** | |
425 | + * <p>Validate that the specified argument collection is neither | |
426 | + * <code>null</code> nor contains any elements that are <code>null</code>; | |
427 | + * otherwise throwing an exception. | |
428 | + * | |
429 | + * <pre>Validate.noNullElements(myCollection);</pre> | |
430 | + * | |
431 | + * <p>If the collection is <code>null</code>, then the message in the exception | |
432 | + * is "The validated object is null".</p> | |
433 | + * | |
434 | + * <p>If the collection has a <code>null</code> element, then the message in the | |
435 | + * exception is "The validated collection contains null element at index: | |
436 | + * " followed by the index.</p> | |
437 | + * | |
438 | + * @param collection the collection to check | |
439 | + * @throws IllegalArgumentException if the collection is <code>null</code> or | |
440 | + * an element in the collection is <code>null</code> | |
441 | + */ | |
442 | + public static void noNullElements(Collection<?> collection) { | |
443 | + Validate.notNull(collection); | |
444 | + int i = 0; | |
445 | + for (Iterator<?> it = collection.iterator(); it.hasNext(); i++) { | |
446 | + if (it.next() == null) { | |
447 | + throw new IllegalArgumentException("The validated collection contains null element at index: " + i); | |
448 | + } | |
449 | + } | |
450 | + } | |
451 | + | |
452 | +} |