001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.text.MessageFormat;
005
006import org.openstreetmap.josm.data.coor.EastNorth;
007import org.openstreetmap.josm.data.coor.LatLon;
008import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
009import org.openstreetmap.josm.data.osm.PrimitiveId;
010
011/**
012 * This utility class provides a collection of static helper methods for checking
013 * parameters at run-time.
014 * @since 2711
015 */
016public final class CheckParameterUtil {
017
018    private CheckParameterUtil() {
019        // Hide default constructor for utils classes
020    }
021
022    /**
023     * Ensures an OSM primitive ID is valid
024     * @param id The id to check
025     * @param parameterName The parameter name
026     * @throws IllegalArgumentException if the primitive ID is not valid (negative or zero)
027     */
028    public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
029        ensureParameterNotNull(id, parameterName);
030        if (id.getUniqueId() <= 0)
031            throw new IllegalArgumentException(
032                    MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
033    }
034
035    /**
036     * Ensures lat/lon coordinates are valid
037     * @param latlon The lat/lon to check
038     * @param parameterName The parameter name
039     * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
040     * @since 5980
041     */
042    public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
043        ensureParameterNotNull(latlon, parameterName);
044        if (!latlon.isValid())
045            throw new IllegalArgumentException(
046                    MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
047    }
048
049    /**
050     * Ensures east/north coordinates are valid
051     * @param eastnorth The east/north to check
052     * @param parameterName The parameter name
053     * @throws IllegalArgumentException if the east/north are {@code null} or not valid
054     * @since 5980
055     */
056    public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
057        ensureParameterNotNull(eastnorth, parameterName);
058        if (!eastnorth.isValid())
059            throw new IllegalArgumentException(
060                    MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
061    }
062
063    /**
064     * Ensures a version number is valid
065     * @param version The version to check
066     * @param parameterName The parameter name
067     * @throws IllegalArgumentException if the version is not valid (negative)
068     */
069    public static void ensureValidVersion(long version, String parameterName) {
070        if (version < 0)
071            throw new IllegalArgumentException(
072                    MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
073    }
074
075    /**
076     * Ensures a parameter is not {@code null}
077     * @param value The parameter to check
078     * @param parameterName The parameter name
079     * @throws IllegalArgumentException if the parameter is {@code null}
080     */
081    public static void ensureParameterNotNull(Object value, String parameterName) {
082        if (value == null)
083            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
084    }
085
086    /**
087     * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
088     * @param value The parameter to check
089     * @throws IllegalArgumentException if the parameter is {@code null}
090     * @since 3871
091     */
092    public static void ensureParameterNotNull(Object value) {
093        if (value == null)
094            throw new IllegalArgumentException("Parameter must not be null");
095    }
096
097    /**
098     * Ensures that the condition {@code condition} holds.
099     * @param condition The condition to check
100     * @throws IllegalArgumentException if the condition does not hold
101     */
102    public static void ensureThat(boolean condition, String message) {
103        if (!condition)
104            throw new IllegalArgumentException(message);
105    }
106
107    /**
108     * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
109     *
110     * @param id the primitive  id
111     * @param parameterName the name of the parameter to be checked
112     * @throws IllegalArgumentException if id is null
113     * @throws IllegalArgumentException if id.getType() != NODE
114     */
115    public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
116        ensureParameterNotNull(id, parameterName);
117        if (!id.getType().equals(OsmPrimitiveType.NODE))
118            throw new IllegalArgumentException(
119                    MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
120    }
121}