Ignore:
Timestamp:
2009-10-27T01:21:32+01:00 (16 years ago)
Author:
Gubaer
Message:

Cleanup in download logic (less global, more encapsulation)

Location:
trunk/src/org/openstreetmap/josm/data
Files:
5 edited

Legend:

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

    r2017 r2327  
    44import java.awt.geom.Rectangle2D;
    55
     6import org.openstreetmap.josm.Main;
    67import org.openstreetmap.josm.data.coor.LatLon;
     8import static org.openstreetmap.josm.tools.I18n.tr;
    79
    810/**
     
    1618     * The minimum and maximum coordinates.
    1719     */
    18     public LatLon min, max;
     20    private LatLon min, max;
     21   
     22    public LatLon getMin() {
     23        return min;
     24    }
     25
     26    public LatLon getMax() {
     27        return max;
     28    }
    1929
    2030    /**
     
    3040        this.max = b;
    3141    }
     42   
     43    public Bounds(double minlat, double minlon, double maxlat, double maxlon) {
     44        this.min = new LatLon(minlat, minlon);
     45        this.max = new LatLon(maxlat, maxlon);
     46    }
     47   
     48    public Bounds(double [] coords) {
     49        if (coords == null)
     50            throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "coords"));
     51        if (coords.length != 4)
     52            throw new IllegalArgumentException(tr("Expected array of length 4, got {0}", coords.length));
     53        this.min = new LatLon(coords[0], coords[1]);
     54        this.max = new LatLon(coords[2], coords[3]);
     55    }
     56   
     57    public Bounds(String asString, String separator) throws IllegalArgumentException {
     58        if (asString == null)
     59            throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "asString"));
     60        String[] components = asString.split(separator);
     61        if (components.length != 4) {
     62            throw new IllegalArgumentException(tr("Exactly four doubles excpected in string, got {0}", components.length));
     63        }
     64        double[] values = new double[4];
     65        for (int i=0; i<4; i++) {
     66            try {
     67                values[i] = Double.parseDouble(components[i]);
     68            } catch(NumberFormatException e) {
     69                throw new IllegalArgumentException(tr("Illegal double value ''{0}''", components[i]));
     70            }
     71        }
     72        this.min = new LatLon(values[0], values[1]);
     73        this.max = new LatLon(values[2], values[3]);
     74    }
     75   
     76    public Bounds(Bounds other) {
     77        this.min = new LatLon(other.min);
     78        this.max = new LatLon(other.max);
     79    }
    3280
     81    public Bounds(Rectangle2D rect) {
     82        this.min = new LatLon(rect.getMinY(), rect.getMinX());
     83        this.max = new LatLon(rect.getMaxY(), rect.getMaxX());
     84    }
     85   
    3386    @Override public String toString() {
    3487        return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
     
    70123        return new Rectangle2D.Double(min.lon(), min.lat(), max.lon()-min.lon(), max.lat()-min.lat());
    71124    }
     125   
     126    public double getArea() {
     127        return (max.lon() - min.lon()) * (max.lat() - min.lat());
     128    }
     129
     130    public String encodeAsString(String separator) {
     131        StringBuffer sb = new StringBuffer();
     132        sb.append(min.lat()).append(separator).append(min.lon())
     133        .append(separator).append(max.lat()).append(separator)
     134        .append(max.lon());
     135        return sb.toString();
     136    }
     137   
     138    @Override
     139    public int hashCode() {
     140        final int prime = 31;
     141        int result = 1;
     142        result = prime * result + ((max == null) ? 0 : max.hashCode());
     143        result = prime * result + ((min == null) ? 0 : min.hashCode());
     144        return result;
     145    }
     146
     147    @Override
     148    public boolean equals(Object obj) {
     149        if (this == obj)
     150            return true;
     151        if (obj == null)
     152            return false;
     153        if (getClass() != obj.getClass())
     154            return false;
     155        Bounds other = (Bounds) obj;
     156        if (max == null) {
     157            if (other.max != null)
     158                return false;
     159        } else if (!max.equals(other.max))
     160            return false;
     161        if (min == null) {
     162            if (other.min != null)
     163                return false;
     164        } else if (!min.equals(other.min))
     165            return false;
     166        return true;
     167    }
    72168
    73169}
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r2224 r2327  
    6262        public String name;
    6363        public double[] latlon = new double[4]; // minlat, minlon, maxlat, maxlon
     64       
     65        public Bookmark() {           
     66        }
     67       
     68        public Bookmark(Bounds b) {
     69            if (b == null) {
     70                latlon[0] = 0.0;
     71                latlon[1] = 0.0;
     72                latlon[2] = 0.0;
     73                latlon[3] = 0.0;
     74            } else {
     75                latlon[0] = b.getMin().lat();
     76                latlon[1] = b.getMin().lon();
     77                latlon[2] = b.getMax().lat();
     78                latlon[3] = b.getMax().lon();
     79            }
     80        }
     81       
    6482        @Override public String toString() {
    6583            return name;
    6684        }
     85               
    6786        public int compareTo(Bookmark b) {
    6887            return name.toLowerCase().compareTo(b.name.toLowerCase());
     88        }
     89       
     90        public Bounds asBounds() {
     91            return new Bounds(latlon[0], latlon[1], latlon[2], latlon[3]);
    6992        }
    7093    }
  • trunk/src/org/openstreetmap/josm/data/coor/Coordinate.java

    r1169 r2327  
    4545    }
    4646
     47    @Override
     48    public int hashCode() {
     49        final int prime = 31;
     50        int result = super.hashCode();
     51        long temp;
     52        temp = java.lang.Double.doubleToLongBits(x);
     53        result = prime * result + (int) (temp ^ (temp >>> 32));
     54        temp = java.lang.Double.doubleToLongBits(y);
     55        result = prime * result + (int) (temp ^ (temp >>> 32));
     56        return result;
     57    }
     58
     59    @Override
     60    public boolean equals(Object obj) {
     61        if (this == obj)
     62            return true;
     63        if (!super.equals(obj))
     64            return false;
     65        if (getClass() != obj.getClass())
     66            return false;
     67        Coordinate other = (Coordinate) obj;
     68        if (java.lang.Double.doubleToLongBits(x) != java.lang.Double.doubleToLongBits(other.x))
     69            return false;
     70        if (java.lang.Double.doubleToLongBits(y) != java.lang.Double.doubleToLongBits(other.y))
     71            return false;
     72        return true;
     73    }
    4774}
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r2174 r2327  
    9090    public boolean isOutSideWorld() {
    9191        Bounds b = Main.proj.getWorldBoundsLatLon();
    92         return lat() < b.min.lat() || lat() > b.max.lat() ||
    93         lon() < b.min.lon() || lon() > b.max.lon();
     92        return lat() < b.getMin().lat() || lat() > b.getMax().lat() ||
     93        lon() < b.getMin().lon() || lon() > b.getMax().lon();
    9494    }
    9595
     
    9898     */
    9999    public boolean isWithin(Bounds b) {
    100         return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon();
     100        return lat() >= b.getMin().lat() && lat() <= b.getMax().lat() && lon() > b.getMin().lon() && lon() < b.getMax().lon();
    101101    }
    102102
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/BoundingXYVisitor.java

    r1946 r2327  
    4545        if(b != null)
    4646        {
    47             visit(b.min);
    48             visit(b.max);
     47            visit(b.getMin());
     48            visit(b.getMax());
    4949        }
    5050    }
Note: See TracChangeset for help on using the changeset viewer.