1 /*
2 * Copyright (C) 2014 jsonwebtoken.io
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 io.jsonwebtoken.lang;
17
18 import java.util.Collection;
19 import java.util.Map;
20
21 public final class Assert {
22
23 //for code coverage
24 private static final Assert INSTANCE = new Assert();
25
26 private Assert(){}
27
28 /**
29 * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
30 * if the test result is <code>false</code>.
31 * <pre class="code">Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
32 * @param expression a boolean expression
33 * @param message the exception message to use if the assertion fails
34 * @throws IllegalArgumentException if expression is <code>false</code>
35 */
36 public static void isTrue(boolean expression, String message) {
37 if (!expression) {
38 throw new IllegalArgumentException(message);
39 }
40 }
41
42 /**
43 * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
44 * if the test result is <code>false</code>.
45 * <pre class="code">Assert.isTrue(i > 0);</pre>
46 * @param expression a boolean expression
47 * @throws IllegalArgumentException if expression is <code>false</code>
48 */
49 public static void isTrue(boolean expression) {
50 isTrue(expression, "[Assertion failed] - this expression must be true");
51 }
52
53 /**
54 * Assert that an object is <code>null</code> .
55 * <pre class="code">Assert.isNull(value, "The value must be null");</pre>
56 * @param object the object to check
57 * @param message the exception message to use if the assertion fails
58 * @throws IllegalArgumentException if the object is not <code>null</code>
59 */
60 public static void isNull(Object object, String message) {
61 if (object != null) {
62 throw new IllegalArgumentException(message);
63 }
64 }
65
66 /**
67 * Assert that an object is <code>null</code> .
68 * <pre class="code">Assert.isNull(value);</pre>
69 * @param object the object to check
70 * @throws IllegalArgumentException if the object is not <code>null</code>
71 */
72 public static void isNull(Object object) {
73 isNull(object, "[Assertion failed] - the object argument must be null");
74 }
75
76 /**
77 * Assert that an object is not <code>null</code> .
78 * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
79 * @param object the object to check
80 * @param message the exception message to use if the assertion fails
81 * @throws IllegalArgumentException if the object is <code>null</code>
82 */
83 public static void notNull(Object object, String message) {
84 if (object == null) {
85 throw new IllegalArgumentException(message);
86 }
87 }
88
89 /**
90 * Assert that an object is not <code>null</code> .
91 * <pre class="code">Assert.notNull(clazz);</pre>
92 * @param object the object to check
93 * @throws IllegalArgumentException if the object is <code>null</code>
94 */
95 public static void notNull(Object object) {
96 notNull(object, "[Assertion failed] - this argument is required; it must not be null");
97 }
98
99 /**
100 * Assert that the given String is not empty; that is,
101 * it must not be <code>null</code> and not the empty String.
102 * <pre class="code">Assert.hasLength(name, "Name must not be empty");</pre>
103 * @param text the String to check
104 * @param message the exception message to use if the assertion fails
105 * @see Strings#hasLength
106 */
107 public static void hasLength(String text, String message) {
108 if (!Strings.hasLength(text)) {
109 throw new IllegalArgumentException(message);
110 }
111 }
112
113 /**
114 * Assert that the given String is not empty; that is,
115 * it must not be <code>null</code> and not the empty String.
116 * <pre class="code">Assert.hasLength(name);</pre>
117 * @param text the String to check
118 * @see Strings#hasLength
119 */
120 public static void hasLength(String text) {
121 hasLength(text,
122 "[Assertion failed] - this String argument must have length; it must not be null or empty");
123 }
124
125 /**
126 * Assert that the given String has valid text content; that is, it must not
127 * be <code>null</code> and must contain at least one non-whitespace character.
128 * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
129 * @param text the String to check
130 * @param message the exception message to use if the assertion fails
131 * @see Strings#hasText
132 */
133 public static void hasText(String text, String message) {
134 if (!Strings.hasText(text)) {
135 throw new IllegalArgumentException(message);
136 }
137 }
138
139 /**
140 * Assert that the given String has valid text content; that is, it must not
141 * be <code>null</code> and must contain at least one non-whitespace character.
142 * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
143 * @param text the String to check
144 * @see Strings#hasText
145 */
146 public static void hasText(String text) {
147 hasText(text,
148 "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
149 }
150
151 /**
152 * Assert that the given text does not contain the given substring.
153 * <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre>
154 * @param textToSearch the text to search
155 * @param substring the substring to find within the text
156 * @param message the exception message to use if the assertion fails
157 */
158 public static void doesNotContain(String textToSearch, String substring, String message) {
159 if (Strings.hasLength(textToSearch) && Strings.hasLength(substring) &&
160 textToSearch.indexOf(substring) != -1) {
161 throw new IllegalArgumentException(message);
162 }
163 }
164
165 /**
166 * Assert that the given text does not contain the given substring.
167 * <pre class="code">Assert.doesNotContain(name, "rod");</pre>
168 * @param textToSearch the text to search
169 * @param substring the substring to find within the text
170 */
171 public static void doesNotContain(String textToSearch, String substring) {
172 doesNotContain(textToSearch, substring,
173 "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
174 }
175
176
177 /**
178 * Assert that an array has elements; that is, it must not be
179 * <code>null</code> and must have at least one element.
180 * <pre class="code">Assert.notEmpty(array, "The array must have elements");</pre>
181 * @param array the array to check
182 * @param message the exception message to use if the assertion fails
183 * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
184 */
185 public static void notEmpty(Object[] array, String message) {
186 if (Objects.isEmpty(array)) {
187 throw new IllegalArgumentException(message);
188 }
189 }
190
191 /**
192 * Assert that an array has elements; that is, it must not be
193 * <code>null</code> and must have at least one element.
194 * <pre class="code">Assert.notEmpty(array);</pre>
195 * @param array the array to check
196 * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
197 */
198 public static void notEmpty(Object[] array) {
199 notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
200 }
201
202 public static void notEmpty(byte[] array, String msg) {
203 if (Objects.isEmpty(array)) {
204 throw new IllegalArgumentException(msg);
205 }
206 }
207
208 /**
209 * Assert that an array has no null elements.
210 * Note: Does not complain if the array is empty!
211 * <pre class="code">Assert.noNullElements(array, "The array must have non-null elements");</pre>
212 * @param array the array to check
213 * @param message the exception message to use if the assertion fails
214 * @throws IllegalArgumentException if the object array contains a <code>null</code> element
215 */
216 public static void noNullElements(Object[] array, String message) {
217 if (array != null) {
218 for (int i = 0; i < array.length; i++) {
219 if (array[i] == null) {
220 throw new IllegalArgumentException(message);
221 }
222 }
223 }
224 }
225
226 /**
227 * Assert that an array has no null elements.
228 * Note: Does not complain if the array is empty!
229 * <pre class="code">Assert.noNullElements(array);</pre>
230 * @param array the array to check
231 * @throws IllegalArgumentException if the object array contains a <code>null</code> element
232 */
233 public static void noNullElements(Object[] array) {
234 noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
235 }
236
237 /**
238 * Assert that a collection has elements; that is, it must not be
239 * <code>null</code> and must have at least one element.
240 * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
241 * @param collection the collection to check
242 * @param message the exception message to use if the assertion fails
243 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
244 */
245 public static void notEmpty(Collection collection, String message) {
246 if (Collections.isEmpty(collection)) {
247 throw new IllegalArgumentException(message);
248 }
249 }
250
251 /**
252 * Assert that a collection has elements; that is, it must not be
253 * <code>null</code> and must have at least one element.
254 * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
255 * @param collection the collection to check
256 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
257 */
258 public static void notEmpty(Collection collection) {
259 notEmpty(collection,
260 "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
261 }
262
263 /**
264 * Assert that a Map has entries; that is, it must not be <code>null</code>
265 * and must have at least one entry.
266 * <pre class="code">Assert.notEmpty(map, "Map must have entries");</pre>
267 * @param map the map to check
268 * @param message the exception message to use if the assertion fails
269 * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
270 */
271 public static void notEmpty(Map map, String message) {
272 if (Collections.isEmpty(map)) {
273 throw new IllegalArgumentException(message);
274 }
275 }
276
277 /**
278 * Assert that a Map has entries; that is, it must not be <code>null</code>
279 * and must have at least one entry.
280 * <pre class="code">Assert.notEmpty(map);</pre>
281 * @param map the map to check
282 * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
283 */
284 public static void notEmpty(Map map) {
285 notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
286 }
287
288
289 /**
290 * Assert that the provided object is an instance of the provided class.
291 * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
292 * @param clazz the required class
293 * @param obj the object to check
294 * @throws IllegalArgumentException if the object is not an instance of clazz
295 * @see Class#isInstance
296 */
297 public static void isInstanceOf(Class clazz, Object obj) {
298 isInstanceOf(clazz, obj, "");
299 }
300
301 /**
302 * Assert that the provided object is an instance of the provided class.
303 * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
304 * @param type the type to check against
305 * @param obj the object to check
306 * @param message a message which will be prepended to the message produced by
307 * the function itself, and which may be used to provide context. It should
308 * normally end in a ": " or ". " so that the function generate message looks
309 * ok when prepended to it.
310 * @throws IllegalArgumentException if the object is not an instance of clazz
311 * @see Class#isInstance
312 */
313 public static void isInstanceOf(Class type, Object obj, String message) {
314 notNull(type, "Type to check against must not be null");
315 if (!type.isInstance(obj)) {
316 throw new IllegalArgumentException(message +
317 "Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
318 "] must be an instance of " + type);
319 }
320 }
321
322 /**
323 * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
324 * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
325 * @param superType the super type to check
326 * @param subType the sub type to check
327 * @throws IllegalArgumentException if the classes are not assignable
328 */
329 public static void isAssignable(Class superType, Class subType) {
330 isAssignable(superType, subType, "");
331 }
332
333 /**
334 * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
335 * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
336 * @param superType the super type to check against
337 * @param subType the sub type to check
338 * @param message a message which will be prepended to the message produced by
339 * the function itself, and which may be used to provide context. It should
340 * normally end in a ": " or ". " so that the function generate message looks
341 * ok when prepended to it.
342 * @throws IllegalArgumentException if the classes are not assignable
343 */
344 public static void isAssignable(Class superType, Class subType, String message) {
345 notNull(superType, "Type to check against must not be null");
346 if (subType == null || !superType.isAssignableFrom(subType)) {
347 throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
348 }
349 }
350
351
352 /**
353 * Assert a boolean expression, throwing <code>IllegalStateException</code>
354 * if the test result is <code>false</code>. Call isTrue if you wish to
355 * throw IllegalArgumentException on an assertion failure.
356 * <pre class="code">Assert.state(id == null, "The id property must not already be initialized");</pre>
357 * @param expression a boolean expression
358 * @param message the exception message to use if the assertion fails
359 * @throws IllegalStateException if expression is <code>false</code>
360 */
361 public static void state(boolean expression, String message) {
362 if (!expression) {
363 throw new IllegalStateException(message);
364 }
365 }
366
367 /**
368 * Assert a boolean expression, throwing {@link IllegalStateException}
369 * if the test result is <code>false</code>.
370 * <p>Call {@link #isTrue(boolean)} if you wish to
371 * throw {@link IllegalArgumentException} on an assertion failure.
372 * <pre class="code">Assert.state(id == null);</pre>
373 * @param expression a boolean expression
374 * @throws IllegalStateException if the supplied expression is <code>false</code>
375 */
376 public static void state(boolean expression) {
377 state(expression, "[Assertion failed] - this state invariant must be true");
378 }
379
380 }
381