From fc5a79eca3e689e28b80594084d0f109c8ff911d Mon Sep 17 00:00:00 2001 From: Mugurell Date: Tue, 6 Jul 2021 16:45:31 +0300 Subject: [PATCH] For #10555 - Fix InputResultDetail hashcode() collisions The previous implementation was affected by the fact that the class has 3 integer properties, all with values [0..4] (-1 was added recently) and it would just add those value to compute the hashcode. Collisions were a given. By using decimal places for each property the new implementation should avoid collisions while allowing for all the other expected guarantees. --- .../concept/engine/InputResultDetail.kt | 6 +++--- .../concept/engine/InputResultDetailTest.kt | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/components/concept/engine/src/main/java/mozilla/components/concept/engine/InputResultDetail.kt b/components/concept/engine/src/main/java/mozilla/components/concept/engine/InputResultDetail.kt index f84dfec221e..f52f38b431d 100644 --- a/components/concept/engine/src/main/java/mozilla/components/concept/engine/InputResultDetail.kt +++ b/components/concept/engine/src/main/java/mozilla/components/concept/engine/InputResultDetail.kt @@ -134,9 +134,9 @@ class InputResultDetail private constructor( @Suppress("MagicNumber") override fun hashCode(): Int { - var hash = inputResult.hashCode() * 31 - hash += (scrollDirections.hashCode()) * 31 - hash += (overscrollDirections.hashCode()) * 31 + var hash = inputResult.hashCode() + hash += (scrollDirections.hashCode()) * 10 + hash += (overscrollDirections.hashCode()) * 100 return hash } diff --git a/components/concept/engine/src/test/java/mozilla/components/concept/engine/InputResultDetailTest.kt b/components/concept/engine/src/test/java/mozilla/components/concept/engine/InputResultDetailTest.kt index 5113d1f306e..96c2444734c 100644 --- a/components/concept/engine/src/test/java/mozilla/components/concept/engine/InputResultDetailTest.kt +++ b/components/concept/engine/src/test/java/mozilla/components/concept/engine/InputResultDetailTest.kt @@ -115,15 +115,15 @@ class InputResultDetailTest { assertTrue(inputResultDetail == inputResultDetail) } - // @Test - // fun `GIVEN an InputResultDetail WHEN hashCode is called for same values objects THEN it returns the same result`() { - // assertEquals(inputResultDetail.hashCode(), inputResultDetail.hashCode()) - // - // assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance().hashCode()) - // - // inputResultDetail = inputResultDetail.copy(OVERSCROLL_DIRECTIONS_VERTICAL) - // assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance(true).hashCode()) - // } + @Test + fun `GIVEN an InputResultDetail WHEN hashCode is called for same values objects THEN it returns the same result`() { + assertEquals(inputResultDetail.hashCode(), inputResultDetail.hashCode()) + + assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance().hashCode()) + + inputResultDetail = inputResultDetail.copy(overscrollDirections = OVERSCROLL_DIRECTIONS_VERTICAL) + assertEquals(inputResultDetail.hashCode(), InputResultDetail.newInstance(true).hashCode()) + } @Test fun `GIVEN an InputResultDetail WHEN hashCode is called for different values objects THEN it returns different results`() {