[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 |
|
---|
[5980] | 8 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 9 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[2711] | 10 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
| 11 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * This utility class provides a collection of static helper methods for checking
|
---|
| 15 | * parameters at run-time.
|
---|
[6362] | 16 | * @since 2711
|
---|
[2711] | 17 | */
|
---|
[6362] | 18 | public final class CheckParameterUtil {
|
---|
[2711] | 19 |
|
---|
[6362] | 20 | private CheckParameterUtil() {
|
---|
| 21 | // Hide default constructor for utils classes
|
---|
| 22 | }
|
---|
[2711] | 23 |
|
---|
[5980] | 24 | /**
|
---|
[12713] | 25 | * Ensures that a parameter is not null and that a certain condition holds.
|
---|
| 26 | * @param <T> parameter type
|
---|
| 27 | * @param obj parameter value
|
---|
| 28 | * @param parameterName parameter name
|
---|
| 29 | * @param conditionMsg string, stating the condition
|
---|
| 30 | * @param condition the condition to check
|
---|
| 31 | * @throws IllegalArgumentException in case the object is null or the condition
|
---|
| 32 | * is violated
|
---|
| 33 | * @since 12713
|
---|
| 34 | */
|
---|
| 35 | public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) {
|
---|
| 36 | ensureParameterNotNull(obj, parameterName);
|
---|
| 37 | if (!condition.test(obj))
|
---|
| 38 | throw new IllegalArgumentException(
|
---|
| 39 | MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''",
|
---|
| 40 | parameterName,
|
---|
| 41 | obj.getClass().getCanonicalName(),
|
---|
| 42 | conditionMsg,
|
---|
| 43 | obj));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Ensures that a parameter is not null and that a certain condition holds.
|
---|
| 48 | * @param <T> parameter type
|
---|
| 49 | * @param obj parameter value
|
---|
| 50 | * @param parameterName parameter name
|
---|
| 51 | * @param condition the condition to check
|
---|
| 52 | * @throws IllegalArgumentException in case the object is null or the condition
|
---|
| 53 | * is violated
|
---|
| 54 | * @since 12713
|
---|
| 55 | */
|
---|
| 56 | public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) {
|
---|
| 57 | ensureParameterNotNull(obj, parameterName);
|
---|
| 58 | if (!condition.test(obj))
|
---|
| 59 | throw new IllegalArgumentException(
|
---|
| 60 | MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''",
|
---|
| 61 | parameterName,
|
---|
| 62 | obj.getClass().getCanonicalName(),
|
---|
| 63 | obj));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
[5980] | 67 | * Ensures an OSM primitive ID is valid
|
---|
| 68 | * @param id The id to check
|
---|
| 69 | * @param parameterName The parameter name
|
---|
| 70 | * @throws IllegalArgumentException if the primitive ID is not valid (negative or zero)
|
---|
[12713] | 71 | * @deprecated use {@link #ensure(Object, String, String, Predicate)}
|
---|
[5980] | 72 | */
|
---|
[12713] | 73 | @Deprecated
|
---|
[8506] | 74 | public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
|
---|
[2853] | 75 | ensureParameterNotNull(id, parameterName);
|
---|
[2711] | 76 | if (id.getUniqueId() <= 0)
|
---|
[8506] | 77 | throw new IllegalArgumentException(
|
---|
| 78 | MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
|
---|
[2711] | 79 | }
|
---|
| 80 |
|
---|
[5980] | 81 | /**
|
---|
| 82 | * Ensures lat/lon coordinates are valid
|
---|
| 83 | * @param latlon The lat/lon to check
|
---|
| 84 | * @param parameterName The parameter name
|
---|
| 85 | * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
|
---|
| 86 | * @since 5980
|
---|
[12713] | 87 | * @deprecated use {@link #ensure(Object, String, Predicate)}
|
---|
[5980] | 88 | */
|
---|
[12713] | 89 | @Deprecated
|
---|
[8506] | 90 | public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
|
---|
[5980] | 91 | ensureParameterNotNull(latlon, parameterName);
|
---|
| 92 | if (!latlon.isValid())
|
---|
[8506] | 93 | throw new IllegalArgumentException(
|
---|
| 94 | MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
|
---|
[5980] | 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Ensures east/north coordinates are valid
|
---|
| 99 | * @param eastnorth The east/north to check
|
---|
| 100 | * @param parameterName The parameter name
|
---|
| 101 | * @throws IllegalArgumentException if the east/north are {@code null} or not valid
|
---|
| 102 | * @since 5980
|
---|
[12713] | 103 | * @deprecated use {@link #ensure(Object, String, Predicate)}
|
---|
[5980] | 104 | */
|
---|
[12713] | 105 | @Deprecated
|
---|
[8506] | 106 | public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
|
---|
[5980] | 107 | ensureParameterNotNull(eastnorth, parameterName);
|
---|
| 108 | if (!eastnorth.isValid())
|
---|
[8506] | 109 | throw new IllegalArgumentException(
|
---|
| 110 | MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
|
---|
[5980] | 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Ensures a version number is valid
|
---|
| 115 | * @param version The version to check
|
---|
| 116 | * @param parameterName The parameter name
|
---|
| 117 | * @throws IllegalArgumentException if the version is not valid (negative)
|
---|
[12713] | 118 | * @deprecated use {@link #ensure(Object, String, String, Predicate)}
|
---|
[5980] | 119 | */
|
---|
[12713] | 120 | @Deprecated
|
---|
[8506] | 121 | public static void ensureValidVersion(long version, String parameterName) {
|
---|
[2711] | 122 | if (version < 0)
|
---|
[8506] | 123 | throw new IllegalArgumentException(
|
---|
| 124 | MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
|
---|
[2711] | 125 | }
|
---|
| 126 |
|
---|
[5980] | 127 | /**
|
---|
| 128 | * Ensures a parameter is not {@code null}
|
---|
| 129 | * @param value The parameter to check
|
---|
| 130 | * @param parameterName The parameter name
|
---|
| 131 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 132 | */
|
---|
[8506] | 133 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
---|
[2711] | 134 | if (value == null)
|
---|
[2853] | 135 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
---|
[2711] | 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
[5980] | 139 | * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
|
---|
| 140 | * @param value The parameter to check
|
---|
| 141 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 142 | * @since 3871
|
---|
[3871] | 143 | */
|
---|
[8506] | 144 | public static void ensureParameterNotNull(Object value) {
|
---|
[3871] | 145 | if (value == null)
|
---|
| 146 | throw new IllegalArgumentException("Parameter must not be null");
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
[6506] | 150 | * Ensures that the condition {@code condition} holds.
|
---|
| 151 | * @param condition The condition to check
|
---|
[9231] | 152 | * @param message error message
|
---|
[6506] | 153 | * @throws IllegalArgumentException if the condition does not hold
|
---|
[12822] | 154 | * @see #ensureThat(boolean, Supplier)
|
---|
[6506] | 155 | */
|
---|
[8506] | 156 | public static void ensureThat(boolean condition, String message) {
|
---|
[6506] | 157 | if (!condition)
|
---|
| 158 | throw new IllegalArgumentException(message);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
[12822] | 162 | * Ensures that the condition {@code condition} holds.
|
---|
| 163 | *
|
---|
| 164 | * This method can be used when the message is not a plain string literal,
|
---|
| 165 | * but somehow constructed. Using a {@link Supplier} improves the performance,
|
---|
| 166 | * as the string construction is skipped when the condition holds.
|
---|
| 167 | * @param condition The condition to check
|
---|
| 168 | * @param messageSupplier supplier of the error message
|
---|
| 169 | * @throws IllegalArgumentException if the condition does not hold
|
---|
| 170 | * @since 12822
|
---|
| 171 | */
|
---|
| 172 | public static void ensureThat(boolean condition, Supplier<String> messageSupplier) {
|
---|
| 173 | if (!condition)
|
---|
| 174 | throw new IllegalArgumentException(messageSupplier.get());
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
[5266] | 178 | * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
|
---|
[2801] | 179 | *
|
---|
[8506] | 180 | * @param id the primitive id
|
---|
[2711] | 181 | * @param parameterName the name of the parameter to be checked
|
---|
[8291] | 182 | * @throws IllegalArgumentException if id is null
|
---|
| 183 | * @throws IllegalArgumentException if id.getType() != NODE
|
---|
[12713] | 184 | * @deprecated use {@link #ensure(Object, String, String, Predicate)}
|
---|
[2711] | 185 | */
|
---|
[12713] | 186 | @Deprecated
|
---|
[8506] | 187 | public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
|
---|
[2853] | 188 | ensureParameterNotNull(id, parameterName);
|
---|
[8443] | 189 | if (!id.getType().equals(OsmPrimitiveType.NODE))
|
---|
[8506] | 190 | throw new IllegalArgumentException(
|
---|
| 191 | MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
|
---|
[2711] | 192 | }
|
---|
| 193 | }
|
---|