Repository: yegor256/cactoos
Pull request #1685 (2023-07-21 10:05:31)

IterableOf, MapEntry and MapEnvelope started using HashCode instead o…
…f thir own hashing

#1440

catdog905: @yegor256 Hello! Could you check this PR that may close #1440 issue?
yegor256: @catdog905 many thanks for your contribution! Please, run `mvn clean install -Pqulice` and then fix the code formatting issues

Patch:
diff --git a/src/main/java/org/cactoos/iterable/IterableOf.java b/src/main/java/org/cactoos/iterable/IterableOf.java
index 3ff8e3cb..12aa2559 100644
--- a/src/main/java/org/cactoos/iterable/IterableOf.java
+++ b/src/main/java/org/cactoos/iterable/IterableOf.java
@@ -29,7 +29,7 @@ import org.cactoos.Fallback;
 import org.cactoos.Scalar;
 import org.cactoos.iterator.IteratorOf;
 import org.cactoos.scalar.And;
-import org.cactoos.scalar.Folded;
+import org.cactoos.scalar.HashCode;
 import org.cactoos.scalar.Or;
 import org.cactoos.scalar.ScalarWithFallback;
 import org.cactoos.scalar.Unchecked;
@@ -119,11 +119,7 @@ public final class IterableOf<X> implements Iterable<X> {
     @Override
     public int hashCode() {
         return new Unchecked<>(
-            new Folded<>(
-                42,
-                (hash, entry) -> 37 * hash + entry.hashCode(),
-                this
-            )
+            new HashCode(42, 37, this)
         ).value();
     }
 
diff --git a/src/main/java/org/cactoos/map/MapEntry.java b/src/main/java/org/cactoos/map/MapEntry.java
index 3b0c3c32..209e8b76 100644
--- a/src/main/java/org/cactoos/map/MapEntry.java
+++ b/src/main/java/org/cactoos/map/MapEntry.java
@@ -24,6 +24,8 @@
 package org.cactoos.map;
 
 import java.util.Map;
+import org.cactoos.iterable.IterableOf;
+import org.cactoos.scalar.HashCode;
 import org.cactoos.text.FormattedText;
 import org.cactoos.text.UncheckedText;
 
@@ -94,17 +96,8 @@ public final class MapEntry<K, V> implements Map.Entry<K, V> {
 
     @Override
     public int hashCode() {
-        int hash;
-        if (this.key == null) {
-            hash = 0;
-        } else {
-            hash = this.key.hashCode();
-        }
-        if (this.value == null) {
-            hash ^= 1;
-        } else {
-            hash ^= this.value.hashCode();
-        }
-        return hash;
+        return new HashCode(
+            new IterableOf<>(this.key, this.value)
+        ).value();
     }
 }
diff --git a/src/main/java/org/cactoos/map/MapEnvelope.java b/src/main/java/org/cactoos/map/MapEnvelope.java
index 43df0659..2b19184b 100644
--- a/src/main/java/org/cactoos/map/MapEnvelope.java
+++ b/src/main/java/org/cactoos/map/MapEnvelope.java
@@ -27,6 +27,7 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
 import org.cactoos.iterable.Mapped;
+import org.cactoos.scalar.HashCode;
 import org.cactoos.text.Concatenated;
 import org.cactoos.text.Joined;
 import org.cactoos.text.TextOf;
@@ -141,7 +142,7 @@ public abstract class MapEnvelope<X, Y> implements Map<X, Y> {
 
     @Override
     public final int hashCode() {
-        return this.map.hashCode();
+        return new HashCode(this.map).value();
     }
 
 }


Test patch:
diff --git a/src/test/java/org/cactoos/map/MapEntryTest.java b/src/test/java/org/cactoos/map/MapEntryTest.java
index 65d1a456..913876fa 100644
--- a/src/test/java/org/cactoos/map/MapEntryTest.java
+++ b/src/test/java/org/cactoos/map/MapEntryTest.java
@@ -85,7 +85,7 @@ final class MapEntryTest {
         MatcherAssert.assertThat(
             "the hash code are not equals",
             new MapEntry<>("elegant", "objects").hashCode(),
-            new IsEqual<>(32_739_498)
+            new IsEqual<>(-1_673_632_025)
         );
     }