1 /*
2 * Copyright 2011 Jakub Narloch
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.castor.core.util;
17
18 /**
19 * A helper class that defines a method used for validating the input arguments.
20 *
21 * @author <a href="mailto:jmnarloch AT gmail DOT com">Jakub Narloch</a>
22 * @version 1.3.3
23 * @since 1.3.3
24 */
25 public class Assert {
26
27 /**
28 * Represents the error message used as exception description when a parameter is null.
29 */
30 private static final String NULL_PARAM_MESSAGE = "Parameter %s can not be null.";
31
32 /**
33 * Represents the error message used as exception description when a parameter is null or empty string.
34 */
35 private static final String EMPTY_PARAM_MESSAGE = "Parameter %s can not be null or empty.";
36
37 /**
38 * Creates new instance of {@link Assert} class. </p> Private constructor prevents instantiation outside this
39 * 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,
60 * then an {@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,
73 * then an {@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
88 * it is, then an {@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 }