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

Cleanup in download logic (less global, more encapsulation)

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.