This is an automated email from the ASF dual-hosted git repository.

mawiesne pushed a commit to branch 
OPENNLP-1471_Ensure_Dictionary#asStringSet()_implements_hashCode()_and_equals()_correctly
in repository https://gitbox.apache.org/repos/asf/opennlp.git

commit 3263a53e0df740211ef31dc65e0f644d4ac29634
Author: Martin Wiesner <[email protected]>
AuthorDate: Sun Feb 19 13:55:52 2023 +0100

    OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and 
equals() correctly
    
    - adds missing equals and hashCode implementations
    - cures existing TODOS in related test cases
---
 .../java/opennlp/tools/dictionary/Dictionary.java  | 34 +++++++++++++++++++++-
 .../DictionaryAsSetCaseInsensitiveTest.java        | 19 ++++++------
 .../DictionaryAsSetCaseSensitiveTest.java          |  7 ++---
 3 files changed, 46 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..2abc0036 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,41 @@ 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;
+        } else {
+          Set<String> toCheck = (Set<String>) o;
+          if (entrySet.size() != toCheck.size()) {
+            return false;
+          } else {
+            boolean isEqual = true; // will flip if one element is not equal
+            Iterator<StringListWrapper> entrySetIter = entrySet.iterator();
+            Iterator<String> toCheckIter = toCheck.iterator();
+            for (int i = 0; i < entrySet.size(); i++) {
+              StringListWrapper entry = entrySetIter.next();
+              if (isCaseSensitive) {
+                isEqual = entry.stringList.equals(new 
StringList(toCheckIter.next()));
+              } else {
+                isEqual = entry.stringList.compareToIgnoreCase(new 
StringList(toCheckIter.next()));
+              }
+              if (!isEqual) {
+                break;
+              }
+            }
+            return isEqual;
+          }
+        }
+      }
+
+      @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 f6413530..cc63d429 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 0515da8a..4c141b38 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());
   }
 
   /**

Reply via email to