New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TEXT-211] TextStringBuilder.equals whatever the capacity is #281
Conversation
Used subarrays for Arrays.equals to allow equals method to be relevant in case the 2 TextStringBuilders have not the same capacity. Updated the test case to reflect the change
Hi @sebx59
|
Corrected the way hashCode is calculated, based only on the relevant characters of the buffer, independantly of the capacity. Modified also the test case with relevant test
You're right, my bad, new commit for hashcode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @sebx59
See 3 comments please.
if (this.size != other.size) { | ||
return false; | ||
} | ||
return Arrays.equals(ArrayUtils.subarray(buffer, 0, size), ArrayUtils.subarray(other.buffer, 0, size)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can reuse our own toString() here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, done in new commit
@@ -1947,7 +1954,7 @@ public String getNullText() { | |||
*/ | |||
@Override | |||
public int hashCode() { | |||
return Arrays.hashCode(buffer); | |||
return Arrays.hashCode(ArrayUtils.subarray(buffer, 0, size)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can reuse our own toString() here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, done in new commit
if (this.size != other.size) { | ||
return false; | ||
} | ||
return this.toString().equals(other.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebx59 your change makes the code look very similar to the code prior to the change that added Arrays.equals
(which I also wouldn't have guessed to cause a regression here).
Maybe we could just revert it instead? 36af8b1
And also add a comment in the code so we don't accidentally add the regression again in the code. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done ! thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking better now @sebx59 ! Thanks for adding the comments. I will take a better look later and also confirm if we can revert hashCode
too (forgot to check that earlier today). We will need to squash the commits when merging it, but you can hold it until others have finished their review 👍 Great work!
This patch cannot be applied manually due to trailing whitespace, please fix:
|
should be OK now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @sebx59
Please see pending comments.
src/test/java/org/apache/commons/text/TextStringBuilderTest.java
Outdated
Show resolved
Hide resolved
new commit with requested changes |
In theory, the build should fail, but not all Commons components are set up properly with Checkstyle, SpotBugs, and PMD. |
Used subarrays for Arrays.equals to allow equals method to be relevant in case the 2 TextStringBuilders have not the same capacity.
Updated the test case to reflect the change