[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[2853] | 4 | import java.text.MessageFormat;
|
---|
[2711] | 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 | public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
|
---|
[2853] | 19 | ensureParameterNotNull(id, parameterName);
|
---|
[2711] | 20 | if (id.getUniqueId() <= 0)
|
---|
[2853] | 21 | throw new IllegalArgumentException(MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
|
---|
[2711] | 22 | }
|
---|
| 23 |
|
---|
| 24 | public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException {
|
---|
| 25 | if (version < 0)
|
---|
[2853] | 26 | throw new IllegalArgumentException(MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
|
---|
[2711] | 27 | }
|
---|
| 28 |
|
---|
| 29 | public static void ensureParameterNotNull(Object value, String parameterName) {
|
---|
| 30 | if (value == null)
|
---|
[2853] | 31 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
|
---|
[2711] | 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
[3871] | 35 | * can find line number in the stack trace, so parameter name is optional
|
---|
| 36 | */
|
---|
| 37 | public static void ensureParameterNotNull(Object value) {
|
---|
| 38 | if (value == null)
|
---|
| 39 | throw new IllegalArgumentException("Parameter must not be null");
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
[2711] | 43 | * Ensures that <code>id</code> is non-null primitive id of type {@see OsmPrimitiveType#NODE}
|
---|
[2801] | 44 | *
|
---|
[2711] | 45 | * @param id the primitive id
|
---|
| 46 | * @param parameterName the name of the parameter to be checked
|
---|
| 47 | * @throws IllegalArgumentException thrown if id is null
|
---|
| 48 | * @throws IllegalArgumentException thrown if id.getType() != NODE
|
---|
| 49 | */
|
---|
| 50 | public static void ensureValidNodeId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
|
---|
[2853] | 51 | ensureParameterNotNull(id, parameterName);
|
---|
[2711] | 52 | if (! id.getType().equals(OsmPrimitiveType.NODE))
|
---|
[2853] | 53 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
|
---|
[2711] | 54 | }
|
---|
| 55 | }
|
---|