[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 | */
|
---|
[2711] | 28 | public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
|
---|
[2853] | 29 | ensureParameterNotNull(id, parameterName);
|
---|
[2711] | 30 | if (id.getUniqueId() <= 0)
|
---|
[2853] | 31 | throw new IllegalArgumentException(MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
|
---|
[2711] | 32 | }
|
---|
| 33 |
|
---|
[5980] | 34 | /**
|
---|
| 35 | * Ensures lat/lon coordinates are valid
|
---|
| 36 | * @param latlon The lat/lon to check
|
---|
| 37 | * @param parameterName The parameter name
|
---|
| 38 | * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
|
---|
| 39 | * @since 5980
|
---|
| 40 | */
|
---|
| 41 | public static void ensureValidCoordinates(LatLon latlon, String parameterName) throws IllegalArgumentException {
|
---|
| 42 | ensureParameterNotNull(latlon, parameterName);
|
---|
| 43 | if (!latlon.isValid())
|
---|
| 44 | throw new IllegalArgumentException(MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Ensures east/north coordinates are valid
|
---|
| 49 | * @param eastnorth The east/north to check
|
---|
| 50 | * @param parameterName The parameter name
|
---|
| 51 | * @throws IllegalArgumentException if the east/north are {@code null} or not valid
|
---|
| 52 | * @since 5980
|
---|
| 53 | */
|
---|
| 54 | public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) throws IllegalArgumentException {
|
---|
| 55 | ensureParameterNotNull(eastnorth, parameterName);
|
---|
| 56 | if (!eastnorth.isValid())
|
---|
| 57 | throw new IllegalArgumentException(MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Ensures a version number is valid
|
---|
| 62 | * @param version The version to check
|
---|
| 63 | * @param parameterName The parameter name
|
---|
| 64 | * @throws IllegalArgumentException if the version is not valid (negative)
|
---|
| 65 | */
|
---|
[2711] | 66 | public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException {
|
---|
| 67 | if (version < 0)
|
---|
[2853] | 68 | throw new IllegalArgumentException(MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
|
---|
[2711] | 69 | }
|
---|
| 70 |
|
---|
[5980] | 71 | /**
|
---|
| 72 | * Ensures a parameter is not {@code null}
|
---|
| 73 | * @param value The parameter to check
|
---|
| 74 | * @param parameterName The parameter name
|
---|
| 75 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 76 | */
|
---|
| 77 | public static void ensureParameterNotNull(Object value, String parameterName) throws IllegalArgumentException {
|
---|
[2711] | 78 | if (value == null)
|
---|
[2853] | 79 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
---|
[2711] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
[5980] | 83 | * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
|
---|
| 84 | * @param value The parameter to check
|
---|
| 85 | * @throws IllegalArgumentException if the parameter is {@code null}
|
---|
| 86 | * @since 3871
|
---|
[3871] | 87 | */
|
---|
[5980] | 88 | public static void ensureParameterNotNull(Object value) throws IllegalArgumentException {
|
---|
[3871] | 89 | if (value == null)
|
---|
| 90 | throw new IllegalArgumentException("Parameter must not be null");
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
[6506] | 94 | * Ensures that the condition {@code condition} holds.
|
---|
| 95 | * @param condition The condition to check
|
---|
| 96 | * @throws IllegalArgumentException if the condition does not hold
|
---|
| 97 | */
|
---|
| 98 | public static void ensureThat(boolean condition, String message) throws IllegalArgumentException {
|
---|
| 99 | if (!condition)
|
---|
| 100 | throw new IllegalArgumentException(message);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
[5266] | 104 | * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
|
---|
[2801] | 105 | *
|
---|
[2711] | 106 | * @param id the primitive id
|
---|
| 107 | * @param parameterName the name of the parameter to be checked
|
---|
| 108 | * @throws IllegalArgumentException thrown if id is null
|
---|
| 109 | * @throws IllegalArgumentException thrown if id.getType() != NODE
|
---|
| 110 | */
|
---|
| 111 | public static void ensureValidNodeId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
|
---|
[2853] | 112 | ensureParameterNotNull(id, parameterName);
|
---|
[2711] | 113 | if (! id.getType().equals(OsmPrimitiveType.NODE))
|
---|
[2853] | 114 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
|
---|
[2711] | 115 | }
|
---|
| 116 | }
|
---|