Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

implementing hashCode() as well for Operation and added test #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions java/com/google/devtools/moe/client/parser/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
22 changes: 22 additions & 0 deletions javatests/com/google/devtools/moe/client/parser/OperationTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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));
}
}