1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Objects;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.tools.Pair;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * A directed pair of nodes (a,b != b,a).
|
---|
10 | * @since 12463 (extracted from CombineWayAction)
|
---|
11 | */
|
---|
12 | public class NodePair {
|
---|
13 | private final Node a;
|
---|
14 | private final Node b;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Constructs a new {@code NodePair}.
|
---|
18 | * @param a The first node
|
---|
19 | * @param b The second node
|
---|
20 | */
|
---|
21 | public NodePair(Node a, Node b) {
|
---|
22 | this.a = a;
|
---|
23 | this.b = b;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code NodePair}.
|
---|
28 | * @param pair An existing {@code Pair} of nodes
|
---|
29 | */
|
---|
30 | public NodePair(Pair<Node, Node> pair) {
|
---|
31 | this(pair.a, pair.b);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Replies the first node.
|
---|
36 | * @return The first node
|
---|
37 | */
|
---|
38 | public Node getA() {
|
---|
39 | return a;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Replies the second node
|
---|
44 | * @return The second node
|
---|
45 | */
|
---|
46 | public Node getB() {
|
---|
47 | return b;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Determines if this pair is successor of another one (other.b == this.a)
|
---|
52 | * @param other other pair
|
---|
53 | * @return {@code true} if other.b == this.a
|
---|
54 | */
|
---|
55 | public boolean isSuccessorOf(NodePair other) {
|
---|
56 | return other.getB() == a;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Determines if this pair is predecessor of another one (this.b == other.a)
|
---|
61 | * @param other other pair
|
---|
62 | * @return {@code true} if this.b == other.a
|
---|
63 | */
|
---|
64 | public boolean isPredecessorOf(NodePair other) {
|
---|
65 | return b == other.getA();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Returns the inversed pair.
|
---|
70 | * @return swapped copy
|
---|
71 | */
|
---|
72 | public NodePair swap() {
|
---|
73 | return new NodePair(b, a);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public String toString() {
|
---|
78 | return new StringBuilder()
|
---|
79 | .append('[')
|
---|
80 | .append(a.getId())
|
---|
81 | .append(',')
|
---|
82 | .append(b.getId())
|
---|
83 | .append(']')
|
---|
84 | .toString();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Determines if this pair contains the given node.
|
---|
89 | * @param n The node to look for
|
---|
90 | * @return {@code true} if {@code n} is in the pair, {@code false} otherwise
|
---|
91 | */
|
---|
92 | public boolean contains(Node n) {
|
---|
93 | return a == n || b == n;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public int hashCode() {
|
---|
98 | return Objects.hash(a, b);
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public boolean equals(Object obj) {
|
---|
103 | if (this == obj) return true;
|
---|
104 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
105 | NodePair nodePair = (NodePair) obj;
|
---|
106 | return Objects.equals(a, nodePair.a) &&
|
---|
107 | Objects.equals(b, nodePair.b);
|
---|
108 | }
|
---|
109 | }
|
---|