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

[BEAM-389] Provide equals and hashCode in DelegateCoder #559

Closed
wants to merge 5 commits into from

Conversation

peihe
Copy link
Contributor

@peihe peihe commented Jun 29, 2016

No description provided.

@peihe peihe force-pushed the fix-delegate-coder-equals branch from c46f3bb to b2211c0 Compare June 29, 2016 21:24
@peihe
Copy link
Contributor Author

peihe commented Jun 29, 2016

R: @kennknowles
CC: @dhalperi

return Objects.hashCode(this.coder, this.toFn, this.fromFn);
}

@Override
public String toString() {
return "DelegateCoder(" + coder + ")";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should toFn/fromFn be used 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.

added

@peihe peihe force-pushed the fix-delegate-coder-equals branch from ad51960 to 677d821 Compare June 29, 2016 22:51

@Override
public int hashCode() {
return Objects.hashCode(this.coder, this.toFn.getClass(), this.fromFn.getClass());
Copy link
Member

Choose a reason for hiding this comment

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

Just use toFn and fromFn. Their class may not be sufficient.

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

@peihe peihe force-pushed the fix-delegate-coder-equals branch from cb214e2 to 1232506 Compare June 30, 2016 19:52
@peihe
Copy link
Contributor Author

peihe commented Jun 30, 2016

I also have to provide equals and hashCode in StringDelegateCoder and tests.
PTAL

@@ -101,8 +104,28 @@ public Object structuralValue(T value) throws Exception {
}

@Override
public boolean equals(Object o) {
if (o == null || this.getClass() != o.getClass()) {
Copy link
Member

Choose a reason for hiding this comment

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

instanceof automatically removes the need for the null check
if (!(o instanceof DelegateCoder)) {
return false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use instanceof will allow DelegateCoder equal to its subclass, which would break the symmetric property when its subclass override equals().

http://www.artima.com/lejava/articles/equality.html

Copy link
Member

Choose a reason for hiding this comment

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

Cool article.

I suggest that DelegateCoder is not intended for subclassing, so we could make it final. Then I think what Pei has is the same as what Luke suggests here. I think that we should actually move more towards the "different types cannot be equal" perspective throughout the SDK, unless those two types are both defined to be just implementations of a common interface, and then the interface should own the definition of equality.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 @kennknowles. Relatedly, I think most classes should either be abstract or final.

The only extender of this class is StringDelegateCoder, and it's only used in Tfidf (and clones of Tfidf). We could replace it with a proper implementation using DelegateCoder instead, or StringDelegateCoder could wrap it instead.

@peihe peihe force-pushed the fix-delegate-coder-equals branch 2 times, most recently from 4c4022a to 634f4e8 Compare July 7, 2016 21:57
@peihe
Copy link
Contributor Author

peihe commented Jul 7, 2016

PTAL
made DelegateCoder final

@@ -26,6 +27,8 @@
import java.util.Collection;
import java.util.List;

import autovalue.shaded.com.google.common.common.base.MoreObjects;
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong import

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@peihe peihe force-pushed the fix-delegate-coder-equals branch from 634f4e8 to 7700250 Compare July 8, 2016 21:58
@asfgit asfgit closed this in 9d70025 Jul 9, 2016
@peihe peihe deleted the fix-delegate-coder-equals branch July 11, 2016 20:11
Abacn pushed a commit to Abacn/beam that referenced this pull request Jan 31, 2023
johnjcasey pushed a commit to johnjcasey/beam that referenced this pull request Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants