From effb4062e34576a011e788a0d33c1433a677f940 Mon Sep 17 00:00:00 2001 From: Joel Martinez Date: Thu, 8 Sep 2016 14:35:38 -0700 Subject: [PATCH] Add an equals method to goog.math.Coordinate and make the existing equals method on goog.math.Vec2 take '*' instead of another goog.math.Vec2. RELNOTES: NONE ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132601819 --- closure/goog/math/coordinate.js | 11 +++++++++++ closure/goog/math/coordinate_test.js | 12 ++++++++++++ closure/goog/math/vec2.js | 12 +++++------- closure/goog/math/vec2_test.js | 1 + 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/closure/goog/math/coordinate.js b/closure/goog/math/coordinate.js index a08b9cb841..0dc2469457 100644 --- a/closure/goog/math/coordinate.js +++ b/closure/goog/math/coordinate.js @@ -66,6 +66,17 @@ if (goog.DEBUG) { } +/** + * Returns whether the specified value is equal to this coordinate. + * @param {*} other Some other value. + * @return {boolean} Whether the specified value is equal to this coordinate. + */ +goog.math.Coordinate.prototype.equals = function(other) { + return other instanceof goog.math.Coordinate && + goog.math.Coordinate.equals(this, other); +}; + + /** * Compares coordinates for equality. * @param {goog.math.Coordinate} a A Coordinate. diff --git a/closure/goog/math/coordinate_test.js b/closure/goog/math/coordinate_test.js index fdd04d6645..e71537b689 100644 --- a/closure/goog/math/coordinate_test.js +++ b/closure/goog/math/coordinate_test.js @@ -83,6 +83,18 @@ function testCoordinateClone() { assertEquals(c.toString(), c.clone().toString()); } +function testCoordinateEquals() { + var a = new goog.math.Coordinate(1, 2); + + assertFalse(a.equals(null)); + assertFalse(a.equals({})); + assertFalse(a.equals(new goog.math.Coordinate(1, 3))); + assertFalse(a.equals(new goog.math.Coordinate(2, 2))); + + assertTrue(a.equals(a)); + assertTrue(a.equals(new goog.math.Coordinate(1, 2))); +} + function testCoordinateDifference() { assertObjectEquals( new goog.math.Coordinate(3, -40), diff --git a/closure/goog/math/vec2.js b/closure/goog/math/vec2.js index 2d0784decc..a1e7b4ffe1 100644 --- a/closure/goog/math/vec2.js +++ b/closure/goog/math/vec2.js @@ -199,14 +199,12 @@ goog.math.Vec2.rotateAroundPoint = function(v, axisPoint, angle) { }; -/** - * Compares this vector with another for equality. - * @param {!goog.math.Vec2} b The other vector. - * @return {boolean} Whether this vector has the same x and y as the given - * vector. - */ +/** @override */ goog.math.Vec2.prototype.equals = function(b) { - return this == b || !!b && this.x == b.x && this.y == b.y; + if (this == b) { + return true; + } + return b instanceof goog.math.Vec2 && !!b && this.x == b.x && this.y == b.y; }; diff --git a/closure/goog/math/vec2_test.js b/closure/goog/math/vec2_test.js index f8418cdf31..63ed34efc7 100644 --- a/closure/goog/math/vec2_test.js +++ b/closure/goog/math/vec2_test.js @@ -141,6 +141,7 @@ function testEquals() { var a = new goog.math.Vec2(1, 2); assertFalse(a.equals(null)); + assertFalse(a.equals({})); assertFalse(a.equals(new goog.math.Vec2(1, 3))); assertFalse(a.equals(new goog.math.Vec2(2, 2)));