001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004/**
005 * A segment consisting of 2 consecutive nodes out of a way.
006 */
007public final class WaySegment implements Comparable<WaySegment> {
008    /**
009     * The way.
010     */
011    public Way way;
012
013    /**
014     * The index of one of the 2 nodes in the way.  The other node has the
015     * index <code>lowerIndex + 1</code>.
016     */
017    public int lowerIndex;
018
019    public WaySegment(Way w, int i) {
020        way = w;
021        lowerIndex = i;
022    }
023
024    /**
025     * Returns the first node of the way segment.
026     * @return the first node
027     */
028    public Node getFirstNode() {
029        return way.getNode(lowerIndex);
030    }
031
032    /**
033     * Returns the second (last) node of the way segment.
034     * @return the second node
035     */
036    public Node getSecondNode(){
037        return way.getNode(lowerIndex + 1);
038    }
039
040    /**
041     * Returns this way segment as complete way.
042     * @return the way segment as {@code Way}
043     */
044    public Way toWay() {
045        Way w = new Way();
046        w.addNode(getFirstNode());
047        w.addNode(getSecondNode());
048        return w;
049    }
050
051    @Override public boolean equals(Object o) {
052        return o instanceof WaySegment
053            && ((WaySegment) o).way == way
054            && ((WaySegment) o).lowerIndex == lowerIndex;
055    }
056
057    @Override public int hashCode() {
058        return way.hashCode() ^ lowerIndex;
059    }
060
061    @Override
062    public int compareTo(WaySegment o) {
063        return equals(o) ? 0 : toWay().compareTo(o.toWay());
064    }
065}