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

Last change on this file since 12841 was 12822, checked in by bastiK, 7 years ago

add method for performance to CheckParameterUtil

  • Property svn:eol-style set to native
File size: 7.9 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;
6import java.util.function.Supplier;
7
8import org.openstreetmap.josm.data.coor.EastNorth;
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
11import 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.
16 * @since 2711
17 */
18public final class CheckParameterUtil {
19
20 private CheckParameterUtil() {
21 // Hide default constructor for utils classes
22 }
23
24 /**
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 /**
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)
71 * @deprecated use {@link #ensure(Object, String, String, Predicate)}
72 */
73 @Deprecated
74 public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
75 ensureParameterNotNull(id, parameterName);
76 if (id.getUniqueId() <= 0)
77 throw new IllegalArgumentException(
78 MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
79 }
80
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
87 * @deprecated use {@link #ensure(Object, String, Predicate)}
88 */
89 @Deprecated
90 public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
91 ensureParameterNotNull(latlon, parameterName);
92 if (!latlon.isValid())
93 throw new IllegalArgumentException(
94 MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
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
103 * @deprecated use {@link #ensure(Object, String, Predicate)}
104 */
105 @Deprecated
106 public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
107 ensureParameterNotNull(eastnorth, parameterName);
108 if (!eastnorth.isValid())
109 throw new IllegalArgumentException(
110 MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
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)
118 * @deprecated use {@link #ensure(Object, String, String, Predicate)}
119 */
120 @Deprecated
121 public static void ensureValidVersion(long version, String parameterName) {
122 if (version < 0)
123 throw new IllegalArgumentException(
124 MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
125 }
126
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 */
133 public static void ensureParameterNotNull(Object value, String parameterName) {
134 if (value == null)
135 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
136 }
137
138 /**
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
143 */
144 public static void ensureParameterNotNull(Object value) {
145 if (value == null)
146 throw new IllegalArgumentException("Parameter must not be null");
147 }
148
149 /**
150 * Ensures that the condition {@code condition} holds.
151 * @param condition The condition to check
152 * @param message error message
153 * @throws IllegalArgumentException if the condition does not hold
154 * @see #ensureThat(boolean, Supplier)
155 */
156 public static void ensureThat(boolean condition, String message) {
157 if (!condition)
158 throw new IllegalArgumentException(message);
159 }
160
161 /**
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 /**
178 * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
179 *
180 * @param id the primitive id
181 * @param parameterName the name of the parameter to be checked
182 * @throws IllegalArgumentException if id is null
183 * @throws IllegalArgumentException if id.getType() != NODE
184 * @deprecated use {@link #ensure(Object, String, String, Predicate)}
185 */
186 @Deprecated
187 public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
188 ensureParameterNotNull(id, parameterName);
189 if (!id.getType().equals(OsmPrimitiveType.NODE))
190 throw new IllegalArgumentException(
191 MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
192 }
193}
Note: See TracBrowser for help on using the repository browser.