Changeset 10334 in josm


Ignore:
Timestamp:
2016-06-07T21:26:44+02:00 (8 years ago)
Author:
Don-vip
Message:

fix equals methods for Coordinate classes

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/CachedLatLon.java

    r8846 r10334  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.coor;
     3
     4import java.util.Objects;
    35
    46import org.openstreetmap.josm.Main;
     
    5456     */
    5557    public final EastNorth getEastNorth() {
    56         if (proj != Main.getProjection()) {
     58        if (!Objects.equals(proj, Main.getProjection())) {
    5759            proj = Main.getProjection();
    5860            eastNorth = proj.latlon2eastNorth(this);
     
    6264
    6365    @Override
     66    public int hashCode() {
     67        return Objects.hash(x, y, eastNorth);
     68    }
     69
     70    @Override
     71    public boolean equals(Object obj) {
     72        if (!super.equals(obj))
     73            return false;
     74        CachedLatLon other = (CachedLatLon) obj;
     75        return Objects.equals(eastNorth, other.eastNorth);
     76    }
     77
     78    @Override
    6479    public String toString() {
    6580        return "CachedLatLon[lat="+lat()+",lon="+lon()+']';
  • trunk/src/org/openstreetmap/josm/data/coor/EastNorth.java

    r9243 r10334  
    1313    private static final long serialVersionUID = 1L;
    1414
     15    /**
     16     * Constructs a new {@code EastNorth}.
     17     * @param east easting
     18     * @param north northing
     19     */
    1520    public EastNorth(double east, double north) {
    1621        super(east, north);
    1722    }
    1823
     24    /**
     25     * Returns easting.
     26     * @return easting
     27     */
    1928    public double east() {
    2029        return x;
    2130    }
    2231
     32    /**
     33     * Returns northing.
     34     * @return northing
     35     */
    2336    public double north() {
    2437        return y;
     
    5366    }
    5467
     68    /**
     69     * Scales this {@link EastNorth} instance to a given factor and returns the result.
     70     * @param s factor
     71     * @return The result.
     72     */
    5573    public EastNorth scale(double s) {
    5674        return new EastNorth(s * x, s * y);
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r10181 r10334  
    5656    public static final LatLon ZERO = new LatLon(0, 0);
    5757
    58     /**
    59      * North and south pole.
    60      */
     58    /** North pole. */
    6159    public static final LatLon NORTH_POLE = new LatLon(90, 0);
     60    /** South pole. */
    6261    public static final LatLon SOUTH_POLE = new LatLon(-90, 0);
    6362
     
    227226    }
    228227
    229 
    230228    /**
    231229     * Returns the latitude, i.e., the north-south position in degrees.
     
    487485    public boolean equals(Object obj) {
    488486        if (this == obj) return true;
    489         if (!(obj instanceof LatLon)) return false;
     487        if (obj == null || getClass() != obj.getClass()) return false;
    490488        LatLon that = (LatLon) obj;
    491489        return Double.compare(that.x, x) == 0 &&
    492                 Double.compare(that.y, y) == 0;
     490               Double.compare(that.y, y) == 0;
    493491    }
    494492
  • trunk/test/unit/org/openstreetmap/josm/data/coor/LatLonTest.java

    r10222 r10334  
    33
    44import static org.junit.Assert.assertEquals;
     5
     6import java.text.DecimalFormat;
    57
    68import org.junit.Before;
     
    911
    1012import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     13import nl.jqno.equalsverifier.EqualsVerifier;
    1114
    1215/**
     
    120123
    121124    /**
    122      * Test of {@link LatLon#equals}
     125     * Unit test of methods {@link LatLon#equals} and {@link LatLon#hashCode}.
    123126     */
    124127    @Test
    125     public void testEquals() {
    126         for (int i = 1; i < SAMPLE_VALUES.length; i++) {
    127             LatLon a = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
    128             LatLon b = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
    129             assertEquals(a, b);
    130         }
    131     }
    132 
    133     /**
    134      * Test of {@link LatLon#hashCode}
    135      */
    136     @Test
    137     public void testHashCode() {
    138         for (int i = 1; i < SAMPLE_VALUES.length; i++) {
    139             LatLon a = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
    140             LatLon b = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
    141             assertEquals(a.hashCode(), b.hashCode());
    142         }
     128    public void equalsContract() {
     129        EqualsVerifier.forClass(LatLon.class).usingGetClass()
     130            .withPrefabValues(DecimalFormat.class, new DecimalFormat("00.0"), new DecimalFormat("00.000"))
     131            .verify();
    143132    }
    144133
Note: See TracChangeset for help on using the changeset viewer.