|
| 1 | +import java.util.Collection; |
| 2 | +import java.util.Map; |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.function.Supplier; |
| 5 | + |
| 6 | +public class Validate { |
| 7 | + |
| 8 | + private static final String DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE = "The validated array is empty"; |
| 9 | + private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE = |
| 10 | + "The validated character sequence is empty"; |
| 11 | + private static final String DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE = "The validated collection is empty"; |
| 12 | + private static final String DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE = "The validated map is empty"; |
| 13 | + private static final String DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE = "The validated array index is invalid: %d"; |
| 14 | + private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = |
| 15 | + "The validated character sequence index is invalid: %d"; |
| 16 | + private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = |
| 17 | + "The validated collection index is invalid: %d"; |
| 18 | + |
| 19 | + private static String getMessage(final String message, final Object... values) { |
| 20 | + return ArrayUtils.isEmpty(values) ? message : String.format(message, values); |
| 21 | + } |
| 22 | + |
| 23 | + public static <T extends Collection<?>> T notEmpty(final T collection) { |
| 24 | + return notEmpty(collection, DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE); |
| 25 | + } |
| 26 | + |
| 27 | + public static <T extends Map<?, ?>> T notEmpty(final T map) { |
| 28 | + return notEmpty(map, DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE); |
| 29 | + } |
| 30 | + |
| 31 | + public static <T extends CharSequence> T notEmpty(final T chars) { |
| 32 | + return notEmpty(chars, DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE); |
| 33 | + } |
| 34 | + |
| 35 | + public static <T extends Collection<?>> T notEmpty(final T collection, final String message, final Object... values) { |
| 36 | + Objects.requireNonNull(collection, toSupplier(message, values)); |
| 37 | + if (collection.isEmpty()) { |
| 38 | + throw new IllegalArgumentException(getMessage(message, values)); |
| 39 | + } |
| 40 | + return collection; |
| 41 | + } |
| 42 | + |
| 43 | + public static <T extends Map<?, ?>> T notEmpty(final T map, final String message, final Object... values) { |
| 44 | + Objects.requireNonNull(map, toSupplier(message, values)); |
| 45 | + if (map.isEmpty()) { |
| 46 | + throw new IllegalArgumentException(getMessage(message, values)); |
| 47 | + } |
| 48 | + return map; |
| 49 | + } |
| 50 | + |
| 51 | + public static <T extends CharSequence> T notEmpty(final T chars, final String message, final Object... values) { |
| 52 | + Objects.requireNonNull(chars, toSupplier(message, values)); |
| 53 | + if (chars.length() == 0) { |
| 54 | + throw new IllegalArgumentException(getMessage(message, values)); |
| 55 | + } |
| 56 | + return chars; |
| 57 | + } |
| 58 | + |
| 59 | + public static <T> T[] notEmpty(final T[] array) { |
| 60 | + return notEmpty(array, DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE); |
| 61 | + } |
| 62 | + |
| 63 | + public static <T> T[] notEmpty(final T[] array, final String message, final Object... values) { |
| 64 | + Objects.requireNonNull(array, toSupplier(message, values)); |
| 65 | + if (array.length == 0) { |
| 66 | + throw new IllegalArgumentException(getMessage(message, values)); |
| 67 | + } |
| 68 | + return array; |
| 69 | + } |
| 70 | + |
| 71 | + private static Supplier<String> toSupplier(final String message, final Object... values) { |
| 72 | + return () -> getMessage(message, values); |
| 73 | + } |
| 74 | + |
| 75 | + private static Supplier<String> toSupplier(final String message, final Object values) { |
| 76 | + return () -> getMessage(message, values); |
| 77 | + } |
| 78 | + |
| 79 | + public static <T extends Collection<?>> T validIndex(final T collection, final int index) { |
| 80 | + return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index)); |
| 81 | + } |
| 82 | + |
| 83 | + public static <T extends CharSequence> T validIndex(final T chars, final int index) { |
| 84 | + return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index)); |
| 85 | + } |
| 86 | + |
| 87 | + public static <T extends Collection<?>> T validIndex(final T collection, final int index, final String message, final Object... values) { |
| 88 | + Objects.requireNonNull(collection, "collection"); |
| 89 | + if (index < 0 || index >= collection.size()) { |
| 90 | + throw new IndexOutOfBoundsException(getMessage(message, values)); |
| 91 | + } |
| 92 | + return collection; |
| 93 | + } |
| 94 | + |
| 95 | + public static <T extends CharSequence> T validIndex(final T chars, final int index, final String message, final Object... values) { |
| 96 | + Objects.requireNonNull(chars, "chars"); |
| 97 | + if (index < 0 || index >= chars.length()) { |
| 98 | + throw new IndexOutOfBoundsException(getMessage(message, values)); |
| 99 | + } |
| 100 | + return chars; |
| 101 | + } |
| 102 | + |
| 103 | + public static <T> T[] validIndex(final T[] array, final int index) { |
| 104 | + return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index)); |
| 105 | + } |
| 106 | + |
| 107 | + public static <T> T[] validIndex(final T[] array, final int index, final String message, final Object... values) { |
| 108 | + Objects.requireNonNull(array, "array"); |
| 109 | + if (index < 0 || index >= array.length) { |
| 110 | + throw new IndexOutOfBoundsException(getMessage(message, values)); |
| 111 | + } |
| 112 | + return array; |
| 113 | + } |
| 114 | +} |
0 commit comments