[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[2853] | 4 | import java.text.MessageFormat;
|
---|
[12713] | 5 | import java.util.function.Predicate;
|
---|
[12822] | 6 | import java.util.function.Supplier;
|
---|
[2711] | 7 |
|
---|
| 8 | /**
|
---|
| 9 | * This utility class provides a collection of static helper methods for checking
|
---|
| 10 | * parameters at run-time.
|
---|
[6362] | 11 | * @since 2711
|
---|
[2711] | 12 | */
|
---|
[6362] | 13 | public final class CheckParameterUtil {
|
---|
[2711] | 14 |
|
---|
[6362] | 15 | private CheckParameterUtil() {
|
---|
| 16 | // Hide default constructor for utils classes
|
---|
| 17 | }
|
---|
[2711] | 18 |
|
---|
[5980] | 19 | /**
|
---|
[12713] | 20 | * Ensures that a parameter is not null and that a certain condition holds.
|
---|
| 21 | * @param <T> parameter type
|
---|
| 22 | * @param obj parameter value
|
---|
| 23 | * @param parameterName parameter name
|
---|
| 24 | * @param conditionMsg string, stating the condition
|
---|
| 25 | * @param condition the condition to check
|
---|
| 26 | * @throws IllegalArgumentException in case the object is null or the condition
|
---|
| 27 | * is violated
|
---|
| 28 | * @since 12713
|
---|
| 29 | */
|
---|
| 30 | public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) {
|
---|
| 31 | ensureParameterNotNull(obj, parameterName);
|
---|
| 32 | if (!condition.test(obj))
|
---|
| 33 | throw new IllegalArgumentException(
|
---|
| 34 | MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''",
|
---|
| 35 | parameterName,
|
---|
| 36 | obj.getClass().getCanonicalName(),
|
---|
| 37 | conditionMsg,
|
---|
| 38 | obj));
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Ensures that a parameter is not null and that a certain condition holds.
|
---|
| 43 | * @param <T> parameter type
|
---|
| 44 | * @param obj parameter value
|
---|
| 45 | * @param parameterName parameter name
|
---|
| 46 | * @param condition the condition to check
|
---|
| 47 | * @throws IllegalArgumentException in case the object is null or the condition
|
---|
| 48 | * is violated
|
---|
| 49 | * @since 12713
|
---|
| 50 | */
|
---|
| 51 | public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) {
|
---|
| 52 | ensureParameterNotNull(obj, parameterName);
|
---|
| 53 | if (!condition.test(obj))
|
---|
| 54 | throw new IllegalArgumentException(
|
---|
| 55 | MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''",
|
---|
| 56 | parameterName,
|
---|
| 57 | obj.getClass().getCanonicalName(),
|
---|
| 58 | obj));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
[5980] | 62 | * Ensures a parameter is not {@code null}
|
---|
| 63 | * @param value The parameter to check
|
---|
| 64 | * @param parameterName The parameter name
|
---|
| 65 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 66 | */
|
---|
[8506] | 67 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
---|
[2711] | 68 | if (value == null)
|
---|
[2853] | 69 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
---|
[2711] | 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
[5980] | 73 | * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
|
---|
| 74 | * @param value The parameter to check
|
---|
| 75 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 76 | * @since 3871
|
---|
[3871] | 77 | */
|
---|
[8506] | 78 | public static void ensureParameterNotNull(Object value) {
|
---|
[3871] | 79 | if (value == null)
|
---|
| 80 | throw new IllegalArgumentException("Parameter must not be null");
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
[6506] | 84 | * Ensures that the condition {@code condition} holds.
|
---|
| 85 | * @param condition The condition to check
|
---|
[9231] | 86 | * @param message error message
|
---|
[6506] | 87 | * @throws IllegalArgumentException if the condition does not hold
|
---|
[12822] | 88 | * @see #ensureThat(boolean, Supplier)
|
---|
[6506] | 89 | */
|
---|
[8506] | 90 | public static void ensureThat(boolean condition, String message) {
|
---|
[6506] | 91 | if (!condition)
|
---|
| 92 | throw new IllegalArgumentException(message);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
[12822] | 96 | * Ensures that the condition {@code condition} holds.
|
---|
| 97 | *
|
---|
| 98 | * This method can be used when the message is not a plain string literal,
|
---|
| 99 | * but somehow constructed. Using a {@link Supplier} improves the performance,
|
---|
| 100 | * as the string construction is skipped when the condition holds.
|
---|
| 101 | * @param condition The condition to check
|
---|
| 102 | * @param messageSupplier supplier of the error message
|
---|
| 103 | * @throws IllegalArgumentException if the condition does not hold
|
---|
| 104 | * @since 12822
|
---|
| 105 | */
|
---|
| 106 | public static void ensureThat(boolean condition, Supplier<String> messageSupplier) {
|
---|
| 107 | if (!condition)
|
---|
| 108 | throw new IllegalArgumentException(messageSupplier.get());
|
---|
| 109 | }
|
---|
[2711] | 110 | }
|
---|