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) throws IllegalArgumentException { 029 ensureParameterNotNull(id, parameterName); 030 if (id.getUniqueId() <= 0) 031 throw new IllegalArgumentException(MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName)); 032 } 033 034 /** 035 * Ensures lat/lon coordinates are valid 036 * @param latlon The lat/lon to check 037 * @param parameterName The parameter name 038 * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid 039 * @since 5980 040 */ 041 public static void ensureValidCoordinates(LatLon latlon, String parameterName) throws IllegalArgumentException { 042 ensureParameterNotNull(latlon, parameterName); 043 if (!latlon.isValid()) 044 throw new IllegalArgumentException(MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon)); 045 } 046 047 /** 048 * Ensures east/north coordinates are valid 049 * @param eastnorth The east/north to check 050 * @param parameterName The parameter name 051 * @throws IllegalArgumentException if the east/north are {@code null} or not valid 052 * @since 5980 053 */ 054 public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) throws IllegalArgumentException { 055 ensureParameterNotNull(eastnorth, parameterName); 056 if (!eastnorth.isValid()) 057 throw new IllegalArgumentException(MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth)); 058 } 059 060 /** 061 * Ensures a version number is valid 062 * @param version The version to check 063 * @param parameterName The parameter name 064 * @throws IllegalArgumentException if the version is not valid (negative) 065 */ 066 public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException { 067 if (version < 0) 068 throw new IllegalArgumentException(MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version)); 069 } 070 071 /** 072 * Ensures a parameter is not {@code null} 073 * @param value The parameter to check 074 * @param parameterName The parameter name 075 * @throws IllegalArgumentException if the parameter is {@code null} 076 */ 077 public static void ensureParameterNotNull(Object value, String parameterName) throws IllegalArgumentException { 078 if (value == null) 079 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName)); 080 } 081 082 /** 083 * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional 084 * @param value The parameter to check 085 * @throws IllegalArgumentException if the parameter is {@code null} 086 * @since 3871 087 */ 088 public static void ensureParameterNotNull(Object value) throws IllegalArgumentException { 089 if (value == null) 090 throw new IllegalArgumentException("Parameter must not be null"); 091 } 092 093 /** 094 * Ensures that the condition {@code condition} holds. 095 * @param condition The condition to check 096 * @throws IllegalArgumentException if the condition does not hold 097 */ 098 public static void ensureThat(boolean condition, String message) throws IllegalArgumentException { 099 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 thrown if id is null 109 * @throws IllegalArgumentException thrown 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}