Changeset 5980 in josm


Ignore:
Timestamp:
2013-06-01T20:39:28+02:00 (12 years ago)
Author:
Don-vip
Message:

see #8631 - Sanity checks in geometry functions dealing with coordinates + javadoc

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java

    r5266 r5980  
    44import java.text.MessageFormat;
    55
     6import org.openstreetmap.josm.data.coor.EastNorth;
     7import org.openstreetmap.josm.data.coor.LatLon;
    68import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    79import org.openstreetmap.josm.data.osm.PrimitiveId;
     
    1012 * This utility class provides a collection of static helper methods for checking
    1113 * parameters at run-time.
    12  *
     14 * @ince 2711
    1315 */
    1416public class CheckParameterUtil {
     
    1618    private CheckParameterUtil(){}
    1719
     20    /**
     21     * Ensures an OSM primitive ID is valid
     22     * @param id The id to check
     23     * @param parameterName The parameter name
     24     * @throws IllegalArgumentException if the primitive ID is not valid (negative or zero)
     25     */
    1826    public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) throws IllegalArgumentException {
    1927        ensureParameterNotNull(id, parameterName);
     
    2230    }
    2331
     32    /**
     33     * Ensures lat/lon coordinates are valid
     34     * @param latlon The lat/lon to check
     35     * @param parameterName The parameter name
     36     * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
     37     * @since 5980
     38     */
     39    public static void ensureValidCoordinates(LatLon latlon, String parameterName) throws IllegalArgumentException {
     40        ensureParameterNotNull(latlon, parameterName);
     41        if (!latlon.isValid())
     42            throw new IllegalArgumentException(MessageFormat.format("Expected valid lat/lon for parameter ''{0}'', got {1}", parameterName, latlon));
     43    }
     44
     45    /**
     46     * Ensures east/north coordinates are valid
     47     * @param eastnorth The east/north to check
     48     * @param parameterName The parameter name
     49     * @throws IllegalArgumentException if the east/north are {@code null} or not valid
     50     * @since 5980
     51     */
     52    public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) throws IllegalArgumentException {
     53        ensureParameterNotNull(eastnorth, parameterName);
     54        if (!eastnorth.isValid())
     55            throw new IllegalArgumentException(MessageFormat.format("Expected valid east/north for parameter ''{0}'', got {1}", parameterName, eastnorth));
     56    }
     57
     58    /**
     59     * Ensures a version number is valid
     60     * @param version The version to check
     61     * @param parameterName The parameter name
     62     * @throws IllegalArgumentException if the version is not valid (negative)
     63     */
    2464    public static void ensureValidVersion(long version, String parameterName) throws IllegalArgumentException {
    2565        if (version < 0)
     
    2767    }
    2868
    29     public static void ensureParameterNotNull(Object value, String parameterName) {
     69    /**
     70     * Ensures a parameter is not {@code null}
     71     * @param value The parameter to check
     72     * @param parameterName The parameter name
     73     * @throws IllegalArgumentException if the parameter is {@code null}
     74     */
     75    public static void ensureParameterNotNull(Object value, String parameterName) throws IllegalArgumentException {
    3076        if (value == null)
    3177            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
     
    3379
    3480    /**
    35      * can find line number in the stack trace, so parameter name is optional
     81     * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
     82     * @param value The parameter to check
     83     * @throws IllegalArgumentException if the parameter is {@code null}
     84     * @since 3871
    3685     */
    37     public static void ensureParameterNotNull(Object value) {
     86    public static void ensureParameterNotNull(Object value) throws IllegalArgumentException {
    3887        if (value == null)
    3988            throw new IllegalArgumentException("Parameter must not be null");
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r5698 r5980  
    247247     * @return EastNorth null if no intersection was found, the EastNorth coordinates of the intersection otherwise
    248248     */
    249     public static EastNorth getSegmentSegmentIntersection(
    250             EastNorth p1, EastNorth p2,
    251             EastNorth p3, EastNorth p4) {
     249    public static EastNorth getSegmentSegmentIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
     250       
     251        CheckParameterUtil.ensureValidCoordinates(p1, "p1");
     252        CheckParameterUtil.ensureValidCoordinates(p2, "p2");
     253        CheckParameterUtil.ensureValidCoordinates(p3, "p3");
     254        CheckParameterUtil.ensureValidCoordinates(p4, "p4");
     255       
    252256        double x1 = p1.getX();
    253257        double y1 = p1.getY();
     
    284288     * Finds the intersection of two lines of infinite length.
    285289     * @return EastNorth null if no intersection was found, the coordinates of the intersection otherwise
     290     * @throws IllegalArgumentException if a parameter is null or without valid coordinates
    286291     */
    287292    public static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    288293
     294        CheckParameterUtil.ensureValidCoordinates(p1, "p1");
     295        CheckParameterUtil.ensureValidCoordinates(p2, "p2");
     296        CheckParameterUtil.ensureValidCoordinates(p3, "p3");
     297        CheckParameterUtil.ensureValidCoordinates(p4, "p4");
     298       
     299        if (!p1.isValid()) throw new IllegalArgumentException();
     300       
    289301        // Convert line from (point, point) form to ax+by=c
    290302        double a1 = p2.getY() - p1.getY();
     
    305317
    306318    public static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
     319
     320        CheckParameterUtil.ensureValidCoordinates(p1, "p1");
     321        CheckParameterUtil.ensureValidCoordinates(p2, "p2");
     322        CheckParameterUtil.ensureValidCoordinates(p3, "p3");
     323        CheckParameterUtil.ensureValidCoordinates(p4, "p4");
     324
    307325        // Convert line from (point, point) form to ax+by=c
    308326        double a1 = p2.getY() - p1.getY();
     
    384402     */
    385403    public static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode) {
     404       
     405        CheckParameterUtil.ensureValidCoordinates(commonNode, "commonNode");
     406        CheckParameterUtil.ensureValidCoordinates(firstNode, "firstNode");
     407        CheckParameterUtil.ensureValidCoordinates(secondNode, "secondNode");
     408       
    386409        double dy1 = (firstNode.getY() - commonNode.getY());
    387410        double dy2 = (secondNode.getY() - commonNode.getY());
     
    583606     */
    584607    public static double getSegmentAngle(EastNorth p1, EastNorth p2) {
     608       
     609        CheckParameterUtil.ensureValidCoordinates(p1, "p1");
     610        CheckParameterUtil.ensureValidCoordinates(p2, "p2");
     611       
    585612        return Math.atan2(p2.north() - p1.north(), p2.east() - p1.east());
    586613    }
     
    595622     */
    596623    public static double getCornerAngle(EastNorth p1, EastNorth p2, EastNorth p3) {
     624       
     625        CheckParameterUtil.ensureValidCoordinates(p1, "p1");
     626        CheckParameterUtil.ensureValidCoordinates(p2, "p2");
     627        CheckParameterUtil.ensureValidCoordinates(p3, "p3");
     628       
    597629        Double result = getSegmentAngle(p2, p1) - getSegmentAngle(p2, p3);
    598630        if (result <= -Math.PI) {
     
    651683     * @return Intersection coordinate or null
    652684     */
    653     public static EastNorth getSegmentAltituteIntersection(EastNorth sp1,
    654             EastNorth sp2, EastNorth ap) {
     685    public static EastNorth getSegmentAltituteIntersection(EastNorth sp1, EastNorth sp2, EastNorth ap) {
     686       
     687        CheckParameterUtil.ensureValidCoordinates(sp1, "sp1");
     688        CheckParameterUtil.ensureValidCoordinates(sp2, "sp2");
     689        CheckParameterUtil.ensureValidCoordinates(ap, "ap");
     690
    655691        Double segmentLenght = sp1.distance(sp2);
    656692        Double altitudeAngle = getSegmentAngle(sp1, sp2) + Math.PI / 2;
Note: See TracChangeset for help on using the changeset viewer.