001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import java.io.File;
019import java.util.List;
020
021import org.apache.commons.lang3.StringUtils;
022
023import com.google.common.base.Optional;
024import com.google.common.collect.ImmutableList;
025
026public abstract class Assert extends org.springframework.util.Assert {
027
028        private static final String NO_NULLS = "null not allowed";
029        private static final String NO_BLANKS = "blank strings not allowed";
030
031        /**
032         * Assert that the optional contains a non-null value
033         */
034        public static void present(Optional<?> optional) {
035                present(optional, "optional value is required");
036        }
037
038        /**
039         * Assert that the optional contains a non-null value
040         */
041        public static void present(Optional<?> optional, String msg) {
042                isTrue(optional.isPresent());
043        }
044
045        /**
046         * Assert that <code>text</code> is concealed
047         */
048        public static void concealed(String text) {
049                isTrue(Str.isConcealed(text), "text must be concealed");
050        }
051
052        /**
053         * Assert that <code>text</code> is not concealed
054         */
055        public static void notConcealed(String text) {
056                isFalse(Str.isConcealed(text), "text is already concealed");
057        }
058
059        /**
060         * Assert that <code>text</code> is encrypted
061         * 
062         * @deprecated
063         */
064        @Deprecated
065        public static void encrypted(String text) {
066                isTrue(org.kuali.common.util.enc.EncUtils.isEncrypted(text), "text must be encrypted");
067        }
068
069        /**
070         * Assert that <code>text</code> is not encrypted
071         * 
072         * @deprecated
073         */
074        @Deprecated
075        public static void notEncrypted(String text) {
076                isFalse(org.kuali.common.util.enc.EncUtils.isEncrypted(text), "text is already encrypted");
077        }
078
079        /**
080         * Assert that <code>text</code> is not encrypted
081         * 
082         * @deprecated use notEncrypted instead
083         * @see notEncrypted
084         */
085        @Deprecated
086        public static void decrypted(String text) {
087                notEncrypted(text);
088        }
089
090        /**
091         * Assert that none of the strings are encrypted
092         * 
093         * @deprecated use notEncrypted instead
094         * @see notEncrypted
095         */
096        @Deprecated
097        public static void decrypted(List<String> strings) {
098                notEncrypted(strings);
099        }
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}