|
| 1 | +package com.javaquery.util; |
| 2 | + |
| 3 | +import com.javaquery.util.collection.Collections; |
| 4 | + |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.function.Supplier; |
| 8 | + |
| 9 | +/** |
| 10 | + * {@code Assert} is a collection of utility methods that helps asserting conditions and throw exception if condition fails |
| 11 | + * |
| 12 | + * @author Harshit |
| 13 | + * @since 1.0 |
| 14 | + */ |
| 15 | +public final class Assert { |
| 16 | + |
| 17 | + private Assert() { |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * <em>Assert</em> that {@code object} is {@code not null}. |
| 22 | + * |
| 23 | + * @param object object to check |
| 24 | + * @param <T> Type of the exception to be thrown |
| 25 | + * @param exceptionSupplier supplier of a exception to be thrown |
| 26 | + * @throws T if {@code object} is {@code null} |
| 27 | + * @throws NullPointerException if {@code object} is {@code null} and {@code exceptionSupplier} is {@code null} |
| 28 | + */ |
| 29 | + public static <T extends Throwable> void nonNull(Object object, Supplier<T> exceptionSupplier) throws T { |
| 30 | + if (object == null) { |
| 31 | + throw exceptionSupplier.get(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * <em>Assert</em> that {@code object} is {@code null}. |
| 37 | + * |
| 38 | + * @param object object to check |
| 39 | + * @param <T> Type of the exception to be thrown |
| 40 | + * @param exceptionSupplier supplier of a exception to throw |
| 41 | + * @throws T if {@code object} is {@code not null} |
| 42 | + * @throws NullPointerException if {@code object} is {@code not null} and {@code exceptionSupplier} is {@code null} |
| 43 | + */ |
| 44 | + public static <T extends Throwable> void isNull(Object object, Supplier<T> exceptionSupplier) throws T { |
| 45 | + if (object != null) { |
| 46 | + throw exceptionSupplier.get(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * <em>Assert</em> that {@code expression} is {@code true}. |
| 52 | + * |
| 53 | + * @param expression expression to check |
| 54 | + * @param <T> Type of the exception to be thrown |
| 55 | + * @param exceptionSupplier supplier of a exception to throw |
| 56 | + * @throws T if {@code expression} is {@code false} |
| 57 | + * @throws NullPointerException if {@code expression} is {@code false} and {@code exceptionSupplier} is {@code null} |
| 58 | + */ |
| 59 | + public static <T extends Throwable> void isTrue(boolean expression, Supplier<T> exceptionSupplier) throws T { |
| 60 | + if (!expression) { |
| 61 | + throw exceptionSupplier.get(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * <em>Assert</em> that {@code expression} is {@code false}. |
| 67 | + * |
| 68 | + * @param expression expression to check |
| 69 | + * @param <T> Type of the exception to be thrown |
| 70 | + * @param exceptionSupplier supplier of a exception to throw |
| 71 | + * @throws T if {@code expression} is {@code true} |
| 72 | + * @throws NullPointerException if {@code expression} is {@code true} and {@code exceptionSupplier} is {@code null} |
| 73 | + */ |
| 74 | + public static <T extends Throwable> void isFalse(boolean expression, Supplier<T> exceptionSupplier) throws T { |
| 75 | + if (expression) { |
| 76 | + throw exceptionSupplier.get(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * <em>Assert</em> that {@code collection} is {@code not null} and {@code not empty}. |
| 82 | + * |
| 83 | + * @param collection collection to check |
| 84 | + * @param <T> Type of the exception to be thrown |
| 85 | + * @param exceptionSupplier supplier of a exception to throw |
| 86 | + * @throws T if {@code collection} is {@code null} or {@code empty} |
| 87 | + * @throws NullPointerException if {@code collection} is {@code null} or {@code empty} and {@code exceptionSupplier} is {@code null} |
| 88 | + */ |
| 89 | + public static <T extends Throwable> void nonNullNonEmpty(Collection<?> collection, Supplier<T> exceptionSupplier) throws T { |
| 90 | + if (Collections.nullOrEmpty(collection)) { |
| 91 | + throw exceptionSupplier.get(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * <em>Assert</em> that {@code collection} is {@code null} or {@code empty}. |
| 97 | + * |
| 98 | + * @param collection collection to check |
| 99 | + * @param <T> Type of the exception to be thrown |
| 100 | + * @param exceptionSupplier supplier of a exception to throw |
| 101 | + * @throws T if {@code collection} is {@code not null} and {@code not empty} |
| 102 | + * @throws NullPointerException if {@code collection} is {@code not null} and {@code not empty} and {@code exceptionSupplier} is {@code null} |
| 103 | + */ |
| 104 | + public static <T extends Throwable> void nullOrEmpty(Collection<?> collection, Supplier<T> exceptionSupplier) throws T { |
| 105 | + if (Collections.nonNullNonEmpty(collection)) { |
| 106 | + throw exceptionSupplier.get(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * <em>Assert</em> that {@code map} is {@code not null} and {@code not empty}. |
| 112 | + * |
| 113 | + * @param map map to check |
| 114 | + * @param <T> Type of the exception to be thrown |
| 115 | + * @param exceptionSupplier supplier of a exception to throw |
| 116 | + * @throws T if {@code map} is {@code null} or {@code empty} |
| 117 | + * @throws NullPointerException if {@code map} is {@code null} or {@code empty} and {@code exceptionSupplier} is {@code null} |
| 118 | + */ |
| 119 | + public static <T extends Throwable> void nonNullNonEmptyMap(Map<?, ?> map, Supplier<T> exceptionSupplier) throws T { |
| 120 | + if (Collections.nullOrEmpty(map)) { |
| 121 | + throw exceptionSupplier.get(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * <em>Assert</em> that {@code map} is {@code null} or {@code empty}. |
| 127 | + * |
| 128 | + * @param map map to check |
| 129 | + * @param <T> Type of the exception to be thrown |
| 130 | + * @param exceptionSupplier supplier of a exception to throw |
| 131 | + * @throws T if {@code map} is {@code not null} and {@code not empty} |
| 132 | + * @throws NullPointerException if {@code map} is {@code not null} and {@code not empty} and {@code exceptionSupplier} is {@code null} |
| 133 | + */ |
| 134 | + public static <T extends Throwable> void nullOrEmptyMap(Map<?, ?> map, Supplier<T> exceptionSupplier) throws T { |
| 135 | + if (Collections.nonNullNonEmpty(map)) { |
| 136 | + throw exceptionSupplier.get(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments