Ignore:
Timestamp:
2016-11-17T21:00:49+01:00 (7 years ago)
Author:
michael2402
Message:

See #13361: Cleanup code and formatting

  • Add javadoc to public methods
  • Add @since tag to newer methods
  • Do not use if (...) return true; else return false;
  • Remove parentheses if they are not required
  • Use blocks for if-statements which is more consistent with most of JOSM code
File:
1 edited

Legend:

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

    r11269 r11272  
    66import java.util.Objects;
    77
     8import org.openstreetmap.josm.data.Bounds;
    89import org.openstreetmap.josm.data.coor.LatLon;
    910import org.openstreetmap.josm.data.coor.QuadTiling;
    1011import org.openstreetmap.josm.tools.Utils;
    1112
     13/**
     14 * A BBox represents an area in lat/lon space. It is used for the quad tree.
     15 *
     16 * In contrast to a {@link Bounds} object, a BBox can represent an invalid (empty) area.
     17 */
    1218public class BBox {
    1319
     
    3036     */
    3137    public BBox(final double x, final double y) {
    32         if (!Double.isNaN(x) && !Double.isNaN(y)) {
    33             xmin = x;
    34             ymin = y;
    35             xmax = x;
    36             ymax = y;
    37         }
     38        add(x, y);
    3839    }
    3940
     
    6970     */
    7071    public BBox(double ax, double ay, double bx, double by) {
    71         if (Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by)) {
    72             return; // use default which is an invalid BBox
    73         }
    74 
    75         if (ax > bx) {
    76             xmax = ax;
    77             xmin = bx;
    78         } else {
    79             xmax = bx;
    80             xmin = ax;
    81         }
    82 
    83         if (ay > by) {
    84             ymax = ay;
    85             ymin = by;
    86         } else {
    87             ymax = by;
    88             ymin = ay;
    89         }
     72        if (!(Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by))) {
     73            add(ax, ay);
     74            add(bx, by);
     75        }
     76        // otherwise use default which is an invalid BBox
    9077    }
    9178
     
    9683     */
    9784    public BBox(Way w) {
    98         w.getNodes().forEach((n) -> add(n.getCoor()));
     85        w.getNodes().forEach(n -> add(n.getCoor()));
    9986    }
    10087
     
    10491     */
    10592    public BBox(Node n) {
    106         if (n.isLatLonKnown())
     93        if (n.isLatLonKnown()) {
    10794            add(n.getCoor());
     95        }
    10896    }
    10997
     
    114102     */
    115103    public final void add(LatLon c) {
    116         if (c != null && c.isValid())
     104        if (c != null && c.isValid()) {
    117105            add(c.lon(), c.lat());
     106        }
    118107    }
    119108
     
    124113     */
    125114    public final void add(double x, double y) {
    126         if (Double.isNaN(x) || Double.isNaN(y))
    127             return;
    128         xmin = Math.min(xmin, x);
    129         xmax = Math.max(xmax, x);
    130         ymin = Math.min(ymin, y);
    131         ymax = Math.max(ymax, y);
     115        if (!Double.isNaN(x) && !Double.isNaN(y)) {
     116            xmin = Math.min(xmin, x);
     117            xmax = Math.max(xmax, x);
     118            ymin = Math.min(ymin, y);
     119            ymax = Math.max(ymax, y);
     120        }
    132121    }
    133122
     
    156145    }
    157146
     147    /**
     148     * Gets the height of the bbox.
     149     * @return The difference between ymax and ymin. 0 for invalid bboxes.
     150     */
    158151    public double height() {
    159         return ymax-ymin;
    160     }
    161 
     152        if (isValid()) {
     153            return ymax - ymin;
     154        } else {
     155            return 0;
     156        }
     157    }
     158
     159    /**
     160     * Gets the width of the bbox.
     161     * @return The difference between xmax and xmin. 0 for invalid bboxes.
     162     */
    162163    public double width() {
    163         return xmax-xmin;
     164        if (isValid()) {
     165            return xmax - xmin;
     166        } else {
     167            return 0;
     168        }
    164169    }
    165170
     
    191196     */
    192197    public boolean intersects(BBox b) {
    193         if (xmin > b.xmax)
    194             return false;
    195         if (xmax < b.xmin)
    196             return false;
    197         if (ymin > b.ymax)
    198             return false;
    199         if (ymax < b.ymin)
    200             return false;
    201         return true;
     198        return xmin <= b.xmax && xmax >= b.xmin
     199            && ymin <= b.ymax && ymax >= b.ymin;
    202200    }
    203201
     
    254252    }
    255253
     254    /**
     255     * Gets the center of this BBox.
     256     * @return The center.
     257     */
    256258    public LatLon getCenter() {
    257259        return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
     
    298300     * @return true if the bbox covers a part of the planets surface
    299301     * Height and width must be non-negative, but may (both) be 0.
     302     * @since 11269
    300303     */
    301304    public boolean isValid() {
    302         return (xmin <= xmax && ymin <= ymax);
    303     }
    304 
    305     /**
    306      * @return true if the bbox covers a part of the planets surface
     305        return xmin <= xmax && ymin <= ymax;
     306    }
     307
     308    /**
     309     * @return true if the bbox  is avalid and covers a part of the planets surface
     310     * @since 11269
    307311     */
    308312    public boolean isInWorld() {
    309         return !(xmin < -180.0 || xmax > 180.0 || ymin < -90.0 || ymax > 90.0);
     313        return isValid() && xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0;
    310314    }
    311315
Note: See TracChangeset for help on using the changeset viewer.