-
Notifications
You must be signed in to change notification settings - Fork 171
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
TextEnvelope equals is not symmetric #1156
Comments
@llorllale/z please, pay attention to this issue |
@g4s8/z this project will fix the problem faster if you donate a few dollars to it; just click here and pay via Stripe, it's very fast, convenient and appreciated; thanks a lot! |
@g4s8 you rolled your own implementation of |
@llorllale that's true, but I think it's a valid case, since anybody can implement class MyText implements Text {
...
@Override
public void equals(Object other) {
if (!(other instanceof MyText)) {
return false;
}
// other checks
}
} So I'd suggest adding type check in |
@g4s8 |
@llorllale but it checks that other object is a new And(
- () -> obj instanceof Text,
+ () -> obj instanceof TextEnvelope,
() -> new UncheckedText(this) |
@g4s8 oh, I think I see what you mean now. My question is: would this change really be useful? Is it causing an actual bug somewhere? Or is this suggestion entirely motivated from a purist point of view? |
@llorllale it's motivated from library user point of view: I may not check source code of new SomeText().equals(new FormattedText("...")) will return same result as new FormattedText("...").equals(new SomeText()) |
@g4s8 @llorllale aren't you in the middle of a discussion trying to solve all that is bad with using inheritance? :) Envelopes are not meant to implement behaviour but delegate to a wrapped object. I personally think that Or introduce an See llorllale/cactoos-matchers#135 for a similar discussion. |
@victornoel nope - @g4s8 is pointing to a deeper problem that would apply to your solution as well. |
@llorllale I understand that, but I believe we would be better served by having a real |
Two |
@llorllale but what if I have a class FileContent implements Text {
private Path file;
@Override
public String asString() {
return new String(Files.readAllBytes(this.file));
}
} in that case I'd say that two instances are equal if they have the same state ( public boolean equals(Object obj) {
return Objects.equals(this.file, FileContent.class.cast(obj).file);
} |
@g4s8 raises a good point, and is highlighting another problem of envelopes we haven't tackled yet: when using envelopes for some of your implementations, you can't rely on reflection and casting. Because if you have But they won't, because I don't think there is a solution for that, or do you have something in mind (with current or my proposed design, cf llorllale/cactoos-matchers#135 (comment), whatever) @g4s8 ? |
@victornoel @llorllale if I really need to compare two texts by final class EqText implements Text {
private final Text txt;
@Override
public String asString() {
return this.txt.asString();
}
@Override
public boolean equals(Object other) {
return (other instanceof EqText)
&& Objects.equals(this.asString(), EqText.class.cast(other).asString());
} |
@g4s8 @paulodamaso @victornoel |
@g4s8 I'm going to close this, there is some discussion on that matter in #1241 but concerning |
Job |
cactoos/src/main/java/org/cactoos/text/TextEnvelope.java
Lines 79 to 90 in 100885a
From Java
Object
docs:But
TextEnvelope
equals()
implementation is not symmetric:The text was updated successfully, but these errors were encountered: