Changeset 11272 in josm


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
Location:
trunk
Files:
5 edited

Legend:

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

    r10910 r11272  
    2727    private double minLat, minLon, maxLat, maxLon;
    2828
     29    /**
     30     * Gets the point that has both the minimal lat and lon coordinate
     31     * @return The point
     32     */
    2933    public LatLon getMin() {
    3034        return new LatLon(minLat, minLon);
     
    5155    }
    5256
     57    /**
     58     * Gets the point that has both the maximum lat and lon coordinate
     59     * @return The point
     60     */
    5361    public LatLon getMax() {
    5462        return new LatLon(maxLat, maxLon);
  • 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
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r11269 r11272  
    14221422     * @param box a bbox instance
    14231423     * @param visited a set of visited members  or null
     1424     * @since 11269
    14241425     */
    14251426    protected abstract void addToBBox(BBox box, Set<PrimitiveId> visited);
  • trunk/src/org/openstreetmap/josm/data/osm/QuadBuckets.java

    r11269 r11272  
    314314            QBLevel<T>[] children = getChildren();
    315315            for (int i = 0; i < QuadTiling.TILES_PER_LEVEL; i++) {
    316                 if (children[i] == findThis)
     316                if (children[i] == findThis) {
    317317                    return i;
     318                }
    318319            }
    319320            return -1;
     
    344345
    345346        boolean canRemove() {
    346             if (content != null && !content.isEmpty())
    347                 return false;
    348             if (this.hasChildren())
    349                 return false;
    350             return true;
     347            return (content == null || content.isEmpty()) && !this.hasChildren();
    351348        }
    352349    }
     
    374371    @Override
    375372    public boolean add(T n) {
    376         if (n.getBBox().isValid())
     373        if (n.getBBox().isValid()) {
    377374            root.add(n);
    378         else
     375        } else {
    379376            invalidBBoxPrimitives.add(n);
     377        }
    380378        size++;
    381379        return true;
     
    388386                continue;
    389387            }
    390             if (!this.remove(o))
     388            if (!this.remove(o)) {
    391389                return false;
     390            }
    392391        }
    393392        return true;
     
    415414    public boolean containsAll(Collection<?> objects) {
    416415        for (Object o : objects) {
    417             if (!this.contains(o))
     416            if (!this.contains(o)) {
    418417                return false;
     418            }
    419419        }
    420420        return true;
     
    428428        QBLevel<T> bucket = root.findBucket(t.getBBox());
    429429        boolean removed = bucket.removeContent(t);
    430         if (!removed)
     430        if (!removed) {
    431431            removed = invalidBBoxPrimitives.remove(o);
    432         if (removed)
     432        }
     433        if (removed) {
    433434            size--;
     435        }
    434436        return removed;
    435437    }
     
    439441        @SuppressWarnings("unchecked")
    440442        T t = (T) o;
    441         if (!t.getBBox().isValid())
     443        if (!t.getBBox().isValid()) {
    442444            return invalidBBoxPrimitives.contains(o);
     445        }
    443446        QBLevel<T> bucket = root.findBucket(t.getBBox());
    444447        return bucket != null && bucket.content != null && bucket.content.contains(t);
     
    521524        @Override
    522525        public T next() {
    523             if (fromInvalidBBoxPrimitives)
     526            if (fromInvalidBBoxPrimitives) {
    524527                return invalidBBoxIterator.next();
     528            }
    525529            T ret = peek();
    526530            if (ret == null)
  • trunk/test/unit/org/openstreetmap/josm/data/osm/BBoxTest.java

    r11269 r11272  
    5252        assertTrue(b1.bounds(b2));
    5353
    54         // invalid latlon values
    55         LatLon invalid1 = new LatLon(-190, 340);
    56         BBox b3 = new BBox(invalid1, latLon1);
    57         BBox b4 = new BBox(latLon1, invalid1);
    58         BBox b5 = new BBox(invalid1, invalid1);
    59         // what should be the result?
     54        // outside of world latlon values
     55        LatLon outOfWorld = new LatLon(-190, 340);
     56        BBox b3 = new BBox(outOfWorld, latLon1);
     57        BBox b4 = new BBox(latLon1, outOfWorld);
     58        BBox b5 = new BBox(outOfWorld, outOfWorld);
     59
    6060        assertTrue(b3.isValid());
    6161        assertTrue(b4.isValid());
     
    6363        assertTrue(b4.bounds(latLon1));
    6464        assertTrue(b5.isValid());
     65        assertFalse(b3.isInWorld());
     66        assertFalse(b4.isInWorld());
    6567        assertFalse(b5.isInWorld());
    6668    }
Note: See TracChangeset for help on using the changeset viewer.