Skip to content
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

Merged
merged 9 commits into from Mar 31, 2022

Conversation

sebx59
Copy link
Contributor

@sebx59 sebx59 commented Nov 3, 2021

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

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
@garydgregory
Copy link
Member

Hi @sebx59

  • What about also testing hashCode()?
  • The formatting looks messed up

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
@sebx59
Copy link
Contributor Author

sebx59 commented Nov 3, 2021

You're right, my bad, new commit for hashcode
What do you mean about formatting ?

Copy link
Member

@garydgregory garydgregory left a 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));
Copy link
Member

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.

Copy link
Contributor Author

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));
Copy link
Member

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.

Copy link
Contributor Author

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());
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ! thanks

Copy link
Member

@kinow kinow left a 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!

@garydgregory
Copy link
Member

This patch cannot be applied manually due to trailing whitespace, please fix:

git apply \temp\281.diff
/temp/281.diff:63: trailing whitespace.

/temp/281.diff:64: trailing whitespace.
        // following TEXT-211 : the hashcode of the buffer may not be equals to the hashcode of the TextStringBuilder itself
/temp/281.diff:73: trailing whitespace.

/temp/281.diff:84: trailing whitespace.
        assertEquals(hc2b2, hc3b2);
warning: 4 lines add whitespace errors.

@sebx59
Copy link
Contributor Author

sebx59 commented Mar 31, 2022

should be OK now

Copy link
Member

@garydgregory garydgregory left a 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.

@sebx59
Copy link
Contributor Author

sebx59 commented Mar 31, 2022

new commit with requested changes
Is there a place I can see those problems before submitting th PR ?

@garydgregory
Copy link
Member

In theory, the build should fail, but not all Commons components are set up properly with Checkstyle, SpotBugs, and PMD.

@garydgregory garydgregory changed the title TEXT-211 - TextStringBuilder.equals whatever the capacity is [TEXT-211] TextStringBuilder.equals whatever the capacity is Mar 31, 2022
@garydgregory garydgregory merged commit 400fb65 into apache:master Mar 31, 2022
@garydgregory
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants