1 16 17 package org.springframework.util; 18 19 import java.util.Collection ; 20 import java.util.Map ; 21 22 54 public abstract class Assert { 55 56 64 public static void isTrue(boolean expression, String message) { 65 if (!expression) { 66 throw new IllegalArgumentException (message); 67 } 68 } 69 70 77 public static void isTrue(boolean expression) { 78 isTrue(expression, "[Assertion failed] - this expression must be true"); 79 } 80 81 88 public static void isNull(Object object, String message) { 89 if (object != null) { 90 throw new IllegalArgumentException (message); 91 } 92 } 93 94 100 public static void isNull(Object object) { 101 isNull(object, "[Assertion failed] - the object argument must be null"); 102 } 103 104 111 public static void notNull(Object object, String message) { 112 if (object == null) { 113 throw new IllegalArgumentException (message); 114 } 115 } 116 117 123 public static void notNull(Object object) { 124 notNull(object, "[Assertion failed] - this argument is required; it must not null"); 125 } 126 127 134 public static void hasLength(String text, String message) { 135 if (!StringUtils.hasLength(text)) { 136 throw new IllegalArgumentException (message); 137 } 138 } 139 140 146 public static void hasLength(String text) { 147 hasLength(text, 148 "[Assertion failed] - this String argument must have length; it must not be <code>null</code> or empty"); 149 } 150 151 159 public static void hasText(String text, String message) { 160 if (!StringUtils.hasText(text)) { 161 throw new IllegalArgumentException (message); 162 } 163 } 164 165 172 public static void hasText(String text) { 173 hasText(text, 174 "[Assertion failed] - this String argument must have text; it must not be <code>null</code>, empty, or blank"); 175 } 176 177 184 public static void doesNotContain(String textToSearch, String substring, String message) { 185 if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && 186 textToSearch.indexOf(substring) != -1) { 187 throw new IllegalArgumentException (message); 188 } 189 } 190 191 197 public static void doesNotContain(String textToSearch, String substring) { 198 doesNotContain(textToSearch, substring, 199 "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); 200 } 201 202 203 211 public static void notEmpty(Object [] array, String message) { 212 if (ObjectUtils.isEmpty(array)) { 213 throw new IllegalArgumentException (message); 214 } 215 } 216 217 224 public static void notEmpty(Object [] array) { 225 notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); 226 } 227 228 236 public static void notEmpty(Collection collection, String message) { 237 if (CollectionUtils.isEmpty(collection)) { 238 throw new IllegalArgumentException (message); 239 } 240 } 241 242 249 public static void notEmpty(Collection collection) { 250 notEmpty(collection, 251 "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); 252 } 253 254 262 public static void notEmpty(Map map, String message) { 263 if (CollectionUtils.isEmpty(map)) { 264 throw new IllegalArgumentException (message); 265 } 266 } 267 268 275 public static void notEmpty(Map map) { 276 notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); 277 } 278 279 280 288 public static void isInstanceOf(Class clazz, Object obj) { 289 isInstanceOf(clazz, obj, ""); 290 } 291 292 304 public static void isInstanceOf(Class type, Object obj, String message) { 305 notNull(type, "Type to check against must not be null"); 306 if (!type.isInstance(obj)) { 307 throw new IllegalArgumentException (message + 308 "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + 309 "] must be an instance of " + type); 310 } 311 } 312 313 320 public static void isAssignable(Class superType, Class subType) { 321 isAssignable(superType, subType, ""); 322 } 323 324 335 public static void isAssignable(Class superType, Class subType, String message) { 336 notNull(superType, "Type to check against must not be null"); 337 if (subType == null || !superType.isAssignableFrom(subType)) { 338 throw new IllegalArgumentException (message + subType + " is not assignable to " + superType); 339 } 340 } 341 342 343 352 public static void state(boolean expression, String message) { 353 if (!expression) { 354 throw new IllegalStateException (message); 355 } 356 } 357 358 367 public static void state(boolean expression) { 368 state(expression, "[Assertion failed] - this state invariant must be true"); 369 } 370 371 } 372 | Popular Tags |