|
| 1 | +package jazzyframework.guard; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | + |
| 5 | +/** |
| 6 | + * A utility class providing static methods for performing common validation checks. |
| 7 | + * This class follows the guard clause pattern and throws {@link ValidationException} |
| 8 | + * when validation fails. |
| 9 | + * |
| 10 | + * <p>All methods return the validated value, allowing for fluent usage in assignments |
| 11 | + * and method chaining scenarios.</p> |
| 12 | + * |
| 13 | + * <p>Example usage:</p> |
| 14 | + * <pre>{@code |
| 15 | + * String username = Ensure.notBlank(user.getUsername(), "Username cannot be blank"); |
| 16 | + * List<Item> items = Ensure.notEmpty(itemList, "Item list cannot be empty"); |
| 17 | + * int age = Ensure.inRange(user.getAge(), 0, 150, "Age must be between 0 and 150"); |
| 18 | + * }</pre> |
| 19 | + * |
| 20 | + */ |
| 21 | +public final class Ensure { |
| 22 | + /** |
| 23 | + * Private constructor to prevent instantiation of this utility class. |
| 24 | + */ |
| 25 | + private Ensure() { |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Ensures that the specified object is not null. |
| 31 | + * |
| 32 | + * @param <T> the type of the object to check |
| 33 | + * @param obj the object to check for null |
| 34 | + * @param message the detail message for the exception if validation fails |
| 35 | + * @return the validated object (guaranteed to be non-null) |
| 36 | + * @throws ValidationException if the object is null |
| 37 | + */ |
| 38 | + public static <T> T notNull(T obj, String message) { |
| 39 | + if (obj == null) { |
| 40 | + throw new ValidationException(message); |
| 41 | + } |
| 42 | + return obj; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Ensures that the specified string is not null and not blank. |
| 47 | + * A string is considered blank if it consists only of whitespace characters. |
| 48 | + * |
| 49 | + * @param str the string to check |
| 50 | + * @param message the detail message for the exception if validation fails |
| 51 | + * @return the validated string (guaranteed to be non-null and non-blank) |
| 52 | + * @throws ValidationException if the string is null or blank |
| 53 | + */ |
| 54 | + public static String notBlank(String str, String message) { |
| 55 | + if (str == null || str.isBlank()) { |
| 56 | + throw new ValidationException(message); |
| 57 | + } |
| 58 | + return str; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Ensures that the specified collection is not null and not empty. |
| 63 | + * |
| 64 | + * @param <T> the type of the collection |
| 65 | + * @param collection the collection to check |
| 66 | + * @param message the detail message for the exception if validation fails |
| 67 | + * @return the validated collection (guaranteed to be non-null and non-empty) |
| 68 | + * @throws ValidationException if the collection is null or empty |
| 69 | + */ |
| 70 | + public static <T extends Collection<?>> T notEmpty(T collection, String message) { |
| 71 | + if (collection == null || collection.isEmpty()) { |
| 72 | + throw new ValidationException(message); |
| 73 | + } |
| 74 | + return collection; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Ensures that the specified array is not null and not empty. |
| 79 | + * |
| 80 | + * @param <T> the component type of the array |
| 81 | + * @param array the array to check |
| 82 | + * @param message the detail message for the exception if validation fails |
| 83 | + * @return the validated array (guaranteed to be non-null and non-empty) |
| 84 | + * @throws ValidationException if the array is null or has zero length |
| 85 | + */ |
| 86 | + public static <T> T[] notEmpty(T[] array, String message) { |
| 87 | + if (array == null || array.length == 0) { |
| 88 | + throw new ValidationException(message); |
| 89 | + } |
| 90 | + return array; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Ensures that the specified integer value is within the given range (inclusive). |
| 95 | + * |
| 96 | + * @param value the value to check |
| 97 | + * @param min the minimum allowed value (inclusive) |
| 98 | + * @param max the maximum allowed value (inclusive) |
| 99 | + * @param message the detail message for the exception if validation fails |
| 100 | + * @return the validated value (guaranteed to be within the specified range) |
| 101 | + * @throws ValidationException if the value is outside the specified range |
| 102 | + */ |
| 103 | + public static int inRange(int value, int min, int max, String message) { |
| 104 | + if (value < min || value > max) { |
| 105 | + throw new ValidationException(message); |
| 106 | + } |
| 107 | + return value; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Ensures that the specified integer value is not negative (greater than or equal to zero). |
| 112 | + * |
| 113 | + * @param value the value to check |
| 114 | + * @param message the detail message for the exception if validation fails |
| 115 | + * @return the validated value (guaranteed to be non-negative) |
| 116 | + * @throws ValidationException if the value is negative |
| 117 | + */ |
| 118 | + public static int notNegative(int value, String message) { |
| 119 | + if (value < 0) { |
| 120 | + throw new ValidationException(message); |
| 121 | + } |
| 122 | + return value; |
| 123 | + } |
| 124 | +} |
0 commit comments