1 /*
2 * Copyright 2011 Jakub Narloch
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14 package org.castor.core.util;
15
16 /**
17 * A helper class that defines a method used for validating the input arguments.
18 *
19 * @author <a href="mailto:jmnarloch AT gmail DOT com">Jakub Narloch</a>
20 * @version 1.3.3
21 * @since 1.3.3
22 */
23 public class Assert {
24
25 /**
26 * Represents the error message used as exception description when a parameter is null.
27 */
28 private static final String NULL_PARAM_MESSAGE = "Parameter %s can not be null.";
29
30 /**
31 * Represents the error message used as exception description when a parameter is null or empty
32 * string.
33 */
34 private static final String EMPTY_PARAM_MESSAGE = "Parameter %s can not be null or empty.";
35
36 /**
37 * Creates new instance of {@link Assert} class.
38 * </p>
39 * Private constructor prevents instantiation outside this class.
40 */
41 private Assert() {
42 // empty constructor
43 }
44
45 /**
46 * Asserts that passed parameter is not null. In case it is, then an
47 * {@link IllegalArgumentException} is thrown.
48 *
49 * @param param the parameter to check
50 * @param paramName the parameter name
51 *
52 * @throws IllegalArgumentException if param is null
53 */
54 public static void paramNotNull(Object param, String paramName) {
55 notNull(param, String.format(NULL_PARAM_MESSAGE, paramName));
56 }
57
58 /**
59 * Asserts that passed parameter is not null and not empty. In case it is, then an
60 * {@link IllegalArgumentException} is thrown.
61 *
62 * @param param the parameter to check
63 * @param paramName the parameter name
64 *
65 * @throws IllegalArgumentException if param is null or empty string
66 */
67 public static void paramNotEmpty(String param, String paramName) {
68 notEmpty(param, String.format(EMPTY_PARAM_MESSAGE, paramName));
69 }
70
71 /**
72 * Asserts that passed object instance is not null. In case it is, then an
73 * {@link IllegalArgumentException} is thrown.
74 *
75 * @param obj the object instance to check
76 * @param msg the error message to use
77 *
78 * @throws IllegalArgumentException if obj is null
79 */
80 public static void notNull(Object obj, String msg) {
81 if (obj == null) {
82 throw new IllegalArgumentException(msg);
83 }
84 }
85
86 /**
87 * Asserts that passed object instance is not null and not empty. In case it is, then an
88 * {@link IllegalArgumentException} is thrown.
89 *
90 * @param obj the object instance to check
91 * @param msg the error message to use
92 *
93 * @throws IllegalArgumentException if obj is null or empty string
94 */
95 public static void notEmpty(String obj, String msg) {
96 notNull(obj, msg);
97 if (obj.trim().length() == 0) {
98 throw new IllegalArgumentException(msg);
99 }
100 }
101 }