Ticket #21646: 21646.equality.patch

File 21646.equality.patch, 2.9 KB (added by taylor.smock, 4 years ago)

Modify equality to use MAX_SERVER_PRECISION as epsilon for coordinate differences

  • src/org/openstreetmap/josm/data/imagery/Shape.java

    diff --git a/src/org/openstreetmap/josm/data/imagery/Shape.java b/src/org/openstreetmap/josm/data/imagery/Shape.java
    index 9e7fa16076..81783e87b7 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.imagery;
    33
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
    46import java.awt.Polygon;
    57import java.text.MessageFormat;
    68import java.util.AbstractList;
    import org.openstreetmap.gui.jmapviewer.Coordinate;  
    1315import org.openstreetmap.josm.data.coor.LatLon;
    1416import org.openstreetmap.josm.tools.CheckParameterUtil;
    1517
    16 import static org.openstreetmap.josm.tools.I18n.tr;
    17 
    1818/**
    1919 * Data class to store the outline for background imagery coverage.
    2020 *
    public class Shape {  
    8585        };
    8686    }
    8787
     88    /**
     89     * Check if the coordinates are inside this shape.
     90     * @see Polygon#contains(int, int)
     91     * @param latlon The latlon to look for
     92     * @return {@code true} if the LatLon is inside the shape.
     93     */
    8894    public boolean contains(LatLon latlon) {
    8995        return coords.contains(
    9096                latlon.getX() * LatLon.MAX_SERVER_INV_PRECISION,
    public class Shape {  
    128134        if (this == obj) return true;
    129135        if (obj == null || getClass() != obj.getClass()) return false;
    130136        Shape shape = (Shape) obj;
    131         return Objects.equals(getPoints(), shape.getPoints());
     137        return Objects.equals(getPoints(), shape.getPoints()) || checkPointsEqualEpsilon(getPoints(), shape.getPoints());
     138    }
     139
     140    /**
     141     * Check if two point lists are equal to OSM precision
     142     * @param pointsOne The first list of points
     143     * @param pointsTwo The second list of points
     144     * @return {@code true} if they are equal within OSM precision
     145     */
     146    private static boolean checkPointsEqualEpsilon(List<Coordinate> pointsOne, List<Coordinate> pointsTwo) {
     147        if (pointsOne.size() == pointsTwo.size()) {
     148            for (int i = 0; i < pointsOne.size(); i++) {
     149                final Coordinate one = pointsOne.get(i);
     150                final Coordinate two = pointsTwo.get(i);
     151                if (!one.equals(two) &&
     152                        !equalsEpsilon(one.getLat(), two.getLat()) && !equalsEpsilon(one.getLon(), two.getLon())) {
     153                    return false;
     154                }
     155            }
     156            return true;
     157        }
     158        return false;
     159    }
     160
     161    /**
     162     * Check if two doubles are equal based off of {@link LatLon#MAX_SERVER_PRECISION}
     163     * @param first The first double
     164     * @param second The second double
     165     * @return {@code true} if they are equal to OSM precision
     166     */
     167    private static boolean equalsEpsilon(final double first, final double second) {
     168        return Math.abs(first - second) < LatLon.MAX_SERVER_PRECISION;
    132169    }
    133170
    134171    @Override