1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
7 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * This utility class provides a collection of static helper methods for checking
|
---|
11 | * parameters at run-time.
|
---|
12 | *
|
---|
13 | */
|
---|
14 | public class CheckParameterUtil {
|
---|
15 |
|
---|
16 | private CheckParameterUtil(){}
|
---|
17 |
|
---|
18 |
|
---|
19 | public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
|
---|
20 | if (id == null)
|
---|
21 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", parameterName));
|
---|
22 | if (id.getUniqueId() <= 0)
|
---|
23 | throw new IllegalArgumentException(tr("Expected unique id > 0 for primitive id, got {0}", id.getUniqueId()));
|
---|
24 | }
|
---|
25 |
|
---|
26 | public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException {
|
---|
27 | if (version < 0)
|
---|
28 | throw new IllegalArgumentException(tr("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
|
---|
29 | }
|
---|
30 |
|
---|
31 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
---|
32 | if (value == null)
|
---|
33 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", parameterName));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Ensures that <code>id</code> is non-null primitive id of type {@see OsmPrimitiveType#NODE}
|
---|
38 | *
|
---|
39 | * @param id the primitive id
|
---|
40 | * @param parameterName the name of the parameter to be checked
|
---|
41 | * @throws IllegalArgumentException thrown if id is null
|
---|
42 | * @throws IllegalArgumentException thrown if id.getType() != NODE
|
---|
43 | */
|
---|
44 | public static void ensureValidNodeId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
|
---|
45 | if (id == null)
|
---|
46 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", parameterName));
|
---|
47 | if (! id.getType().equals(OsmPrimitiveType.NODE))
|
---|
48 | throw new IllegalArgumentException(tr("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
|
---|
49 | }
|
---|
50 | }
|
---|