[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[2853] | 4 | import java.text.MessageFormat;
|
---|
[2711] | 5 |
|
---|
[5980] | 6 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 7 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[2711] | 8 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
| 9 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * This utility class provides a collection of static helper methods for checking
|
---|
| 13 | * parameters at run-time.
|
---|
[6362] | 14 | * @since 2711
|
---|
[2711] | 15 | */
|
---|
[6362] | 16 | public final class CheckParameterUtil {
|
---|
[2711] | 17 |
|
---|
[6362] | 18 | private CheckParameterUtil() {
|
---|
| 19 | // Hide default constructor for utils classes
|
---|
| 20 | }
|
---|
[2711] | 21 |
|
---|
[5980] | 22 | /**
|
---|
| 23 | * Ensures an OSM primitive ID is valid
|
---|
| 24 | * @param id The id to check
|
---|
| 25 | * @param parameterName The parameter name
|
---|
| 26 | * @throws IllegalArgumentException if the primitive ID is not valid (negative or zero)
|
---|
| 27 | */
|
---|
[8506] | 28 | public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
|
---|
[2853] | 29 | ensureParameterNotNull(id, parameterName);
|
---|
[2711] | 30 | if (id.getUniqueId() <= 0)
|
---|
[8506] | 31 | throw new IllegalArgumentException(
|
---|
| 32 | MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
|
---|
[2711] | 33 | }
|
---|
| 34 |
|
---|
[5980] | 35 | /**
|
---|
| 36 | * Ensures lat/lon coordinates are valid
|
---|
| 37 | * @param latlon The lat/lon to check
|
---|
| 38 | * @param parameterName The parameter name
|
---|
| 39 | * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
|
---|
| 40 | * @since 5980
|
---|
| 41 | */
|
---|
[8506] | 42 | public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
|
---|
[5980] | 43 | ensureParameterNotNull(latlon, parameterName);
|
---|
| 44 | if (!latlon.isValid())
|
---|
[8506] | 45 | throw new IllegalArgumentException(
|
---|
| 46 | MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
|
---|
[5980] | 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Ensures east/north coordinates are valid
|
---|
| 51 | * @param eastnorth The east/north to check
|
---|
| 52 | * @param parameterName The parameter name
|
---|
| 53 | * @throws IllegalArgumentException if the east/north are {@code null} or not valid
|
---|
| 54 | * @since 5980
|
---|
| 55 | */
|
---|
[8506] | 56 | public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
|
---|
[5980] | 57 | ensureParameterNotNull(eastnorth, parameterName);
|
---|
| 58 | if (!eastnorth.isValid())
|
---|
[8506] | 59 | throw new IllegalArgumentException(
|
---|
| 60 | MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
|
---|
[5980] | 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Ensures a version number is valid
|
---|
| 65 | * @param version The version to check
|
---|
| 66 | * @param parameterName The parameter name
|
---|
| 67 | * @throws IllegalArgumentException if the version is not valid (negative)
|
---|
| 68 | */
|
---|
[8506] | 69 | public static void ensureValidVersion(long version, String parameterName) {
|
---|
[2711] | 70 | if (version < 0)
|
---|
[8506] | 71 | throw new IllegalArgumentException(
|
---|
| 72 | MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
|
---|
[2711] | 73 | }
|
---|
| 74 |
|
---|
[5980] | 75 | /**
|
---|
| 76 | * Ensures a parameter is not {@code null}
|
---|
| 77 | * @param value The parameter to check
|
---|
| 78 | * @param parameterName The parameter name
|
---|
| 79 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 80 | */
|
---|
[8506] | 81 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
---|
[2711] | 82 | if (value == null)
|
---|
[2853] | 83 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
---|
[2711] | 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
[5980] | 87 | * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
|
---|
| 88 | * @param value The parameter to check
|
---|
| 89 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 90 | * @since 3871
|
---|
[3871] | 91 | */
|
---|
[8506] | 92 | public static void ensureParameterNotNull(Object value) {
|
---|
[3871] | 93 | if (value == null)
|
---|
| 94 | throw new IllegalArgumentException("Parameter must not be null");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
[6506] | 98 | * Ensures that the condition {@code condition} holds.
|
---|
| 99 | * @param condition The condition to check
|
---|
[9231] | 100 | * @param message error message
|
---|
[6506] | 101 | * @throws IllegalArgumentException if the condition does not hold
|
---|
| 102 | */
|
---|
[8506] | 103 | public static void ensureThat(boolean condition, String message) {
|
---|
[6506] | 104 | if (!condition)
|
---|
| 105 | throw new IllegalArgumentException(message);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
[5266] | 109 | * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
|
---|
[2801] | 110 | *
|
---|
[8506] | 111 | * @param id the primitive id
|
---|
[2711] | 112 | * @param parameterName the name of the parameter to be checked
|
---|
[8291] | 113 | * @throws IllegalArgumentException if id is null
|
---|
| 114 | * @throws IllegalArgumentException if id.getType() != NODE
|
---|
[2711] | 115 | */
|
---|
[8506] | 116 | public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
|
---|
[2853] | 117 | ensureParameterNotNull(id, parameterName);
|
---|
[8443] | 118 | if (!id.getType().equals(OsmPrimitiveType.NODE))
|
---|
[8506] | 119 | throw new IllegalArgumentException(
|
---|
| 120 | MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
|
---|
[2711] | 121 | }
|
---|
| 122 | }
|
---|