View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.common.util;
17  
18  import java.io.File;
19  import java.util.List;
20  
21  import org.apache.commons.lang3.StringUtils;
22  
23  import com.google.common.base.Optional;
24  import com.google.common.collect.ImmutableList;
25  
26  public abstract class Assert extends org.springframework.util.Assert {
27  
28  	private static final String NO_NULLS = "null not allowed";
29  	private static final String NO_BLANKS = "blank strings not allowed";
30  
31  	/**
32  	 * Assert that the optional contains a non-null value
33  	 */
34  	public static void present(Optional<?> optional) {
35  		present(optional, "optional value is required");
36  	}
37  
38  	/**
39  	 * Assert that the optional contains a non-null value
40  	 */
41  	public static void present(Optional<?> optional, String msg) {
42  		isTrue(optional.isPresent());
43  	}
44  
45  	/**
46  	 * Assert that <code>text</code> is concealed
47  	 */
48  	public static void concealed(String text) {
49  		isTrue(Str.isConcealed(text), "text must be concealed");
50  	}
51  
52  	/**
53  	 * Assert that <code>text</code> is not concealed
54  	 */
55  	public static void notConcealed(String text) {
56  		isFalse(Str.isConcealed(text), "text is already concealed");
57  	}
58  
59  	/**
60  	 * Assert that <code>text</code> is encrypted
61  	 * 
62  	 * @deprecated
63  	 */
64  	@Deprecated
65  	public static void encrypted(String text) {
66  		isTrue(org.kuali.common.util.enc.EncUtils.isEncrypted(text), "text must be encrypted");
67  	}
68  
69  	/**
70  	 * Assert that <code>text</code> is not encrypted
71  	 * 
72  	 * @deprecated
73  	 */
74  	@Deprecated
75  	public static void notEncrypted(String text) {
76  		isFalse(org.kuali.common.util.enc.EncUtils.isEncrypted(text), "text is already encrypted");
77  	}
78  
79  	/**
80  	 * Assert that <code>text</code> is not encrypted
81  	 * 
82  	 * @deprecated use notEncrypted instead
83  	 * @see notEncrypted
84  	 */
85  	@Deprecated
86  	public static void decrypted(String text) {
87  		notEncrypted(text);
88  	}
89  
90  	/**
91  	 * Assert that none of the strings are encrypted
92  	 * 
93  	 * @deprecated use notEncrypted instead
94  	 * @see notEncrypted
95  	 */
96  	@Deprecated
97  	public static void decrypted(List<String> strings) {
98  		notEncrypted(strings);
99  	}
100 
101 	/**
102 	 * Assert that none of the strings in the list are encrypted
103 	 */
104 	public static void notEncrypted(List<String> strings) {
105 		for (String string : strings) {
106 			notEncrypted(string);
107 		}
108 	}
109 
110 	/**
111 	 * Assert that <code>port</code> is >= 0 and <= 65535
112 	 */
113 	public static void isPort(int port) {
114 		isTrue(port >= 0 && port <= 65535, "Port must be a number between 0 and 65535");
115 	}
116 
117 	/**
118 	 * Assert that all of the numbers in the array are greater than or equal to zero
119 	 */
120 	public static void noNegatives(int... numbers) {
121 		for (int number : numbers) {
122 			notNegative(number);
123 		}
124 	}
125 
126 	/**
127 	 * Assert that all of the numbers in the array are greater than or equal to zero
128 	 */
129 	public static void noNegatives(long... numbers) {
130 		for (long number : numbers) {
131 			notNegative(number);
132 		}
133 	}
134 
135 	/**
136 	 * Assert that all of the numbers in the array are less than or equal to zero
137 	 */
138 	public static void noPositives(int... numbers) {
139 		for (int number : numbers) {
140 			notPositive(number);
141 		}
142 	}
143 
144 	/**
145 	 * Assert that <code>i</code> is greater than or equal to zero
146 	 */
147 	public static void notNegative(int i) {
148 		isTrue(i >= 0, i + " is negative");
149 	}
150 
151 	/**
152 	 * Assert that <code>i</code> is greater than or equal to zero
153 	 */
154 	public static void notNegative(long i) {
155 		isTrue(i >= 0, i + " is negative");
156 	}
157 
158 	/**
159 	 * Assert that <code>i</code> is less than or equal to zero
160 	 */
161 	public static void notPositive(int i) {
162 		isTrue(i <= 0, i + " is positive");
163 	}
164 
165 	/**
166 	 * Assert that <code>i</code> is greater than zero
167 	 */
168 	public static void positive(int i) {
169 		isTrue(i > 0, i + " is not a positive integer");
170 	}
171 
172 	/**
173 	 * Assert that <code>i</code> is greater than zero
174 	 */
175 	public static void positive(long i) {
176 		isTrue(i > 0, i + " is not a positive long");
177 	}
178 
179 	/**
180 	 * Assert that <code>i</code> is less than zero
181 	 */
182 	public static void negative(int i) {
183 		isTrue(i < 0, i + " is not a negative integer");
184 	}
185 
186 	/**
187 	 * Assert that <code>i</code> is zero
188 	 */
189 	public static void zero(int i) {
190 		isTrue(i == 0, i + " is not zero");
191 	}
192 
193 	public static void exists(String location) {
194 		exists(location, "[" + location + "] does not exist");
195 	}
196 
197 	public static void exists(String location, String message) {
198 		isTrue(LocationUtils.exists(location), message);
199 	}
200 
201 	public static void isExistingDir(File dir) {
202 		isExistingDir(dir, "[" + dir + "] is not an existing directory");
203 	}
204 
205 	public static void isExistingDir(File dir, String message) {
206 		exists(dir, message);
207 		isTrue(dir.isDirectory(), message);
208 	}
209 
210 	public static void exists(File file) {
211 		exists(file, "[" + file + "] does not exist");
212 	}
213 
214 	public static void exists(File file, String message) {
215 		isTrue(file.exists(), message);
216 	}
217 
218 	public static void isOdd(int i) {
219 		isOdd(i, "[" + i + "] is not an odd number");
220 	}
221 
222 	public static void isOdd(int i, String message) {
223 		isTrue(i % 2 != 0, message);
224 	}
225 
226 	public static void isEven(int i) {
227 		isEven(i, "[" + i + "] is not an even number");
228 	}
229 
230 	public static void isEven(int i, String message) {
231 		isTrue(i % 2 == 0, message);
232 	}
233 
234 	public static void isFalse(boolean condition) {
235 		isTrue(!condition);
236 	}
237 
238 	public static void isFalse(boolean condition, String message) {
239 		isTrue(!condition, message);
240 	}
241 
242 	public static void notBlank(String string) {
243 		isFalse(StringUtils.isBlank(string));
244 	}
245 
246 	public static void noBlanks(String... strings) {
247 		noBlanksWithMsg(NO_BLANKS, strings);
248 	}
249 
250 	public static void present(Optional<?>... optionals) {
251 		for (Optional<?> optional : optionals) {
252 			Assert.isTrue(optional.isPresent(), "Optional is required");
253 		}
254 	}
255 
256 	/**
257 	 * Assert that if the optional string is present it is not blank
258 	 */
259 	public static void noBlanks(Optional<String> string) {
260 		noBlankOptionals(ImmutableList.of(string));
261 	}
262 
263 	public static void noBlanks(Optional<String> s1, Optional<String> s2) {
264 		noBlankOptionals(ImmutableList.of(s1, s2));
265 	}
266 
267 	public static void noBlanks(Optional<String> s1, Optional<String> s2, Optional<String> s3) {
268 		noBlankOptionals(ImmutableList.of(s1, s2, s3));
269 	}
270 
271 	public static void noBlanks(Optional<String> s1, Optional<String> s2, Optional<String> s3, Optional<String> s4) {
272 		noBlankOptionals(ImmutableList.of(s1, s2, s3, s4));
273 	}
274 
275 	public static void noBlankOptionals(List<Optional<String>> optionals) {
276 		for (Optional<String> optional : optionals) {
277 			if (optional.isPresent()) {
278 				noBlanks(optional.get());
279 			}
280 		}
281 	}
282 
283 	/**
284 	 * @deprecated Use noBlankOptionals instead
285 	 * @see noBlankOptionals
286 	 */
287 	@Deprecated
288 	@SafeVarargs
289 	public static void noBlanksIfPresent(Optional<String>... optionals) {
290 		for (Optional<String> optional : optionals) {
291 			if (optional.isPresent()) {
292 				noBlanks(optional.get());
293 			}
294 		}
295 	}
296 
297 	/**
298 	 * Assert that <code>strings</code> is not null and that none of the elements are blank
299 	 */
300 	public static void noBlanks(List<String> strings) {
301 		Assert.noNulls(strings);
302 		for (String string : strings) {
303 			noBlanksWithMsg(NO_BLANKS, string);
304 		}
305 	}
306 
307 	public static void noNullStrings(String... strings) {
308 		notNull((Object) strings);
309 		for (String string : strings) {
310 			notNull(string, NO_NULLS);
311 		}
312 	}
313 
314 	public static void noNulls(Object... objects) {
315 		noNullsWithMsg(NO_NULLS, objects);
316 	}
317 
318 	public static void noNullsWithMsg(String msg, Object... objects) {
319 		for (Object object : objects) {
320 			notNull(object, msg);
321 		}
322 	}
323 
324 	public static void noBlanksWithMsg(String msg, String... strings) {
325 		for (String string : strings) {
326 			isFalse(StringUtils.isBlank(string), msg);
327 		}
328 	}
329 
330 	@Deprecated
331 	public static void notNull(Object... objects) {
332 		for (Object object : objects) {
333 			notNull(object);
334 		}
335 	}
336 
337 	@Deprecated
338 	public static void notBlank(String... strings) {
339 		noBlanksWithMsg(NO_BLANKS, strings);
340 	}
341 
342 	@Deprecated
343 	public static void noNulls(String msg, Object... objects) {
344 		for (Object object : objects) {
345 			notNull(object, msg);
346 		}
347 	}
348 
349 }