diff --git a/java/com/google/devtools/moe/client/parser/Operation.java b/java/com/google/devtools/moe/client/parser/Operation.java index 1f19e496..fc39f5a6 100644 --- a/java/com/google/devtools/moe/client/parser/Operation.java +++ b/java/com/google/devtools/moe/client/parser/Operation.java @@ -39,9 +39,11 @@ 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 + 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