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

Last change on this file since 9284 was 9231, checked in by Don-vip, 8 years ago

javadoc update

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.MessageFormat;
5
6import org.openstreetmap.josm.data.coor.EastNorth;
7import org.openstreetmap.josm.data.coor.LatLon;
8import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
9import 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 */
16public 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 * @param message error message
101 * @throws IllegalArgumentException if the condition does not hold
102 */
103 public static void ensureThat(boolean condition, String message) {
104 if (!condition)
105 throw new IllegalArgumentException(message);
106 }
107
108 /**
109 * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
110 *
111 * @param id the primitive id
112 * @param parameterName the name of the parameter to be checked
113 * @throws IllegalArgumentException if id is null
114 * @throws IllegalArgumentException if id.getType() != NODE
115 */
116 public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
117 ensureParameterNotNull(id, parameterName);
118 if (!id.getType().equals(OsmPrimitiveType.NODE))
119 throw new IllegalArgumentException(
120 MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
121 }
122}
Note: See TracBrowser for help on using the repository browser.