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

Last change on this file since 8395 was 8291, checked in by Don-vip, 9 years ago

fix squid:RedundantThrowsDeclarationCheck + consistent Javadoc for exceptions

  • Property svn:eol-style set to native
File size: 5.0 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) throws IllegalArgumentException {
29 ensureParameterNotNull(id, parameterName);
30 if (id.getUniqueId() <= 0)
31 throw new IllegalArgumentException(MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
32 }
33
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 */
66 public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException {
67 if (version < 0)
68 throw new IllegalArgumentException(MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
69 }
70
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 {
78 if (value == null)
79 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
80 }
81
82 /**
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
87 */
88 public static void ensureParameterNotNull(Object value) throws IllegalArgumentException {
89 if (value == null)
90 throw new IllegalArgumentException("Parameter must not be null");
91 }
92
93 /**
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 /**
104 * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
105 *
106 * @param id the primitive id
107 * @param parameterName the name of the parameter to be checked
108 * @throws IllegalArgumentException if id is null
109 * @throws IllegalArgumentException if id.getType() != NODE
110 */
111 public static void ensureValidNodeId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
112 ensureParameterNotNull(id, parameterName);
113 if (! id.getType().equals(OsmPrimitiveType.NODE))
114 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
115 }
116}
Note: See TracBrowser for help on using the repository browser.