From 03d6087312c92f1fbdafcae0c0a7596d270cee0a Mon Sep 17 00:00:00 2001 From: David Beaumont Date: Tue, 8 Nov 2011 11:05:58 -0500 Subject: 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 --- src/com/google/common/geometry/S2Edge.java | 8 ++++---- src/com/google/common/geometry/S2Polygon.java | 28 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/com/google/common/geometry/S2Edge.java b/src/com/google/common/geometry/S2Edge.java index 35cf5c2..eafd872 100644 --- a/src/com/google/common/geometry/S2Edge.java +++ b/src/com/google/common/geometry/S2Edge.java @@ -21,7 +21,7 @@ package com.google.common.geometry; * * @author kirilll@google.com (Kirill Levin) */ -public class S2Edge { +public final class S2Edge { private final S2Point start; private final S2Point end; @@ -41,15 +41,15 @@ public class S2Edge { @Override public String toString() { - return "Edge: (" + start.toDegreesString() + " -> " + end.toDegreesString() + ")\n" + " or [" - + start + " -> " + end + "]"; + return String.format("Edge: (%s -> %s)\n or [%s -> %s]", + start.toDegreesString(), end.toDegreesString(), start, end); } @Override public int hashCode() { return getStart().hashCode() - getEnd().hashCode(); } - + @Override public boolean equals(Object o) { if (o == null || !(o instanceof S2Edge)) { diff --git a/src/com/google/common/geometry/S2Polygon.java b/src/com/google/common/geometry/S2Polygon.java index e3b93f7..b90b6fd 100644 --- a/src/com/google/common/geometry/S2Polygon.java +++ b/src/com/google/common/geometry/S2Polygon.java @@ -1084,10 +1084,32 @@ public final strictfp class S2Polygon implements S2Region, Comparable return sb.toString(); } - private static class UndirectedEdge extends S2Edge { + private static final class UndirectedEdge { + // Note: An UndirectedEdge and an S2Edge can never be considered equal (in + // terms of the equals() method) and hence they re not be related types. + // If you need to convert between the types then separate conversion + // methods should be introduced. - public UndirectedEdge(S2Point vertexA, S2Point vertexB) { - super(vertexA, vertexB); + private final S2Point a; + private final S2Point b; + + public UndirectedEdge(S2Point start, S2Point end) { + this.a = start; + this.b = end; + } + + public S2Point getStart() { + return a; + } + + public S2Point getEnd() { + return b; + } + + @Override + public String toString() { + return String.format("Edge: (%s <-> %s)\n or [%s <-> %s]", + a.toDegreesString(), b.toDegreesString(), a, b); } @Override -- cgit v1.2.3