001/** 002 * Copyright 2010-2013 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; 022import org.kuali.common.util.enc.EncUtils; 023 024import com.google.common.base.Optional; 025import com.google.common.collect.ImmutableList; 026 027public abstract class Assert extends org.springframework.util.Assert { 028 029 private static final String NO_NULLS = "null not allowed"; 030 private static final String NO_BLANKS = "blank strings not allowed"; 031 032 /** 033 * Assert that the optional contains a non-null value 034 */ 035 public static void present(Optional<?> optional) { 036 present(optional, "optional value is required"); 037 } 038 039 /** 040 * Assert that the optional contains a non-null value 041 */ 042 public static void present(Optional<?> optional, String msg) { 043 isTrue(optional.isPresent()); 044 } 045 046 /** 047 * Assert that <code>text</code> is concealed 048 */ 049 public static void concealed(String text) { 050 isTrue(Str.isConcealed(text), "text must be concealed"); 051 } 052 053 /** 054 * Assert that <code>text</code> is not concealed 055 */ 056 public static void notConcealed(String text) { 057 isFalse(Str.isConcealed(text), "text is already concealed"); 058 } 059 060 /** 061 * Assert that <code>text</code> is encrypted 062 */ 063 public static void encrypted(String text) { 064 isTrue(EncUtils.isEncrypted(text), "text must be encrypted"); 065 } 066 067 /** 068 * Assert that <code>text</code> is not encrypted 069 */ 070 public static void notEncrypted(String text) { 071 isFalse(EncUtils.isEncrypted(text), "text is already encrypted"); 072 } 073 074 /** 075 * Assert that <code>text</code> is not encrypted 076 * 077 * @deprecated use notEncrypted instead 078 * @see notEncrypted 079 */ 080 @Deprecated 081 public static void decrypted(String text) { 082 notEncrypted(text); 083 } 084 085 /** 086 * Assert that none of the strings are encrypted 087 * 088 * @deprecated use notEncrypted instead 089 * @see notEncrypted 090 */ 091 @Deprecated 092 public static void decrypted(List<String> strings) { 093 notEncrypted(strings); 094 } 095 096 /** 097 * Assert that none of the strings in the list are encrypted 098 */ 099 public static void notEncrypted(List<String> strings) { 100 for (String string : strings) { 101 notEncrypted(string); 102 } 103 } 104 105 /** 106 * Assert that <code>port</code> is >= 0 and <= 65535 107 */ 108 public static void isPort(int port) { 109 isTrue(port >= 0 && port <= 65535, "Port must be a number between 0 and 65535"); 110 } 111 112 /** 113 * Assert that all of the numbers in the array are greater than or equal to zero 114 */ 115 public static void noNegatives(int... numbers) { 116 for (int number : numbers) { 117 notNegative(number); 118 } 119 } 120 121 /** 122 * Assert that all of the numbers in the array are greater than or equal to zero 123 */ 124 public static void noNegatives(long... numbers) { 125 for (long number : numbers) { 126 notNegative(number); 127 } 128 } 129 130 /** 131 * Assert that all of the numbers in the array are less than or equal to zero 132 */ 133 public static void noPositives(int... numbers) { 134 for (int number : numbers) { 135 notPositive(number); 136 } 137 } 138 139 /** 140 * Assert that <code>i</code> is greater than or equal to zero 141 */ 142 public static void notNegative(int i) { 143 isTrue(i >= 0, i + " is negative"); 144 } 145 146 /** 147 * Assert that <code>i</code> is greater than or equal to zero 148 */ 149 public static void notNegative(long i) { 150 isTrue(i >= 0, i + " is negative"); 151 } 152 153 /** 154 * Assert that <code>i</code> is less than or equal to zero 155 */ 156 public static void notPositive(int i) { 157 isTrue(i <= 0, i + " is positive"); 158 } 159 160 /** 161 * Assert that <code>i</code> is greater than zero 162 */ 163 public static void positive(int i) { 164 isTrue(i > 0, i + " is not a positive integer"); 165 } 166 167 /** 168 * Assert that <code>i</code> is greater than zero 169 */ 170 public static void positive(long i) { 171 isTrue(i > 0, i + " is not a positive long"); 172 } 173 174 /** 175 * Assert that <code>i</code> is less than zero 176 */ 177 public static void negative(int i) { 178 isTrue(i < 0, i + " is not a negative integer"); 179 } 180 181 /** 182 * Assert that <code>i</code> is zero 183 */ 184 public static void zero(int i) { 185 isTrue(i == 0, i + " is not zero"); 186 } 187 188 public static void exists(String location) { 189 exists(location, "[" + location + "] does not exist"); 190 } 191 192 public static void exists(String location, String message) { 193 isTrue(LocationUtils.exists(location), message); 194 } 195 196 public static void isExistingDir(File dir) { 197 isExistingDir(dir, "[" + dir + "] is not an existing directory"); 198 } 199 200 public static void isExistingDir(File dir, String message) { 201 exists(dir, message); 202 isTrue(dir.isDirectory(), message); 203 } 204 205 public static void exists(File file) { 206 exists(file, "[" + file + "] does not exist"); 207 } 208 209 public static void exists(File file, String message) { 210 isTrue(file.exists(), message); 211 } 212 213 public static void isOdd(int i) { 214 isOdd(i, "[" + i + "] is not an odd number"); 215 } 216 217 public static void isOdd(int i, String message) { 218 isTrue(i % 2 != 0, message); 219 } 220 221 public static void isEven(int i) { 222 isEven(i, "[" + i + "] is not an even number"); 223 } 224 225 public static void isEven(int i, String message) { 226 isTrue(i % 2 == 0, message); 227 } 228 229 public static void isFalse(boolean condition) { 230 isTrue(!condition); 231 } 232 233 public static void isFalse(boolean condition, String message) { 234 isTrue(!condition, message); 235 } 236 237 public static void notBlank(String string) { 238 isFalse(StringUtils.isBlank(string)); 239 } 240 241 public static void noBlanks(String... strings) { 242 noBlanksWithMsg(NO_BLANKS, strings); 243 } 244 245 public static void present(Optional<?>... optionals) { 246 for (Optional<?> optional : optionals) { 247 Assert.isTrue(optional.isPresent(), "Optional is required"); 248 } 249 } 250 251 /** 252 * Assert that if the optional string is present it is not blank 253 */ 254 public static void noBlanks(Optional<String> string) { 255 noBlankOptionals(ImmutableList.of(string)); 256 } 257 258 public static void noBlanks(Optional<String> s1, Optional<String> s2) { 259 noBlankOptionals(ImmutableList.of(s1, s2)); 260 } 261 262 public static void noBlanks(Optional<String> s1, Optional<String> s2, Optional<String> s3) { 263 noBlankOptionals(ImmutableList.of(s1, s2, s3)); 264 } 265 266 public static void noBlanks(Optional<String> s1, Optional<String> s2, Optional<String> s3, Optional<String> s4) { 267 noBlankOptionals(ImmutableList.of(s1, s2, s3, s4)); 268 } 269 270 public static void noBlankOptionals(List<Optional<String>> optionals) { 271 for (Optional<String> optional : optionals) { 272 if (optional.isPresent()) { 273 noBlanks(optional.get()); 274 } 275 } 276 } 277 278 /** 279 * @deprecated Use noBlankOptionals instead 280 * @see noBlankOptionals 281 */ 282 @Deprecated 283 public static void noBlanksIfPresent(Optional<String>... optionals) { 284 for (Optional<String> optional : optionals) { 285 if (optional.isPresent()) { 286 noBlanks(optional.get()); 287 } 288 } 289 } 290 291 /** 292 * Assert that <code>strings</code> is not null and that none of the elements are blank 293 */ 294 public static void noBlanks(List<String> strings) { 295 Assert.noNulls(strings); 296 for (String string : strings) { 297 noBlanksWithMsg(NO_BLANKS, string); 298 } 299 } 300 301 public static void noNullStrings(String... strings) { 302 notNull((Object) strings); 303 for (String string : strings) { 304 notNull(string, NO_NULLS); 305 } 306 } 307 308 public static void noNulls(Object... objects) { 309 noNullsWithMsg(NO_NULLS, objects); 310 } 311 312 public static void noNullsWithMsg(String msg, Object... objects) { 313 for (Object object : objects) { 314 notNull(object, msg); 315 } 316 } 317 318 public static void noBlanksWithMsg(String msg, String... strings) { 319 for (String string : strings) { 320 isFalse(StringUtils.isBlank(string), msg); 321 } 322 } 323 324 @Deprecated 325 public static void notNull(Object... objects) { 326 for (Object object : objects) { 327 notNull(object); 328 } 329 } 330 331 @Deprecated 332 public static void notBlank(String... strings) { 333 noBlanksWithMsg(NO_BLANKS, strings); 334 } 335 336 @Deprecated 337 public static void noNulls(String msg, Object... objects) { 338 for (Object object : objects) { 339 notNull(object, msg); 340 } 341 } 342 343}