Skip to content

Commit

Permalink
(#922) Refactor hashCode and equals
Browse files Browse the repository at this point in the history
  • Loading branch information
vladhss committed Apr 8, 2019
1 parent 2a79730 commit ac845f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
26 changes: 9 additions & 17 deletions src/main/java/org/cactoos/map/MapEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
@SuppressWarnings(
{
"PMD.TooManyMethods",
"PMD.AbstractNaming",
"unchecked"
"PMD.AbstractNaming"
}
)
public abstract class MapEnvelope<X, Y> implements Map<X, Y> {
Expand Down Expand Up @@ -167,16 +166,11 @@ public final int hashCode() {
return new Unchecked<>(
new Folded<>(
42,
(hash, entry) -> {
final int keys = new SumOfInt(
() -> 37 * hash,
() -> entry.getKey().hashCode()
).value();
return new SumOfInt(
() -> 37 * keys,
() -> Objects.hashCode(entry.getValue())
).value();
},
(hash, entry) -> new SumOfInt(
() -> 37 * hash,
() -> entry.getKey().hashCode(),
() -> Objects.hashCode(entry.getValue())
).value(),
this.map.value().entrySet()
)
).value();
Expand All @@ -192,13 +186,11 @@ private Boolean contentsEqual(final Map<?, ?> other) {
return new Unchecked<>(
new And(
(entry) -> {
final X key = entry.getKey();
final Y value = entry.getValue();
return new And(
() -> other.containsKey(key),
() -> other.containsKey(entry.getKey()),
() -> new EqualsNullable(
() -> other.get(key),
value
() -> other.get(entry.getKey()),
entry.getValue()
).value()
).value();
}, this.entrySet()
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/cactoos/scalar/EqualsNullable.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
* <p>There is no thread-safety guarantee.
* @since 1.0
*/
@SuppressWarnings(
{
"PMD.SuspiciousEqualsMethodName",
"PMD.CompareObjectsWithEquals"
}
)
public final class EqualsNullable implements Scalar<Boolean> {
/**
* The first object for comparison.
Expand Down Expand Up @@ -86,6 +80,7 @@ public EqualsNullable(final Scalar<Object> first,
}

@Override
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public Boolean value() throws Exception {
final Object source = this.first.value();
final Object compared = this.second.value();
Expand Down

0 comments on commit ac845f1

Please sign in to comment.