Skip to content

Commit 03d6087

Browse files
hagbardMichael Bolin
authored andcommitted
Split S2Edge and UndirectedEdge up to make equals() method correct.
Before this CL it was possible to have instances of S2Edge such that: a.equals(b) ==/==> b.equals(a) b.equals(a) && b.equals(c) ==/==> a.equals(c) Undirected edges are just fundamentally a different type to an S2Edge and should be treated as such. Additionally they are only used privately in one place and never using polymorphic behaviour. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=25140523
1 parent c04b68b commit 03d6087

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/com/google/common/geometry/S2Edge.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author kirilll@google.com (Kirill Levin)
2323
*/
24-
public class S2Edge {
24+
public final class S2Edge {
2525

2626
private final S2Point start;
2727
private final S2Point end;
@@ -41,15 +41,15 @@ public S2Point getEnd() {
4141

4242
@Override
4343
public String toString() {
44-
return "Edge: (" + start.toDegreesString() + " -> " + end.toDegreesString() + ")\n" + " or ["
45-
+ start + " -> " + end + "]";
44+
return String.format("Edge: (%s -> %s)\n or [%s -> %s]",
45+
start.toDegreesString(), end.toDegreesString(), start, end);
4646
}
4747

4848
@Override
4949
public int hashCode() {
5050
return getStart().hashCode() - getEnd().hashCode();
5151
}
52-
52+
5353
@Override
5454
public boolean equals(Object o) {
5555
if (o == null || !(o instanceof S2Edge)) {

src/com/google/common/geometry/S2Polygon.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,10 +1084,32 @@ public String toString() {
10841084
return sb.toString();
10851085
}
10861086

1087-
private static class UndirectedEdge extends S2Edge {
1087+
private static final class UndirectedEdge {
1088+
// Note: An UndirectedEdge and an S2Edge can never be considered equal (in
1089+
// terms of the equals() method) and hence they re not be related types.
1090+
// If you need to convert between the types then separate conversion
1091+
// methods should be introduced.
10881092

1089-
public UndirectedEdge(S2Point vertexA, S2Point vertexB) {
1090-
super(vertexA, vertexB);
1093+
private final S2Point a;
1094+
private final S2Point b;
1095+
1096+
public UndirectedEdge(S2Point start, S2Point end) {
1097+
this.a = start;
1098+
this.b = end;
1099+
}
1100+
1101+
public S2Point getStart() {
1102+
return a;
1103+
}
1104+
1105+
public S2Point getEnd() {
1106+
return b;
1107+
}
1108+
1109+
@Override
1110+
public String toString() {
1111+
return String.format("Edge: (%s <-> %s)\n or [%s <-> %s]",
1112+
a.toDegreesString(), b.toDegreesString(), a, b);
10911113
}
10921114

10931115
@Override

0 commit comments

Comments
 (0)