| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.tools; |
|---|
| 3 | |
|---|
| 4 | import java.text.MessageFormat; |
|---|
| 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 { |
|---|
| 19 | ensureParameterNotNull(id, parameterName); |
|---|
| 20 | if (id.getUniqueId() <= 0) |
|---|
| 21 | throw new IllegalArgumentException(MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName)); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException { |
|---|
| 25 | if (version < 0) |
|---|
| 26 | throw new IllegalArgumentException(MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version)); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public static void ensureParameterNotNull(Object value, String parameterName) { |
|---|
| 30 | if (value == null) |
|---|
| 31 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName)); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 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 | /** |
|---|
| 43 | * Ensures that <code>id</code> is non-null primitive id of type {@see OsmPrimitiveType#NODE} |
|---|
| 44 | * |
|---|
| 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 { |
|---|
| 51 | ensureParameterNotNull(id, parameterName); |
|---|
| 52 | if (! id.getType().equals(OsmPrimitiveType.NODE)) |
|---|
| 53 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName())); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|