1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.text.MessageFormat;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
7 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
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.
|
---|
14 | * @since 2711
|
---|
15 | */
|
---|
16 | public final class CheckParameterUtil {
|
---|
17 |
|
---|
18 | private CheckParameterUtil() {
|
---|
19 | // Hide default constructor for utils classes
|
---|
20 | }
|
---|
21 |
|
---|
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 | */
|
---|
28 | public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
|
---|
29 | ensureParameterNotNull(id, parameterName);
|
---|
30 | if (id.getUniqueId() <= 0)
|
---|
31 | throw new IllegalArgumentException(
|
---|
32 | MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
|
---|
33 | }
|
---|
34 |
|
---|
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 | */
|
---|
42 | public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
|
---|
43 | ensureParameterNotNull(latlon, parameterName);
|
---|
44 | if (!latlon.isValid())
|
---|
45 | throw new IllegalArgumentException(
|
---|
46 | MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
|
---|
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 | */
|
---|
56 | public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
|
---|
57 | ensureParameterNotNull(eastnorth, parameterName);
|
---|
58 | if (!eastnorth.isValid())
|
---|
59 | throw new IllegalArgumentException(
|
---|
60 | MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
|
---|
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 | */
|
---|
69 | public static void ensureValidVersion(long version, String parameterName) {
|
---|
70 | if (version < 0)
|
---|
71 | throw new IllegalArgumentException(
|
---|
72 | MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
|
---|
73 | }
|
---|
74 |
|
---|
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 | */
|
---|
81 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
---|
82 | if (value == null)
|
---|
83 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
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
|
---|
91 | */
|
---|
92 | public static void ensureParameterNotNull(Object value) {
|
---|
93 | if (value == null)
|
---|
94 | throw new IllegalArgumentException("Parameter must not be null");
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Ensures that the condition {@code condition} holds.
|
---|
99 | * @param condition The condition to check
|
---|
100 | * @throws IllegalArgumentException if the condition does not hold
|
---|
101 | */
|
---|
102 | public static void ensureThat(boolean condition, String message) {
|
---|
103 | if (!condition)
|
---|
104 | throw new IllegalArgumentException(message);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
|
---|
109 | *
|
---|
110 | * @param id the primitive id
|
---|
111 | * @param parameterName the name of the parameter to be checked
|
---|
112 | * @throws IllegalArgumentException if id is null
|
---|
113 | * @throws IllegalArgumentException if id.getType() != NODE
|
---|
114 | */
|
---|
115 | public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
|
---|
116 | ensureParameterNotNull(id, parameterName);
|
---|
117 | if (!id.getType().equals(OsmPrimitiveType.NODE))
|
---|
118 | throw new IllegalArgumentException(
|
---|
119 | MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
|
---|
120 | }
|
---|
121 | }
|
---|