1 16 package org.apache.commons.lang; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 40 public class Validate { 41 43 46 public Validate() { 47 } 48 49 52 73 public static void isTrue(boolean expression, String message, Object value) { 74 if (expression == false) { 75 throw new IllegalArgumentException (message + value); 76 } 77 } 78 79 99 public static void isTrue(boolean expression, String message, long value) { 100 if (expression == false) { 101 throw new IllegalArgumentException (message + value); 102 } 103 } 104 105 126 public static void isTrue(boolean expression, String message, double value) { 127 if (expression == false) { 128 throw new IllegalArgumentException (message + value); 129 } 130 } 131 132 153 public static void isTrue(boolean expression, String message) { 154 if (expression == false) { 155 throw new IllegalArgumentException (message); 156 } 157 } 158 159 177 public static void isTrue(boolean expression) { 178 if (expression == false) { 179 throw new IllegalArgumentException ("The validated expression is false"); 180 } 181 } 182 183 186 199 public static void notNull(Object object, String message) { 200 if (object == null) { 201 throw new IllegalArgumentException (message); 202 } 203 } 204 205 218 public static void notNull(Object object) { 219 if (object == null) { 220 throw new IllegalArgumentException ("The validated object is null"); 221 } 222 } 223 224 227 239 public static void notEmpty(Object [] array, String message) { 240 if (array == null || array.length == 0) { 241 throw new IllegalArgumentException (message); 242 } 243 } 244 245 258 public static void notEmpty(Object [] array) { 259 if (array == null || array.length == 0) { 260 throw new IllegalArgumentException ("The validated array is empty"); 261 } 262 } 263 264 267 279 public static void notEmpty(Collection collection, String message) { 280 if (collection == null || collection.size() == 0) { 281 throw new IllegalArgumentException (message); 282 } 283 } 284 285 298 public static void notEmpty(Collection collection) { 299 if (collection == null || collection.size() == 0) { 300 throw new IllegalArgumentException ("The validated collection is empty"); 301 } 302 } 303 304 307 319 public static void notEmpty(Map map, String message) { 320 if (map == null || map.size() == 0) { 321 throw new IllegalArgumentException (message); 322 } 323 } 324 325 338 public static void notEmpty(Map map) { 339 if (map == null || map.size() == 0) { 340 throw new IllegalArgumentException ("The validated map is empty"); 341 } 342 } 343 344 347 359 public static void notEmpty(String string, String message) { 360 if (string == null || string.length() == 0) { 361 throw new IllegalArgumentException (message); 362 } 363 } 364 365 378 public static void notEmpty(String string) { 379 if (string == null || string.length() == 0) { 380 throw new IllegalArgumentException ("The validated string is empty"); 381 } 382 } 383 384 387 404 public static void noNullElements(Object [] array, String message) { 405 Validate.notNull(array); 406 for (int i = 0; i < array.length; i++) { 407 if (array[i] == null) { 408 throw new IllegalArgumentException (message); 409 } 410 } 411 } 412 413 431 public static void noNullElements(Object [] array) { 432 Validate.notNull(array); 433 for (int i = 0; i < array.length; i++) { 434 if (array[i] == null) { 435 throw new IllegalArgumentException ("The validated array contains null element at index: " + i); 436 } 437 } 438 } 439 440 443 460 public static void noNullElements(Collection collection, String message) { 461 Validate.notNull(collection); 462 for (Iterator it = collection.iterator(); it.hasNext();) { 463 if (it.next() == null) { 464 throw new IllegalArgumentException (message); 465 } 466 } 467 } 468 469 486 public static void noNullElements(Collection collection) { 487 Validate.notNull(collection); 488 int i = 0; 489 for (Iterator it = collection.iterator(); it.hasNext(); i++) { 490 if (it.next() == null) { 491 throw new IllegalArgumentException ("The validated collection contains null element at index: " + i); 492 } 493 } 494 } 495 496 510 public static void allElementsOfType(Collection collection, Class clazz, String message) { 511 Validate.notNull(collection); 512 Validate.notNull(clazz); 513 for (Iterator it = collection.iterator(); it.hasNext(); ) { 514 if (clazz.isInstance(it.next()) == false) { 515 throw new IllegalArgumentException (message); 516 } 517 } 518 } 519 520 540 public static void allElementsOfType(Collection collection, Class clazz) { 541 Validate.notNull(collection); 542 Validate.notNull(clazz); 543 int i = 0; 544 for (Iterator it = collection.iterator(); it.hasNext(); i++) { 545 if (clazz.isInstance(it.next()) == false) { 546 throw new IllegalArgumentException ("The validated collection contains an element not of type " 547 + clazz.getName() + " at index: " + i); 548 } 549 } 550 } 551 552 } 553 | Popular Tags |