001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.osm; 003 004import java.util.Objects; 005 006import org.openstreetmap.josm.tools.Pair; 007 008/** 009 * A directed pair of nodes (a,b != b,a). 010 * @since 12463 (extracted from CombineWayAction) 011 */ 012public class NodePair { 013 private final Node a; 014 private final Node b; 015 016 /** 017 * Constructs a new {@code NodePair}. 018 * @param a The first node 019 * @param b The second node 020 */ 021 public NodePair(Node a, Node b) { 022 this.a = a; 023 this.b = b; 024 } 025 026 /** 027 * Constructs a new {@code NodePair}. 028 * @param pair An existing {@code Pair} of nodes 029 */ 030 public NodePair(Pair<Node, Node> pair) { 031 this(pair.a, pair.b); 032 } 033 034 /** 035 * Replies the first node. 036 * @return The first node 037 */ 038 public Node getA() { 039 return a; 040 } 041 042 /** 043 * Replies the second node 044 * @return The second node 045 */ 046 public Node getB() { 047 return b; 048 } 049 050 /** 051 * Determines if this pair is successor of another one (other.b == this.a) 052 * @param other other pair 053 * @return {@code true} if other.b == this.a 054 */ 055 public boolean isSuccessorOf(NodePair other) { 056 return other.getB() == a; 057 } 058 059 /** 060 * Determines if this pair is predecessor of another one (this.b == other.a) 061 * @param other other pair 062 * @return {@code true} if this.b == other.a 063 */ 064 public boolean isPredecessorOf(NodePair other) { 065 return b == other.getA(); 066 } 067 068 /** 069 * Returns the inversed pair. 070 * @return swapped copy 071 */ 072 public NodePair swap() { 073 return new NodePair(b, a); 074 } 075 076 @Override 077 public String toString() { 078 return "[" + 079 a.getId() + 080 ',' + 081 b.getId() + 082 ']'; 083 } 084 085 /** 086 * Determines if this pair contains the given node. 087 * @param n The node to look for 088 * @return {@code true} if {@code n} is in the pair, {@code false} otherwise 089 */ 090 public boolean contains(Node n) { 091 return a == n || b == n; 092 } 093 094 @Override 095 public int hashCode() { 096 return Objects.hash(a, b); 097 } 098 099 @Override 100 public boolean equals(Object obj) { 101 if (this == obj) return true; 102 if (obj == null || getClass() != obj.getClass()) return false; 103 NodePair nodePair = (NodePair) obj; 104 return Objects.equals(a, nodePair.a) && 105 Objects.equals(b, nodePair.b); 106 } 107}