Ignore:
Timestamp:
2026-11-23T06:35:25+01:00 (6 hours ago)
Author:
GerdP
Message:

fix #25042: Fix Sonar issues in Geometry
patch by DanProgs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r19446 r19703  
    8787    public static final double INTERSECTION_EPS_EAST_NORTH = 1e-4;
    8888
     89    private static final String INVALID_COORDINATE_MSG = " invalid";
     90
    8991    /**
    9092     * Will find all intersection and add nodes there for list of given ways.
    … …  
    103105    public static Set<Node> addIntersections(List<Way> ways, boolean test, List<Command> cmds) {
    104106
    105         int n = ways.size();
     107        final int n = ways.size();
    106108        @SuppressWarnings("unchecked")
     109        final
    107110        List<Node>[] newNodes = new ArrayList[n];
    108         BBox[] wayBounds = new BBox[n];
    109         boolean[] changedWays = new boolean[n];
    110 
    111         Set<Node> intersectionNodes = new LinkedHashSet<>();
     111        final BBox[] wayBounds = new BBox[n];
     112        final boolean[] changedWays = new boolean[n];
     113
     114        final Set<Node> intersectionNodes = new LinkedHashSet<>();
    112115
    113116        //copy node arrays for local usage.
    … …  
    118121        }
    119122
    120         DataSet dataset = ways.get(0).getDataSet();
     123        final DataSet dataset = ways.get(0).getDataSet();
    121124
    122125        //iterate over all way pairs and introduce the intersections
    123         Comparator<Node> coordsComparator = new NodePositionComparator();
     126        final Comparator<Node> coordsComparator = new NodePositionComparator();
    124127        for (int seg1Way = 0; seg1Way < n; seg1Way++) {
    125128            for (int seg2Way = seg1Way; seg2Way < n; seg2Way++) {
    … …  
    130133                }
    131134
    132                 List<Node> way1Nodes = newNodes[seg1Way];
    133                 List<Node> way2Nodes = newNodes[seg2Way];
     135                final List<Node> way1Nodes = newNodes[seg1Way];
     136                final List<Node> way2Nodes = newNodes[seg2Way];
    134137
    135138                //iterate over primary segment
    … …  
    137140
    138141                    //iterate over secondary segment
    139                     int seg2Start = seg1Way != seg2Way ? 0 : seg1Pos + 2; //skip the adjacent segment
     142                    final int seg2Start = seg1Way != seg2Way ? 0 : seg1Pos + 2; //skip the adjacent segment
    140143
    141144                    for (int seg2Pos = seg2Start; seg2Pos + 1 < way2Nodes.size(); seg2Pos++) {
    142145
    143146                        //need to get them again every time, because other segments may be changed
    144                         Node seg1Node1 = way1Nodes.get(seg1Pos);
    145                         Node seg1Node2 = way1Nodes.get(seg1Pos + 1);
    146                         Node seg2Node1 = way2Nodes.get(seg2Pos);
    147                         Node seg2Node2 = way2Nodes.get(seg2Pos + 1);
     147                        final Node seg1Node1 = way1Nodes.get(seg1Pos);
     148                        final Node seg1Node2 = way1Nodes.get(seg1Pos + 1);
     149                        final Node seg2Node1 = way2Nodes.get(seg2Pos);
     150                        final Node seg2Node2 = way2Nodes.get(seg2Pos + 1);
    148151
    149152                        int commonCount = 0;
    … …  
    169172                        //no common nodes - find intersection
    170173                        if (commonCount == 0) {
    171                             EastNorth intersection = getSegmentSegmentIntersection(
     174                            final EastNorth intersection = getSegmentSegmentIntersection(
    172175                                    seg1Node1.getEastNorth(), seg1Node2.getEastNorth(),
    173176                                    seg2Node1.getEastNorth(), seg2Node2.getEastNorth());
    174177
    175178                            if (intersection != null) {
    176                                 Node newNode = new Node(ProjectionRegistry.getProjection().eastNorth2latlon(intersection));
     179                                final Node newNode = new Node(ProjectionRegistry.getProjection().eastNorth2latlon(intersection));
    177180                                Node intNode = newNode;
    178181                                boolean insertInSeg1 = false;
    … …  
    227230                                }
    228231                            }
    229                         } else if (test && !intersectionNodes.isEmpty())
     232                        } else if (test && !intersectionNodes.isEmpty()) {
    230233                            return intersectionNodes;
     234                        }
    231235                    }
    232236                }
    … …  
    261265     */
    262266    public static <N extends INode> boolean isToTheRightSideOfLine(N lineP1, N lineP2, N lineP3, N testPoint) {
    263         boolean pathBendToRight = angleIsClockwise(lineP1, lineP2, lineP3);
    264         boolean rightOfSeg1 = angleIsClockwise(lineP1, lineP2, testPoint);
    265         boolean rightOfSeg2 = angleIsClockwise(lineP2, lineP3, testPoint);
    266 
    267         if (pathBendToRight)
     267        final boolean pathBendToRight = angleIsClockwise(lineP1, lineP2, lineP3);
     268        final boolean rightOfSeg1 = angleIsClockwise(lineP1, lineP2, testPoint);
     269        final boolean rightOfSeg2 = angleIsClockwise(lineP2, lineP3, testPoint);
     270
     271        if (pathBendToRight) {
    268272            return rightOfSeg1 && rightOfSeg2;
    269         else
     273        } else {
    270274            return !(!rightOfSeg1 && !rightOfSeg2);
     275        }
    271276    }
    272277
    … …  
    295300        // see the ILatLon version for an explanation why the checks are in the if statement
    296301        if (!(p1.isValid() && p2.isValid() && p3.isValid() && p4.isValid())) {
    297             CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
    298             CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
    299             CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
    300             CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");
    301         }
    302 
    303         double x1 = p1.getX();
    304         double y1 = p1.getY();
    305         double x2 = p2.getX();
    306         double y2 = p2.getY();
    307         double x3 = p3.getX();
    308         double y3 = p3.getY();
    309         double x4 = p4.getX();
    310         double y4 = p4.getY();
    311         double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4);
     302            CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG);
     303            CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG);
     304            CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG);
     305            CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + INVALID_COORDINATE_MSG);
     306        }
     307
     308        final double x1 = p1.getX();
     309        final double y1 = p1.getY();
     310        final double x2 = p2.getX();
     311        final double y2 = p2.getY();
     312        final double x3 = p3.getX();
     313        final double y3 = p3.getY();
     314        final double x4 = p4.getX();
     315        final double y4 = p4.getY();
     316        final double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4);
    312317        if (en != null && en.length == 2) {
    313318            return new EastNorth(en[0], en[1]);
    … …  
    331336        // There was also a 2/3 decrease in CPU samples for the method.
    332337        if (!(p1.isLatLonKnown() && p2.isLatLonKnown() && p3.isLatLonKnown() && p4.isLatLonKnown())) {
    333             CheckParameterUtil.ensureThat(p1.isLatLonKnown(), () -> p1 + " invalid");
    334             CheckParameterUtil.ensureThat(p2.isLatLonKnown(), () -> p2 + " invalid");
    335             CheckParameterUtil.ensureThat(p3.isLatLonKnown(), () -> p3 + " invalid");
    336             CheckParameterUtil.ensureThat(p4.isLatLonKnown(), () -> p4 + " invalid");
    337         }
    338 
    339         double x1 = p1.lon();
    340         double y1 = p1.lat();
    341         double x2 = p2.lon();
    342         double y2 = p2.lat();
    343         double x3 = p3.lon();
    344         double y3 = p3.lat();
    345         double x4 = p4.lon();
    346         double y4 = p4.lat();
    347         double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4);
     338            CheckParameterUtil.ensureThat(p1.isLatLonKnown(), () -> p1 + INVALID_COORDINATE_MSG);
     339            CheckParameterUtil.ensureThat(p2.isLatLonKnown(), () -> p2 + INVALID_COORDINATE_MSG);
     340            CheckParameterUtil.ensureThat(p3.isLatLonKnown(), () -> p3 + INVALID_COORDINATE_MSG);
     341            CheckParameterUtil.ensureThat(p4.isLatLonKnown(), () -> p4 + INVALID_COORDINATE_MSG);
     342        }
     343
     344        final double x1 = p1.lon();
     345        final double y1 = p1.lat();
     346        final double x2 = p2.lon();
     347        final double y2 = p2.lat();
     348        final double x3 = p3.lon();
     349        final double y3 = p3.lat();
     350        final double x4 = p4.lon();
     351        final double y4 = p4.lat();
     352        final double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4);
    348353        if (en != null && en.length == 2) {
    349354            return new LatLon(en[1], en[0]);
    … …  
    369374        //TODO: do this locally.
    370375        //TODO: remove this check after careful testing
    371         if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
     376        if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) {
     377            return null;
     378        }
    372379
    373380        // solve line-line intersection in parametric form:
    … …  
    376383        // if 0<= u,v <=1, intersection exists at ( x1+ (x2-x1)*u, y1 + (y2-y1)*u )
    377384
    378         double a1 = x2 - x1;
    379         double b1 = x3 - x4;
    380         double c1 = x3 - x1;
    381 
    382         double a2 = y2 - y1;
    383         double b2 = y3 - y4;
    384         double c2 = y3 - y1;
     385        final double a1 = x2 - x1;
     386        final double b1 = x3 - x4;
     387        final double c1 = x3 - x1;
     388
     389        final double a2 = y2 - y1;
     390        final double b2 = y3 - y4;
     391        final double c2 = y3 - y1;
    385392
    386393        // Solve the equations
    387         double det = a1*b2 - a2*b1;
    388 
    389         double uu = b2*c1 - b1*c2;
    390         double vv = a1*c2 - a2*c1;
    391         double mag = Math.abs(uu)+Math.abs(vv);
     394        final double det = a1*b2 - a2*b1;
     395
     396        final double uu = b2*c1 - b1*c2;
     397        final double vv = a1*c2 - a2*c1;
     398        final double mag = Math.abs(uu)+Math.abs(vv);
    392399
    393400        if (Math.abs(det) > 1e-12 * mag) {
    394401            double u = uu/det, v = vv/det;
    395402            if (u > -1e-8 && u < 1+1e-8 && v > -1e-8 && v < 1+1e-8) {
    396                 if (u < 0) u = 0;
    397                 if (u > 1) u = 1.0;
     403                if (u < 0) {
     404                    u = 0;
     405                }
     406                if (u > 1) {
     407                    u = 1.0;
     408                }
    398409                return new double[] {x1+a1*u, y1+a2*u};
    399410            } else {
    … …  
    418429    public static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    419430
    420         CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
    421         CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
    422         CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
    423         CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");
     431        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG);
     432        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG);
     433        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG);
     434        CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + INVALID_COORDINATE_MSG);
    424435
    425436        // Basically, the formula from wikipedia is used:
    … …  
    435446
    436447        // Convert line from (point, point) form to ax+by=c
    437         double a1 = p2.getY() - p1.getY();
    438         double b1 = p1.getX() - p2.getX();
    439 
    440         double a2 = p4.getY() - p3.getY();
    441         double b2 = p3.getX() - p4.getX();
     448        final double a1 = p2.getY() - p1.getY();
     449        final double b1 = p1.getX() - p2.getX();
     450
     451        final double a2 = p4.getY() - p3.getY();
     452        final double b2 = p3.getX() - p4.getX();
    442453
    443454        // Solve the equations
    444         double det = a1 * b2 - a2 * b1;
    445         if (det == 0)
     455        final double det = a1 * b2 - a2 * b1;
     456        if (det == 0) {
    446457            return null; // Lines are parallel
    447 
    448         double c2 = (p4.getX() - p1.getX()) * (p3.getY() - p1.getY()) - (p3.getX() - p1.getX()) * (p4.getY() - p1.getY());
     458        }
     459
     460        final double c2 = (p4.getX() - p1.getX()) * (p3.getY() - p1.getY()) - (p3.getX() - p1.getX()) * (p4.getY() - p1.getY());
    449461
    450462        return new EastNorth(b1 * c2 / det + p1.getX(), -a1 * c2 / det + p1.getY());
    … …  
    461473    public static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    462474
    463         CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
    464         CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
    465         CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
    466         CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");
     475        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG);
     476        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG);
     477        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG);
     478        CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + INVALID_COORDINATE_MSG);
    467479
    468480        // Convert line from (point, point) form to ax+by=c
    469         double a1 = p2.getY() - p1.getY();
    470         double b1 = p1.getX() - p2.getX();
    471 
    472         double a2 = p4.getY() - p3.getY();
    473         double b2 = p3.getX() - p4.getX();
     481        final double a1 = p2.getY() - p1.getY();
     482        final double b1 = p1.getX() - p2.getX();
     483
     484        final double a2 = p4.getY() - p3.getY();
     485        final double b2 = p3.getX() - p4.getX();
    474486
    475487        // Solve the equations
    … …  
    485497        CheckParameterUtil.ensureParameterNotNull(point, "point");
    486498
    487         double ldx = p2.getX() - p1.getX();
    488         double ldy = p2.getY() - p1.getY();
     499        final double ldx = p2.getX() - p1.getX();
     500        final double ldy = p2.getY() - p1.getY();
    489501
    490502        //segment zero length
    491         if (ldx == 0 && ldy == 0)
     503        if (ldx == 0 && ldy == 0) {
    492504            return p1;
    493 
    494         double pdx = point.getX() - p1.getX();
    495         double pdy = point.getY() - p1.getY();
    496 
    497         double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
    498 
    499         if (segmentOnly && offset <= 0)
     505        }
     506
     507        final double pdx = point.getX() - p1.getX();
     508        final double pdy = point.getY() - p1.getY();
     509
     510        final double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
     511
     512        if (segmentOnly && offset <= 0) {
    500513            return p1;
    501         else if (segmentOnly && offset >= 1)
     514        } else if (segmentOnly && offset >= 1) {
    502515            return p2;
    503         else
     516        } else {
    504517            return p1.interpolate(p2, offset);
     518        }
    505519    }
    506520
    … …  
    547561    public static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode) {
    548562
    549         CheckParameterUtil.ensureThat(commonNode.isValid(), () -> commonNode + " invalid");
    550         CheckParameterUtil.ensureThat(firstNode.isValid(), () -> firstNode + " invalid");
    551         CheckParameterUtil.ensureThat(secondNode.isValid(), () -> secondNode + " invalid");
    552 
    553         double dy1 = firstNode.getY() - commonNode.getY();
    554         double dy2 = secondNode.getY() - commonNode.getY();
    555         double dx1 = firstNode.getX() - commonNode.getX();
    556         double dx2 = secondNode.getX() - commonNode.getX();
     563        CheckParameterUtil.ensureThat(commonNode.isValid(), () -> commonNode + INVALID_COORDINATE_MSG);
     564        CheckParameterUtil.ensureThat(firstNode.isValid(), () -> firstNode + INVALID_COORDINATE_MSG);
     565        CheckParameterUtil.ensureThat(secondNode.isValid(), () -> secondNode + INVALID_COORDINATE_MSG);
     566
     567        final double dy1 = firstNode.getY() - commonNode.getY();
     568        final double dy2 = secondNode.getY() - commonNode.getY();
     569        final double dx1 = firstNode.getX() - commonNode.getX();
     570        final double dx2 = secondNode.getX() - commonNode.getX();
    557571
    558572        return dy1 * dx2 - dx1 * dy2 > 0;
    … …  
    566580     */
    567581    public static Area getArea(List<? extends INode> polygon) {
    568         Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO, polygon.size());
     582        final Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO, polygon.size());
    569583
    570584        boolean begin = true;
    571585        for (INode n : polygon) {
    572             EastNorth en = n.getEastNorth();
     586            final EastNorth en = n.getEastNorth();
    573587            if (en != null) {
    574588                if (begin) {
    … …  
    595609     */
    596610    public static Path2D buildPath2DLatLon(List<? extends ILatLon> polygon, Path2D path2d) {
    597         Path2D path = path2d != null ? path2d : new Path2D.Double();
     611        final Path2D path = path2d != null ? path2d : new Path2D.Double();
    598612        boolean begin = true;
    599613        for (ILatLon n : polygon) {
    … …  
    622636        }
    623637        if (p instanceof Relation && p.isMultipolygon() && !p.isIncomplete()) {
    624             Multipolygon mp = MultipolygonCache.getInstance().get((Relation) p);
     638            final Multipolygon mp = MultipolygonCache.getInstance().get((Relation) p);
    625639            if (mp.getOpenEnds().isEmpty()) {
    626                 Path2D path = new Path2D.Double();
     640                final Path2D path = new Path2D.Double();
    627641                path.setWindingRule(Path2D.WIND_EVEN_ODD);
    628642                for (PolyData pd : mp.getCombinedPolygons()) {
    … …  
    642656    public static Area getAreaLatLon(Relation multipolygon) {
    643657        final Multipolygon mp = MultipolygonCache.getInstance().get(multipolygon);
    644         Path2D path = new Path2D.Double();
     658        final Path2D path = new Path2D.Double();
    645659        path.setWindingRule(Path2D.WIND_EVEN_ODD);
    646660        for (Multipolygon.PolyData pd : mp.getCombinedPolygons()) {
    … …  
    660674     */
    661675    public static PolygonIntersection polygonIntersection(List<? extends INode> first, List<? extends INode> second) {
    662         Area a1 = getArea(first);
    663         Area a2 = getArea(second);
     676        final Area a1 = getArea(first);
     677        final Area a2 = getArea(second);
    664678        return polygonIntersection(a1, a2, INTERSECTION_EPS_EAST_NORTH);
    665679    }
    … …  
    704718            return new Pair<>(PolygonIntersection.OUTSIDE, new Area());
    705719        }
    706         Area inter = new Area(a1);
     720        final Area inter = new Area(a1);
    707721        inter.intersect(a2);
    708722
    … …  
    727741     */
    728742    private static boolean checkIntersection(Area inter, double eps) {
    729         PathIterator pit = inter.getPathIterator(null);
    730         double[] res = new double[6];
     743        final PathIterator pit = inter.getPathIterator(null);
     744        final double[] res = new double[6];
    731745        Rectangle2D r = new Rectangle2D.Double();
    732746        while (!pit.isDone()) {
    733             int type = pit.currentSegment(res);
     747            final int type = pit.currentSegment(res);
    734748            switch (type) {
    735749            case PathIterator.SEG_MOVETO:
    … …  
    740754                break;
    741755            case PathIterator.SEG_CLOSE:
    742                 if (r.getWidth() > eps || r.getHeight() > eps)
     756                if (r.getWidth() > eps || r.getHeight() > eps) {
    743757                    return true;
     758                }
    744759                break;
    745760            default:
    … …  
    758773     */
    759774    public static boolean nodeInsidePolygon(INode point, List<? extends INode> polygonNodes) {
    760         if (polygonNodes.size() < 2)
     775        if (polygonNodes.size() < 2) {
    761776            return false;
     777        }
    762778
    763779        //iterate each side of the polygon, start with the last segment
    … …  
    790806            }
    791807
    792             EastNorth pEN = point.getEastNorth();
    793             EastNorth opEN = oldPoint.getEastNorth();
    794             EastNorth npEN = newPoint.getEastNorth();
    795             EastNorth p1EN = p1.getEastNorth();
    796             EastNorth p2EN = p2.getEastNorth();
     808            final EastNorth pEN = point.getEastNorth();
     809            final EastNorth opEN = oldPoint.getEastNorth();
     810            final EastNorth npEN = newPoint.getEastNorth();
     811            final EastNorth p1EN = p1.getEastNorth();
     812            final EastNorth p2EN = p2.getEastNorth();
    797813
    798814            if (pEN != null && opEN != null && npEN != null && p1EN != null && p2EN != null) {
    … …  
    878894     */
    879895    public static boolean isClockwise(List<? extends INode> nodes) {
    880         int nodesCount = nodes.size();
     896        final int nodesCount = nodes.size();
    881897        if (nodesCount < 3 || nodes.get(0) != nodes.get(nodesCount - 1)) {
    882898            throw new IllegalArgumentException("Way must be closed to check orientation.");
    … …  
    885901
    886902        for (int node = 1; node <= /*sic! consider last-first as well*/ nodesCount; node++) {
    887             INode coorPrev = nodes.get(node - 1);
    888             INode coorCurr = nodes.get(node % nodesCount);
     903            final INode coorPrev = nodes.get(node - 1);
     904            final INode coorCurr = nodes.get(node % nodesCount);
    889905            area2 += coorPrev.lon() * coorCurr.lat();
    890906            area2 -= coorCurr.lon() * coorPrev.lat();
    … …  
    902918    public static double getSegmentAngle(EastNorth p1, EastNorth p2) {
    903919
    904         CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
    905         CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
     920        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG);
     921        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG);
    906922
    907923        return Math.atan2(p2.north() - p1.north(), p2.east() - p1.east());
    … …  
    918934    public static double getCornerAngle(EastNorth p1, EastNorth common, EastNorth p3) {
    919935
    920         CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
    921         CheckParameterUtil.ensureThat(common.isValid(), () -> common + " invalid");
    922         CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
     936        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG);
     937        CheckParameterUtil.ensureThat(common.isValid(), () -> common + INVALID_COORDINATE_MSG);
     938        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG);
    923939
    924940        double result = getSegmentAngle(common, p1) - getSegmentAngle(common, p3);
    … …  
    978994        // See https://en.wikipedia.org/wiki/Centroid#Of_a_polygon for the equation used here
    979995        for (int i = 0; i < size; i++) {
    980             EastNorth n0 = nodes.get(i);
    981             EastNorth n1 = nodes.get((i+1) % size);
     996            final EastNorth n0 = nodes.get(i);
     997            final EastNorth n1 = nodes.get((i+1) % size);
    982998
    983999            if (n0 != null && n1 != null && n0.isValid() && n1.isValid()) {
    984                 BigDecimal x0 = BigDecimal.valueOf(n0.east());
    985                 BigDecimal y0 = BigDecimal.valueOf(n0.north());
    986                 BigDecimal x1 = BigDecimal.valueOf(n1.east());
    987                 BigDecimal y1 = BigDecimal.valueOf(n1.north());
    988 
    989                 BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128));
     1000                final BigDecimal x0 = BigDecimal.valueOf(n0.east());
     1001                final BigDecimal y0 = BigDecimal.valueOf(n0.north());
     1002                final BigDecimal x1 = BigDecimal.valueOf(n1.east());
     1003                final BigDecimal y1 = BigDecimal.valueOf(n1.north());
     1004
     1005                final BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128));
    9901006
    9911007                area = area.add(k, MathContext.DECIMAL128);
    … …  
    9951011        }
    9961012
    997         BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3
     1013        final BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3
    9981014        area = area.multiply(d, MathContext.DECIMAL128);
    9991015        if (area.compareTo(BigDecimal.ZERO) != 0) {
    … …  
    10241040     */
    10251041    public static EastNorth getCenter(List<? extends INode> nodes) {
    1026         int nc = nodes.size();
    1027         if (nc < 3) return null;
     1042        final int nc = nodes.size();
     1043        if (nc < 3) {
     1044            return null;
     1045        }
    10281046        /*
    10291047         * Equation of each bisector ax + by + c = 0
    10301048         */
    1031         double[] a = new double[nc];
    1032         double[] b = new double[nc];
    1033         double[] c = new double[nc];
     1049        final double[] a = new double[nc];
     1050        final double[] b = new double[nc];
     1051        final double[] c = new double[nc];
    10341052        // Compute equation of bisector
    10351053        for (int i = 0; i < nc; i++) {
    1036             EastNorth pt1 = nodes.get(i).getEastNorth();
    1037             EastNorth pt2 = nodes.get((i+1) % nc).getEastNorth();
     1054            final EastNorth pt1 = nodes.get(i).getEastNorth();
     1055            final EastNorth pt2 = nodes.get((i+1) % nc).getEastNorth();
    10381056            a[i] = pt1.east() - pt2.east();
    10391057            b[i] = pt1.north() - pt2.north();
    1040             double d = Math.sqrt(a[i]*a[i] + b[i]*b[i]);
    1041             if (d == 0) return null;
     1058            final double d = Math.sqrt(a[i]*a[i] + b[i]*b[i]);
     1059            if (d == 0) {
     1060                return null;
     1061            }
    10421062            a[i] /= d;
    10431063            b[i] /= d;
    1044             double xC = (pt1.east() + pt2.east()) / 2;
    1045             double yC = (pt1.north() + pt2.north()) / 2;
     1064            final double xC = (pt1.east() + pt2.east()) / 2;
     1065            final double yC = (pt1.north() + pt2.north()) / 2;
    10461066            c[i] = -(a[i]*xC + b[i]*yC);
    10471067        }
    … …  
    10581078        }
    10591079        // (At.A)^-1 = [invij]
    1060         double det = a11*a22 - a12*a12;
    1061         if (Math.abs(det) < 1e-5) return null;
    1062         double inv11 = a22/det;
    1063         double inv12 = -a12/det;
    1064         double inv22 = a11/det;
     1080        final double det = a11*a22 - a12*a12;
     1081        if (Math.abs(det) < 1e-5) {
     1082            return null;
     1083        }
     1084        final double inv11 = a22/det;
     1085        final double inv12 = -a12/det;
     1086        final double inv22 = a11/det;
    10651087        // center (xC, yC) = (At.A)^-1.At.y
    1066         double xC = inv11*b1 + inv12*b2;
    1067         double yC = inv12*b1 + inv22*b2;
     1088        final double xC = inv11*b1 + inv12*b2;
     1089        final double yC = inv12*b1 + inv22*b2;
    10681090        return new EastNorth(xC, yC);
    10691091    }
    … …  
    11171139    public static boolean isPolygonInsideMultiPolygon(List<? extends INode> nodes, Pair<List<JoinedPolygon>,
    11181140            List<JoinedPolygon>> outerInner, Predicate<Way> isOuterWayAMatch) {
    1119         Area a1 = nodes.size() == 1 ? null : getArea(nodes);
     1141        final Area a1 = nodes.size() == 1 ? null : getArea(nodes);
    11201142        // Test if object is inside an outer member
    11211143        for (JoinedPolygon out : outerInner.a) {
    … …  
    11241146                    : PolygonIntersection.FIRST_INSIDE_SECOND == polygonIntersection(a1, out.area)) {
    11251147                // If inside an outer, check it is not inside an inner
    1126                 boolean insideInner = outerInner.b.stream().anyMatch(in -> a1 == null
     1148                final boolean insideInner = outerInner.b.stream().anyMatch(in -> a1 == null
    11271149                        ? nodeInsidePolygon(nodes.get(0), in.nodes)
    11281150                        : in.area.getBounds2D().contains(a1.getBounds2D())
    … …  
    11701192     */
    11711193    public static List<IPrimitive> filterInsidePolygon(Collection<IPrimitive> primitives, IWay<?> polygon) {
    1172         List<IPrimitive> res = new ArrayList<>();
    1173         if (!polygon.isClosed() || polygon.getNodesCount() <= 3)
     1194        final List<IPrimitive> res = new ArrayList<>();
     1195        if (!polygon.isClosed() || polygon.getNodesCount() <= 3) {
    11741196            return res;
     1197        }
    11751198        /* polygon area in east north space, calculated only when really needed */
    11761199        Area polygonArea = null;
    … …  
    11941217                    polygonArea = getArea(polygon.getNodes());
    11951218                }
    1196                 Multipolygon mp = p.getDataSet() != null ? MultipolygonCache.getInstance().get((Relation) p) : new Multipolygon((Relation) p);
     1219                final Multipolygon mp = p.getDataSet() != null ? MultipolygonCache.getInstance().get((Relation) p)
     1220                        : new Multipolygon((Relation) p);
    11971221                boolean inside = true;
    11981222                // a (valid) multipolygon is inside the polygon if all outer rings are inside
    … …  
    12371261    public static List<IPrimitive> filterInsideMultipolygon(Collection<IPrimitive> primitives, Relation multiPolygon,
    12381262                                                            Map<IRelation<?>, Pair<List<JoinedPolygon>, List<JoinedPolygon>>> cache) {
    1239         List<IPrimitive> res = new ArrayList<>();
    1240         if (primitives.isEmpty())
     1263        final List<IPrimitive> res = new ArrayList<>();
     1264        if (primitives.isEmpty()) {
    12411265            return res;
     1266        }
    12421267
    12431268        final Pair<List<JoinedPolygon>, List<JoinedPolygon>> outerInner;
    … …  
    12501275        }
    12511276
    1252         Set<OsmPrimitive> members = multiPolygon.getMemberPrimitives();
     1277        final Set<OsmPrimitive> members = multiPolygon.getMemberPrimitives();
    12531278        for (IPrimitive p : primitives) {
    1254             if (members.contains(p))
     1279            if (members.contains(p)) {
    12551280                continue;
     1281            }
    12561282            if (p instanceof Node) {
    12571283                if (isPolygonInsideMultiPolygon(Collections.singletonList((Node) p), outerInner, null)) {
    … …  
    12631289                }
    12641290            } else if (p.isMultipolygon()) {
    1265                 Multipolygon mp = new Multipolygon((Relation) p);
     1291                final Multipolygon mp = new Multipolygon((Relation) p);
    12661292                // a (valid) multipolygon is inside multiPolygon if all outer rings are inside
    1267                 boolean inside = mp.getOuterPolygons().stream()
     1293                final boolean inside = mp.getOuterPolygons().stream()
    12681294                        .allMatch(outer -> outer.isClosed() && isPolygonInsideMultiPolygon(outer.getNodes(), outerInner, null));
    12691295                if (inside) {
    … …  
    13331359        double area = 0;
    13341360        double perimeter = 0;
    1335         Projection useProjection = projection == null ? ProjectionRegistry.getProjection() : projection;
     1361        final Projection useProjection = projection == null ? ProjectionRegistry.getProjection() : projection;
    13361362
    13371363        if (!nodes.isEmpty()) {
    1338             boolean closed = nodes.get(0) == nodes.get(nodes.size() - 1);
    1339             int numSegments = closed ? nodes.size() - 1 : nodes.size();
     1364            final boolean closed = nodes.get(0) == nodes.get(nodes.size() - 1);
     1365            final int numSegments = closed ? nodes.size() - 1 : nodes.size();
    13401366            EastNorth p1 = nodes.get(0).getEastNorth(useProjection);
    13411367            for (int i = 1; i <= numSegments; i++) {
    … …  
    13721398     */
    13731399    public static <T extends OsmPrimitive> T getClosestPrimitive(OsmPrimitive osm, Collection<T> primitives) {
    1374         Collection<T> collection = getClosestPrimitives(osm, primitives);
     1400        final Collection<T> collection = getClosestPrimitives(osm, primitives);
    13751401        return collection.iterator().next();
    13761402    }
    … …  
    13951421    public static <T extends OsmPrimitive> Collection<T> getClosestPrimitives(OsmPrimitive osm, Collection<T> primitives) {
    13961422        double lowestDistance = Double.MAX_VALUE;
    1397         TreeSet<T> closest = new TreeSet<>();
     1423        final TreeSet<T> closest = new TreeSet<>();
    13981424        for (T primitive : primitives) {
    1399             double distance = getDistance(osm, primitive);
    1400             if (Double.isNaN(distance)) continue;
    1401             int comp = Double.compare(distance, lowestDistance);
     1425            final double distance = getDistance(osm, primitive);
     1426            if (Double.isNaN(distance)) {
     1427                continue;
     1428            }
     1429            final int comp = Double.compare(distance, lowestDistance);
    14021430            if (comp < 0) {
    14031431                closest.clear();
    … …  
    14591487    public static <T extends OsmPrimitive> Collection<T> getFurthestPrimitives(OsmPrimitive osm, Collection<T> primitives) {
    14601488        double furthestDistance = Double.NEGATIVE_INFINITY;
    1461         TreeSet<T> furthest = new TreeSet<>();
     1489        final TreeSet<T> furthest = new TreeSet<>();
    14621490        for (T primitive : primitives) {
    1463             double distance = getDistance(osm, primitive);
    1464             if (Double.isNaN(distance)) continue;
    1465             int comp = Double.compare(distance, furthestDistance);
     1491            final double distance = getDistance(osm, primitive);
     1492            if (Double.isNaN(distance)) {
     1493                continue;
     1494            }
     1495            final int comp = Double.compare(distance, furthestDistance);
    14661496            if (comp > 0) {
    14671497                furthest.clear();
    … …  
    14901520        double rValue = Double.MAX_VALUE;
    14911521        if (one == null || two == null || one.isIncomplete()
    1492                 || two.isIncomplete()) return Double.NaN;
     1522                || two.isIncomplete()) {
     1523            return Double.NaN;
     1524        }
    14931525        if (one instanceof ILatLon && two instanceof ILatLon) {
    14941526            rValue = ((ILatLon) one).greatCircleDistance(((ILatLon) two));
    … …  
    15011533        } else if (one instanceof Relation && !(two instanceof Relation)) {
    15021534            for (OsmPrimitive osmPrimitive: ((Relation) one).getMemberPrimitives()) {
    1503                 double currentDistance = getDistance(osmPrimitive, two);
    1504                 if (currentDistance < rValue) rValue = currentDistance;
     1535                final double currentDistance = getDistance(osmPrimitive, two);
     1536                if (currentDistance < rValue) {
     1537                    rValue = currentDistance;
     1538                }
    15051539            }
    15061540        } else if (!(one instanceof Relation) && two instanceof Relation) {
    … …  
    15091543            for (OsmPrimitive osmPrimitive1 : ((Relation) one).getMemberPrimitives()) {
    15101544                for (OsmPrimitive osmPrimitive2 : ((Relation) two).getMemberPrimitives()) {
    1511                     double currentDistance = getDistance(osmPrimitive1, osmPrimitive2);
    1512                     if (currentDistance < rValue) rValue = currentDistance;
     1545                    final double currentDistance = getDistance(osmPrimitive1, osmPrimitive2);
     1546                    if (currentDistance < rValue) {
     1547                        rValue = currentDistance;
     1548                    }
    15131549                }
    15141550            }
    … …  
    15271563     */
    15281564    public static double getDistanceWayNode(Way way, Node node) {
    1529         if (way == null || node == null || way.getNodesCount() < 2 || !node.isLatLonKnown())
     1565        if (way == null || node == null || way.getNodesCount() < 2 || !node.isLatLonKnown()) {
    15301566            return Double.NaN;
     1567        }
    15311568
    15321569        double smallest = Double.MAX_VALUE;
    1533         EastNorth en0 = node.getEastNorth();
     1570        final EastNorth en0 = node.getEastNorth();
    15341571        // go through the nodes as if they were paired
    1535         Iterator<Node> iter = way.getNodes().iterator();
     1572        final Iterator<Node> iter = way.getNodes().iterator();
    15361573        EastNorth en1 = iter.next().getEastNorth();
    15371574        while (iter.hasNext()) {
    1538             EastNorth en2 = iter.next().getEastNorth();
    1539             double distance = getSegmentNodeDistSq(en1, en2, en0);
    1540             if (distance < smallest)
     1575            final EastNorth en2 = iter.next().getEastNorth();
     1576            final double distance = getSegmentNodeDistSq(en1, en2, en0);
     1577            if (distance < smallest) {
    15411578                smallest = distance;
     1579            }
    15421580            en1 = en2;
    15431581        }
    … …  
    15581596    public static WaySegment getClosestWaySegment(Way way, OsmPrimitive primitive) {
    15591597        if (way == null || primitive == null || way.isIncomplete()
    1560                 || primitive.isIncomplete()) return null;
     1598                || primitive.isIncomplete()) {
     1599            return null;
     1600        }
    15611601        double lowestDistance = Double.MAX_VALUE;
    15621602        Pair<Node, Node> closestNodes = null;
    15631603        for (Pair<Node, Node> nodes : way.getNodePairs(false)) {
    1564             Way tWay = new Way();
     1604            final Way tWay = new Way();
    15651605            tWay.addNode(new Node(nodes.a));
    15661606            tWay.addNode(new Node(nodes.b));
    1567             double distance = getDistance(tWay, primitive);
     1607            final double distance = getDistance(tWay, primitive);
    15681608            if (distance < lowestDistance) {
    15691609                lowestDistance = distance;
    … …  
    15711611            }
    15721612        }
    1573         if (closestNodes == null) return null;
     1613        if (closestNodes == null) {
     1614            return null;
     1615        }
    15741616        return lowestDistance != Double.MAX_VALUE ? WaySegment.forNodePair(way, closestNodes.a, closestNodes.b) : null;
    15751617    }
    … …  
    15861628     */
    15871629    public static double getDistanceWayWay(Way w1, Way w2) {
    1588         if (w1 == null || w2 == null || w1.getNodesCount() < 2 || w2.getNodesCount() < 2)
     1630        if (w1 == null || w2 == null || w1.getNodesCount() < 2 || w2.getNodesCount() < 2) {
    15891631            return Double.NaN;
     1632        }
    15901633        double rValue = Double.MAX_VALUE;
    1591         Iterator<Node> iter1 = w1.getNodes().iterator();
    1592         List<Node> w2Nodes = w2.getNodes();
     1634        final Iterator<Node> iter1 = w1.getNodes().iterator();
     1635        final List<Node> w2Nodes = w2.getNodes();
    15931636        Node w1N1 = iter1.next();
    15941637        while (iter1.hasNext()) {
    1595             Node w1N2 = iter1.next();
    1596             Iterator<Node> iter2 = w2Nodes.iterator();
     1638            final Node w1N2 = iter1.next();
     1639            final Iterator<Node> iter2 = w2Nodes.iterator();
    15971640            Node w2N1 = iter2.next();
    15981641            while (iter2.hasNext()) {
    1599                 Node w2N2 = iter2.next();
    1600                 double distance = getDistanceSegmentSegment(w1N1, w1N2, w2N1, w2N2);
    1601                 if (distance < rValue)
     1642                final Node w2N2 = iter2.next();
     1643                final double distance = getDistanceSegmentSegment(w1N1, w1N2, w2N1, w2N2);
     1644                if (distance < rValue) {
    16021645                    rValue = distance;
     1646                }
    16031647                w2N1 = w2N2;
    16041648            }
    … …  
    16361680            return Double.NaN;
    16371681        }
    1638         EastNorth enWs1Node1 = ws1Node1.getEastNorth();
    1639         EastNorth enWs1Node2 = ws1Node2.getEastNorth();
    1640         EastNorth enWs2Node1 = ws2Node1.getEastNorth();
    1641         EastNorth enWs2Node2 = ws2Node2.getEastNorth();
    1642         if (getSegmentSegmentIntersection(enWs1Node1, enWs1Node2, enWs2Node1, enWs2Node2) != null)
     1682        final EastNorth enWs1Node1 = ws1Node1.getEastNorth();
     1683        final EastNorth enWs1Node2 = ws1Node2.getEastNorth();
     1684        final EastNorth enWs2Node1 = ws2Node1.getEastNorth();
     1685        final EastNorth enWs2Node2 = ws2Node2.getEastNorth();
     1686        if (getSegmentSegmentIntersection(enWs1Node1, enWs1Node2, enWs2Node1, enWs2Node2) != null) {
    16431687            return 0;
    1644 
    1645         double dist1sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node1);
    1646         double dist2sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node2);
    1647         double dist3sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node1);
    1648         double dist4sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node2);
    1649         double smallest = Math.min(Math.min(dist1sq, dist2sq), Math.min(dist3sq, dist4sq));
     1688        }
     1689
     1690        final double dist1sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node1);
     1691        final double dist2sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node2);
     1692        final double dist3sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node1);
     1693        final double dist4sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node2);
     1694        final double smallest = Math.min(Math.min(dist1sq, dist2sq), Math.min(dist3sq, dist4sq));
    16501695        return smallest != Double.MAX_VALUE ? Math.sqrt(smallest) : Double.NaN;
    16511696    }
    … …  
    16821727     */
    16831728    private static double getSegmentNodeDistSq(EastNorth s1, EastNorth s2, EastNorth p) {
    1684         EastNorth c1 = closestPointTo(s1, s2, p, true);
     1729        final EastNorth c1 = closestPointTo(s1, s2, p, true);
    16851730        return c1.distanceSq(p);
    16861731    }
Note: See TracChangeset for help on using the changeset viewer.