From 4f5024ff8213398d4a6d23b4ef4dd51e9ac0500b Mon Sep 17 00:00:00 2001 From: AjitSingh Date: Sun, 21 Feb 2016 09:59:42 +0530 Subject: [PATCH 1/2] implementing hashCode() as well for Operation and added test --- .../devtools/moe/client/parser/Operation.java | 5 +++++ .../moe/client/parser/OperationTest.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 javatests/com/google/devtools/moe/client/parser/OperationTest.java diff --git a/java/com/google/devtools/moe/client/parser/Operation.java b/java/com/google/devtools/moe/client/parser/Operation.java index 1f19e496..9dc82645 100644 --- a/java/com/google/devtools/moe/client/parser/Operation.java +++ b/java/com/google/devtools/moe/client/parser/Operation.java @@ -44,4 +44,9 @@ public boolean equals(Object o) { } return toString().equals(o.toString()); } + + @Override + public int hashCode() { + return toString().hashCode(); + } } diff --git a/javatests/com/google/devtools/moe/client/parser/OperationTest.java b/javatests/com/google/devtools/moe/client/parser/OperationTest.java new file mode 100644 index 00000000..abf149be --- /dev/null +++ b/javatests/com/google/devtools/moe/client/parser/OperationTest.java @@ -0,0 +1,22 @@ +package com.google.devtools.moe.client.parser; + +import junit.framework.TestCase; + +import java.util.HashMap; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class OperationTest extends TestCase { + + public void testHashCode() throws Exception { + HashMap map1 = new HashMap<>(); + map1.put("key", "value"); + + Operation operation1 = new Operation(Operator.EDIT, new Term("term1", map1)); + Operation operation2 = new Operation(Operator.EDIT, new Term("term1", map1)); + + assertThat(operation1.hashCode(), is(operation2.hashCode())); + assertTrue(operation1.equals(operation2)); + } +} \ No newline at end of file From fe18730ff645256ed3b1c7620e90952e4554b9be Mon Sep 17 00:00:00 2001 From: AjitSingh Date: Sun, 21 Feb 2016 10:09:38 +0530 Subject: [PATCH 2/2] simplifying Operation's equals method --- java/com/google/devtools/moe/client/parser/Operation.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/java/com/google/devtools/moe/client/parser/Operation.java b/java/com/google/devtools/moe/client/parser/Operation.java index 9dc82645..fc39f5a6 100644 --- a/java/com/google/devtools/moe/client/parser/Operation.java +++ b/java/com/google/devtools/moe/client/parser/Operation.java @@ -39,10 +39,7 @@ public String toString() { @Override public boolean equals(Object o) { - if (!(o instanceof Operation)) { - return false; - } - return toString().equals(o.toString()); + return o instanceof Operation && toString().equals(o.toString()); } @Override