You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@opennlp.apache.org by "mawiesne (via GitHub)" <gi...@apache.org> on 2023/02/19 12:56:36 UTC

[GitHub] [opennlp] mawiesne opened a new pull request, #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

mawiesne opened a new pull request, #505:
URL: https://github.com/apache/opennlp/pull/505

   Change
   -
   
   - adds missing equals and hashCode implementations
   - cures existing TODOS in related test cases
   
   Tasks
   -
   Thank you for contributing to Apache OpenNLP.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [x] Is there a JIRA ticket associated with this PR? Is it referenced 
        in the commit message?
   
   - [x] Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [x] Has your PR been rebased against the latest commit within the target branch (typically main)?
   
   - [x] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [x] Have you ensured that the full suite of tests is executed via mvn clean install at the root opennlp folder?
   - [x] Have you written or updated unit tests to verify your changes?
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
   - [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file in opennlp folder?
   - [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found in opennlp folder?
   
   ### For documentation related changes:
   - [x] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions for build issues and submit an update to your PR as soon as possible.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #505:
URL: https://github.com/apache/opennlp/pull/505#issuecomment-1436008087

   @kinow Thanks for your feedback & improvements. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on code in PR #505:
URL: https://github.com/apache/opennlp/pull/505#discussion_r1111246964


##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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;
+              }

Review Comment:
   We're are in a loop here. Only if one element is not equal, an early break makes sense. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne merged pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne merged PR #505:
URL: https://github.com/apache/opennlp/pull/505


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on code in PR #505:
URL: https://github.com/apache/opennlp/pull/505#discussion_r1111247187


##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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;
+          }
+        }
+      }

Review Comment:
   @kinow Will check/test it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on code in PR #505:
URL: https://github.com/apache/opennlp/pull/505#discussion_r1111248291


##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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;
+          }
+        }
+      }

Review Comment:
   Adapted and pushed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] kinow commented on a diff in pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "kinow (via GitHub)" <gi...@apache.org>.
kinow commented on code in PR #505:
URL: https://github.com/apache/opennlp/pull/505#discussion_r1111234745


##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           result = entrySet.contains(new StringListWrapper(new StringList(str)));
 
         }
-
         return result;
       }
+
+      @Override
+      public boolean equals(Object o) {
+        if (! (o instanceof Set)) {
+          return false;
+        } else {

Review Comment:
   We can drop the `else` I think? Just a
   
   ```
   if (! (o instanceof Set)) {
     return False;
   }
   
   // rest of the code
   ```



##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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();

Review Comment:
   I think it would be faster to iterate on the entrySet,
   
   ```
   for (StringListWrapper entry : entrySet) {
   ```
   
   Then I think we also won't need the `Iterator<StringListWrapper> entrySetIter = entrySet.iterator();` declaration.



##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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;
+          }
+        }
+      }

Review Comment:
   I think this one has the suggestions above, and doesn't need the `isEqual` variable.
   
   ```suggestion
   @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;
         }
   ```



##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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;
+              }

Review Comment:
   I think we can use `return` directly instead of `isEqual`?



##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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 {

Review Comment:
   Same here, `else` is optional`. Maybe having less "levels" makes the code easier to read?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #505: OPENNLP-1471 Ensure Dictionary#asStringSet() implements hashCode() and equals() correctly

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on code in PR #505:
URL: https://github.com/apache/opennlp/pull/505#discussion_r1111247645


##########
opennlp-tools/src/main/java/opennlp/tools/dictionary/Dictionary.java:
##########
@@ -343,9 +343,41 @@ public boolean contains(Object obj) {
           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;
+          }
+        }
+      }

Review Comment:
   LGTM



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org