-
Notifications
You must be signed in to change notification settings - Fork 170
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
#881 equals and hashCode for CollectionEnvelope #894
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,19 +27,19 @@ | |
import java.util.Iterator; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.iterator.Immutable; | ||
import org.cactoos.scalar.And; | ||
import org.cactoos.scalar.Folded; | ||
import org.cactoos.scalar.InheritanceLevel; | ||
import org.cactoos.scalar.Or; | ||
import org.cactoos.scalar.SumOfIntScalar; | ||
import org.cactoos.scalar.UncheckedScalar; | ||
|
||
/** | ||
* Base collection. | ||
* | ||
* <p>There is no thread-safety guarantee.</p> | ||
* | ||
* @param <X> Element type | ||
* @since 0.23 | ||
* @todo #844:30min Implement methods equals and hashCode for this class. | ||
* Implementation should rely on the items of the nested collection, but not | ||
* on default JVM impl. Class {@link org.cactoos.map.MapEnvelope} can be used | ||
* as an example. | ||
* @checkstyle AbstractClassNameCheck (500 lines) | ||
*/ | ||
@SuppressWarnings( | ||
|
@@ -147,4 +147,48 @@ public String toString() { | |
return this.col.value().toString(); | ||
} | ||
|
||
@Override | ||
public final boolean equals(final Object other) { | ||
return new UncheckedScalar<>( | ||
new And( | ||
new Or( | ||
() -> new InheritanceLevel( | ||
other.getClass(), Collection.class | ||
).value() > -1, | ||
() -> new InheritanceLevel( | ||
other.getClass(), CollectionEnvelope.class | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Vatavuk I believe this isn't necessary because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @llorllale There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @llorllale the end result is that we will only compare classes that are related to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Vatavuk We should be comparing I think I understand the problem now: We're missing tests that compare Please leave a puzzle for this and we'll merge. |
||
).value() > -1 | ||
), | ||
() -> { | ||
final Collection<?> compared = (Collection<?>) other; | ||
return this.size() == compared.size(); | ||
}, | ||
() -> { | ||
final Iterable<?> compared = (Iterable<?>) other; | ||
final Iterator<?> iterator = compared.iterator(); | ||
return new UncheckedScalar<>( | ||
new And( | ||
(X input) -> input.equals(iterator.next()), | ||
this | ||
) | ||
).value(); | ||
} | ||
) | ||
).value(); | ||
} | ||
|
||
// @checkstyle MagicNumberCheck (30 lines) | ||
@Override | ||
public final int hashCode() { | ||
return new UncheckedScalar<>( | ||
new Folded<>( | ||
42, | ||
(hash, entry) -> new SumOfIntScalar( | ||
() -> 37 * hash, | ||
entry::hashCode | ||
).value(), | ||
this | ||
) | ||
).value(); | ||
} | ||
} |
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.
@Vatavuk this
equals
is missing the null-checkThere 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.
@llorllale fixed in d4538f8