Changeset 19065 in josm for trunk/src


Ignore:
Timestamp:
2024-04-28T09:37:20+02:00 (10 months ago)
Author:
GerdP
Message:

see #21881 Add a check for loops in directional waterways
Fix code in WaySegment and IWaySegment

  • change text in Exception to "Node pair is not a single segment of the way!" and move it to constant IWaySegment.NOT_A_SEGMENT
  • fix error in WaySegment.forNodePair and IWaySegment.forNodePair
  • add unit test for further invalid node pairs
Location:
trunk/src/org/openstreetmap/josm/data/osm
Files:
2 edited

Legend:

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

    r17986 r19065  
    1818 */
    1919public class IWaySegment<N extends INode, W extends IWay<N>> implements Comparable<IWaySegment<N, W>> {
     20    protected static final String NOT_A_SEGMENT = "Node pair is not a single segment of the way!";
    2021
    2122    private final W way;
     
    9798     * @param second second node
    9899     * @return way segment
    99      * @throws IllegalArgumentException if the node pair is not part of way
     100     * @throws IllegalArgumentException if the node pair is not a single segment of the way
    100101     */
    101102    public static <N extends INode, W extends IWay<N>> IWaySegment<N, W> forNodePair(W way, N first, N second) {
     
    103104        while (endIndex > 0) {
    104105            final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
     106            if (indexOfFirst < 0)
     107                break;
    105108            if (second.equals(way.getNode(indexOfFirst + 1))) {
    106109                return new IWaySegment<>(way, indexOfFirst);
     
    108111            endIndex--;
    109112        }
    110         throw new IllegalArgumentException("Node pair is not part of way!");
     113        throw new IllegalArgumentException(NOT_A_SEGMENT);
    111114    }
    112115
     
    162165    @Override
    163166    public int compareTo(IWaySegment o) {
     167        if (o == null)
     168            return -1;
    164169        final W thisWay;
    165170        final IWay<?> otherWay;
    166171        try {
    167172            thisWay = toWay();
    168             otherWay = o == null ? null : o.toWay();
     173            otherWay = o.toWay();
    169174        } catch (ReflectiveOperationException e) {
    170175            Logging.error(e);
    171176            return -1;
    172177        }
    173         return o == null ? -1 : (equals(o) ? 0 : thisWay.compareTo(otherWay));
     178        return equals(o) ? 0 : thisWay.compareTo(otherWay);
    174179    }
    175180
  • trunk/src/org/openstreetmap/josm/data/osm/WaySegment.java

    r19062 r19065  
    2626     * @param second second node
    2727     * @return way segment
    28      * @throws IllegalArgumentException if the node pair is not part of way
     28     * @throws IllegalArgumentException if the node pair is not single a segment of the way
    2929     */
    3030    public static WaySegment forNodePair(Way way, Node first, Node second) {
     
    3232        while (endIndex > 0) {
    3333            final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
     34            if (indexOfFirst < 0)
     35                break;
    3436            if (second.equals(way.getNode(indexOfFirst + 1))) {
    3537                return new WaySegment(way, indexOfFirst);
     
    3739            endIndex--;
    3840        }
    39         throw new IllegalArgumentException("The node pair is not consecutive part of the way!");
     41        throw new IllegalArgumentException(IWaySegment.NOT_A_SEGMENT);
    4042    }
    4143
Note: See TracChangeset for help on using the changeset viewer.