This is an automated email from the ASF dual-hosted git repository.
mawiesne pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git
The following commit(s) were added to refs/heads/main by this push:
new 7d91ff2e OPENNLP-1471 Ensure Dictionary#asStringSet() implements
hashCode() and equals() correctly (#505)
7d91ff2e is described below
commit 7d91ff2e0a617f797a10a5015d49117309a6ed9f
Author: Martin Wiesner <[email protected]>
AuthorDate: Thu Feb 23 17:46:27 2023 +0100
OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and
equals() correctly (#505)
- adds missing equals and hashCode implementations
- cures existing TODOS in related test cases
- addresses PR reviewer's feedback
---
.../java/opennlp/tools/dictionary/Dictionary.java | 30 +++++++++++++++++++++-
.../DictionaryAsSetCaseInsensitiveTest.java | 19 +++++++-------
.../DictionaryAsSetCaseSensitiveTest.java | 7 +++--
3 files changed, 42 insertions(+), 14 deletions(-)
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java
b/opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java
index d47c821e..3a1df1f5 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java
@@ -343,9 +343,37 @@ public class Dictionary implements Iterable<StringList>,
SerializableArtifact {
result = entrySet.contains(new StringListWrapper(new
StringList(str)));
}
-
return result;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (! (o instanceof Set)) {
+ return false;
+ }
+ Set<String> toCheck = (Set<String>) o;
+ if (entrySet.size() != toCheck.size()) {
+ return false;
+ }
+ Iterator<String> toCheckIter = toCheck.iterator();
+ for (StringListWrapper entry : entrySet) {
+ if (isCaseSensitive) {
+ if (!entry.stringList.equals(new StringList(toCheckIter.next()))) {
+ return false;
+ }
+ } else {
+ if (!entry.stringList.compareToIgnoreCase(new
StringList(toCheckIter.next()))) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return entrySet.hashCode();
+ }
};
}
diff --git
a/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseInsensitiveTest.java
b/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseInsensitiveTest.java
index d31b1557..4511b7a2 100644
---
a/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseInsensitiveTest.java
+++
b/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseInsensitiveTest.java
@@ -143,7 +143,7 @@ public class DictionaryAsSetCaseInsensitiveTest {
}
/**
- * Tests the {@link Dictionary#hashCode()} method.
+ * Tests the {@link Dictionary#asStringSet()#hashCode()} method without case
changes.
*/
@Test
void testHashCode() {
@@ -152,18 +152,19 @@ public class DictionaryAsSetCaseInsensitiveTest {
Dictionary dictA = getDict();
dictA.put(asSL(entry1));
- Set<String> setA = dictA.asStringSet();
-
Dictionary dictB = getDict();
dictB.put(asSL(entry1));
+ Assertions.assertEquals(dictA.hashCode(), dictB.hashCode());
+
+ Set<String> setA = dictA.asStringSet();
Set<String> setB = dictB.asStringSet();
Assertions.assertEquals(setA.hashCode(), setB.hashCode());
}
/**
- * Tests the {@link Dictionary#hashCode()} method.
+ * Tests the {@link Dictionary#asStringSet()#hashCode()}} method with case
changes.
*/
@Test
void testHashCodeDifferentCase() {
@@ -172,15 +173,15 @@ public class DictionaryAsSetCaseInsensitiveTest {
Dictionary dictA = getDict();
dictA.put(asSL(entry1));
- Set<String> setA = dictA.asStringSet();
-
Dictionary dictB = getDict();
- dictB.put(asSL(entry1.toUpperCase()));
+ dictB.put(asSL(entry1.toUpperCase())); // adjusting entry to differ
case-wise.
+
+ Assertions.assertEquals(dictA.hashCode(), dictB.hashCode());
+ Set<String> setA = dictA.asStringSet();
Set<String> setB = dictB.asStringSet();
- // TODO: should it be equal??
- Assertions.assertNotSame(setA.hashCode(), setB.hashCode());
+ Assertions.assertEquals(setA.hashCode(), setB.hashCode());
}
/**
diff --git
a/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseSensitiveTest.java
b/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseSensitiveTest.java
index 314096d7..21f3aef3 100644
---
a/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseSensitiveTest.java
+++
b/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryAsSetCaseSensitiveTest.java
@@ -144,7 +144,7 @@ public class DictionaryAsSetCaseSensitiveTest {
}
/**
- * Tests the {@link Dictionary#hashCode()} method.
+ * Tests the {@link Dictionary#asStringSet()#hashCode()} method.
*/
@Test
void testHashCode() {
@@ -164,7 +164,7 @@ public class DictionaryAsSetCaseSensitiveTest {
}
/**
- * Tests the {@link Dictionary#hashCode()} method.
+ * Tests the {@link Dictionary#asStringSet()#hashCode()} method.
*/
@Test
void testHashCodeDifferentCase() {
@@ -180,8 +180,7 @@ public class DictionaryAsSetCaseSensitiveTest {
Set<String> setB = dictB.asStringSet();
- // TODO: should it be equal??
- Assertions.assertNotSame(setA.hashCode(), setB.hashCode());
+ Assertions.assertEquals(setA.hashCode(), setB.hashCode());
}
/**