source: josm/trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java@ 12742

Last change on this file since 12742 was 12713, checked in by bastiK, 7 years ago

see #15229 - remove dependencies of CheckParameterUtil on various data classes

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.MessageFormat;
5import java.util.function.Predicate;
6
7import org.openstreetmap.josm.data.coor.EastNorth;
8import org.openstreetmap.josm.data.coor.LatLon;
9import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
10import org.openstreetmap.josm.data.osm.PrimitiveId;
11
12/**
13 * This utility class provides a collection of static helper methods for checking
14 * parameters at run-time.
15 * @since 2711
16 */
17public final class CheckParameterUtil {
18
19 private CheckParameterUtil() {
20 // Hide default constructor for utils classes
21 }
22
23 /**
24 * Ensures that a parameter is not null and that a certain condition holds.
25 * @param <T> parameter type
26 * @param obj parameter value
27 * @param parameterName parameter name
28 * @param conditionMsg string, stating the condition
29 * @param condition the condition to check
30 * @throws IllegalArgumentException in case the object is null or the condition
31 * is violated
32 * @since 12713
33 */
34 public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) {
35 ensureParameterNotNull(obj, parameterName);
36 if (!condition.test(obj))
37 throw new IllegalArgumentException(
38 MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''",
39 parameterName,
40 obj.getClass().getCanonicalName(),
41 conditionMsg,
42 obj));
43 }
44
45 /**
46 * Ensures that a parameter is not null and that a certain condition holds.
47 * @param <T> parameter type
48 * @param obj parameter value
49 * @param parameterName parameter name
50 * @param condition the condition to check
51 * @throws IllegalArgumentException in case the object is null or the condition
52 * is violated
53 * @since 12713
54 */
55 public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) {
56 ensureParameterNotNull(obj, parameterName);
57 if (!condition.test(obj))
58 throw new IllegalArgumentException(
59 MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''",
60 parameterName,
61 obj.getClass().getCanonicalName(),
62 obj));
63 }
64
65 /**
66 * Ensures an OSM primitive ID is valid
67 * @param id The id to check
68 * @param parameterName The parameter name
69 * @throws IllegalArgumentException if the primitive ID is not valid (negative or zero)
70 * @deprecated use {@link #ensure(Object, String, String, Predicate)}
71 */
72 @Deprecated
73 public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
74 ensureParameterNotNull(id, parameterName);
75 if (id.getUniqueId() <= 0)
76 throw new IllegalArgumentException(
77 MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
78 }
79
80 /**
81 * Ensures lat/lon coordinates are valid
82 * @param latlon The lat/lon to check
83 * @param parameterName The parameter name
84 * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
85 * @since 5980
86 * @deprecated use {@link #ensure(Object, String, Predicate)}
87 */
88 @Deprecated
89 public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
90 ensureParameterNotNull(latlon, parameterName);
91 if (!latlon.isValid())
92 throw new IllegalArgumentException(
93 MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
94 }
95
96 /**
97 * Ensures east/north coordinates are valid
98 * @param eastnorth The east/north to check
99 * @param parameterName The parameter name
100 * @throws IllegalArgumentException if the east/north are {@code null} or not valid
101 * @since 5980
102 * @deprecated use {@link #ensure(Object, String, Predicate)}
103 */
104 @Deprecated
105 public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
106 ensureParameterNotNull(eastnorth, parameterName);
107 if (!eastnorth.isValid())
108 throw new IllegalArgumentException(
109 MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
110 }
111
112 /**
113 * Ensures a version number is valid
114 * @param version The version to check
115 * @param parameterName The parameter name
116 * @throws IllegalArgumentException if the version is not valid (negative)
117 * @deprecated use {@link #ensure(Object, String, String, Predicate)}
118 */
119 @Deprecated
120 public static void ensureValidVersion(long version, String parameterName) {
121 if (version < 0)
122 throw new IllegalArgumentException(
123 MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
124 }
125
126 /**
127 * Ensures a parameter is not {@code null}
128 * @param value The parameter to check
129 * @param parameterName The parameter name
130 * @throws IllegalArgumentException if the parameter is {@code null}
131 */
132 public static void ensureParameterNotNull(Object value, String parameterName) {
133 if (value == null)
134 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
135 }
136
137 /**
138 * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
139 * @param value The parameter to check
140 * @throws IllegalArgumentException if the parameter is {@code null}
141 * @since 3871
142 */
143 public static void ensureParameterNotNull(Object value) {
144 if (value == null)
145 throw new IllegalArgumentException("Parameter must not be null");
146 }
147
148 /**
149 * Ensures that the condition {@code condition} holds.
150 * @param condition The condition to check
151 * @param message error message
152 * @throws IllegalArgumentException if the condition does not hold
153 */
154 public static void ensureThat(boolean condition, String message) {
155 if (!condition)
156 throw new IllegalArgumentException(message);
157 }
158
159 /**
160 * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
161 *
162 * @param id the primitive id
163 * @param parameterName the name of the parameter to be checked
164 * @throws IllegalArgumentException if id is null
165 * @throws IllegalArgumentException if id.getType() != NODE
166 * @deprecated use {@link #ensure(Object, String, String, Predicate)}
167 */
168 @Deprecated
169 public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
170 ensureParameterNotNull(id, parameterName);
171 if (!id.getType().equals(OsmPrimitiveType.NODE))
172 throw new IllegalArgumentException(
173 MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
174 }
175}
Note: See TracBrowser for help on using the repository browser.