001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.tools;
003    
004    import java.text.MessageFormat;
005    
006    import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
007    import org.openstreetmap.josm.data.osm.PrimitiveId;
008    
009    /**
010     * This utility class provides a collection of static helper methods for checking
011     * parameters at run-time.
012     *
013     */
014    public class CheckParameterUtil {
015    
016        private CheckParameterUtil(){}
017    
018        public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
019            ensureParameterNotNull(id, parameterName);
020            if (id.getUniqueId() <= 0)
021                throw new IllegalArgumentException(MessageFormat.format("Expected unique id > 0 for primitive ''{1}'', got {0}", id.getUniqueId(), parameterName));
022        }
023    
024        public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException {
025            if (version < 0)
026                throw new IllegalArgumentException(MessageFormat.format("Expected value of type long > 0 for parameter ''{0}'', got {1}", parameterName, version));
027        }
028    
029        public static void ensureParameterNotNull(Object value, String parameterName) {
030            if (value == null)
031                throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
032        }
033    
034        /**
035         * can find line number in the stack trace, so parameter name is optional
036         */
037        public static void ensureParameterNotNull(Object value) {
038            if (value == null)
039                throw new IllegalArgumentException("Parameter must not be null");
040        }
041    
042        /**
043         * Ensures that <code>id</code> is non-null primitive id of type {@link OsmPrimitiveType#NODE}
044         *
045         * @param id  the primitive  id
046         * @param parameterName the name of the parameter to be checked
047         * @throws IllegalArgumentException thrown if id is null
048         * @throws IllegalArgumentException thrown if id.getType() != NODE
049         */
050        public static void ensureValidNodeId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
051            ensureParameterNotNull(id, parameterName);
052            if (! id.getType().equals(OsmPrimitiveType.NODE))
053                throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' of type node expected, got ''{1}''", parameterName, id.getType().getAPIName()));
054        }
055    }