| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.tools;
|
|---|
| 3 |
|
|---|
| 4 | import java.text.MessageFormat;
|
|---|
| 5 | import java.util.function.Supplier;
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * This utility class provides a collection of static helper methods for checking
|
|---|
| 9 | * parameters at run-time.
|
|---|
| 10 | * @since 2711
|
|---|
| 11 | */
|
|---|
| 12 | public final class CheckParameterUtil {
|
|---|
| 13 |
|
|---|
| 14 | private CheckParameterUtil() {
|
|---|
| 15 | // Hide default constructor for utils classes
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Ensures a parameter is not {@code null}
|
|---|
| 20 | * @param value The parameter to check
|
|---|
| 21 | * @param parameterName The parameter name
|
|---|
| 22 | * @throws IllegalArgumentException if the parameter is {@code null}
|
|---|
| 23 | */
|
|---|
| 24 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
|---|
| 25 | if (value == null)
|
|---|
| 26 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
|
|---|
| 31 | * @param value The parameter to check
|
|---|
| 32 | * @throws IllegalArgumentException if the parameter is {@code null}
|
|---|
| 33 | * @since 3871
|
|---|
| 34 | */
|
|---|
| 35 | public static void ensureParameterNotNull(Object value) {
|
|---|
| 36 | if (value == null)
|
|---|
| 37 | throw new IllegalArgumentException("Parameter must not be null");
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Ensures that the condition {@code condition} holds.
|
|---|
| 42 | * @param condition The condition to check
|
|---|
| 43 | * @param message error message
|
|---|
| 44 | * @throws IllegalArgumentException if the condition does not hold
|
|---|
| 45 | * @see #ensureThat(boolean, Supplier)
|
|---|
| 46 | */
|
|---|
| 47 | public static void ensureThat(boolean condition, String message) {
|
|---|
| 48 | if (!condition)
|
|---|
| 49 | throw new IllegalArgumentException(message);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Ensures that the condition {@code condition} holds.
|
|---|
| 54 | *
|
|---|
| 55 | * This method can be used when the message is not a plain string literal,
|
|---|
| 56 | * but somehow constructed. Using a {@link Supplier} improves the performance,
|
|---|
| 57 | * as the string construction is skipped when the condition holds.
|
|---|
| 58 | * @param condition The condition to check
|
|---|
| 59 | * @param messageSupplier supplier of the error message
|
|---|
| 60 | * @throws IllegalArgumentException if the condition does not hold
|
|---|
| 61 | * @since 12822
|
|---|
| 62 | */
|
|---|
| 63 | public static void ensureThat(boolean condition, Supplier<String> messageSupplier) {
|
|---|
| 64 | if (!condition)
|
|---|
| 65 | throw new IllegalArgumentException(messageSupplier.get());
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|