From 3c1295eb9dc532b1e753cf8992c0bb4992efe4b4 Mon Sep 17 00:00:00 2001 From: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com> Date: Fri, 22 Dec 2023 19:44:40 +0100 Subject: [PATCH] Update selected classes to override `equals` and `hashCode` Co-authored-by: Patryk Goworowski --- .../vico/core/model/CartesianLayerModel.kt | 8 +++++ .../core/model/ColumnCartesianLayerModel.kt | 32 +++++++++++++++++++ .../vico/core/model/ExtraStore.kt | 5 +++ .../core/model/LineCartesianLayerModel.kt | 28 ++++++++++++++++ .../ColumnCartesianLayerDrawingModel.kt | 16 ++++++++-- .../vico/core/model/drawing/DrawingModel.kt | 8 +++++ .../drawing/LineCartesianLayerDrawingModel.kt | 20 +++++++++++- .../vico/core/model/XDeltaGcdTest.kt | 8 ++--- 8 files changed, 117 insertions(+), 8 deletions(-) diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/CartesianLayerModel.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/CartesianLayerModel.kt index 44913859c..cf664050c 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/CartesianLayerModel.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/CartesianLayerModel.kt @@ -65,6 +65,10 @@ public interface CartesianLayerModel { */ public fun copy(extraStore: ExtraStore): CartesianLayerModel + override fun equals(other: Any?): Boolean + + override fun hashCode(): Int + /** * Represents a single entity in a [CartesianLayerModel]. */ @@ -73,6 +77,10 @@ public interface CartesianLayerModel { * The _x_ coordinate. */ public val x: Float + + override fun equals(other: Any?): Boolean + + override fun hashCode(): Int } /** diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ColumnCartesianLayerModel.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ColumnCartesianLayerModel.kt index a21c56bde..dd2e3b697 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ColumnCartesianLayerModel.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ColumnCartesianLayerModel.kt @@ -115,11 +115,43 @@ public class ColumnCartesianLayerModel : CartesianLayerModel { extraStore, ) + override fun equals(other: Any?): Boolean = + this === other || + other is ColumnCartesianLayerModel && + series == other.series && + id == other.id && + minX == other.minX && + maxX == other.maxX && + minY == other.minY && + maxY == other.maxY && + minAggregateY == other.minAggregateY && + maxAggregateY == other.maxAggregateY && + xDeltaGcd == other.xDeltaGcd && + extraStore == other.extraStore + + override fun hashCode(): Int { + var result = series.hashCode() + result = 31 * result + id + result = 31 * result + minX.hashCode() + result = 31 * result + maxX.hashCode() + result = 31 * result + minY.hashCode() + result = 31 * result + maxY.hashCode() + result = 31 * result + minAggregateY.hashCode() + result = 31 * result + maxAggregateY.hashCode() + result = 31 * result + xDeltaGcd.hashCode() + result = 31 * result + extraStore.hashCode() + return result + } + /** * Represents a column of height [y] at [x]. */ public class Entry internal constructor(override val x: Float, public val y: Float) : CartesianLayerModel.Entry { public constructor(x: Number, y: Number) : this(x.toFloat(), y.toFloat()) + + override fun equals(other: Any?): Boolean = this === other || other is Entry && x == other.x && y == other.y + + override fun hashCode(): Int = 31 * x.hashCode() + y.hashCode() } /** diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ExtraStore.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ExtraStore.kt index 0aa82def6..046e7f07c 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ExtraStore.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/ExtraStore.kt @@ -51,6 +51,11 @@ public abstract class ExtraStore internal constructor() { */ public abstract operator fun plus(other: ExtraStore): ExtraStore + override fun equals(other: Any?): Boolean = + this === other || other is ExtraStore && mapDelegate == other.mapDelegate + + override fun hashCode(): Int = mapDelegate.hashCode() + /** * Used for writing to and reading from [ExtraStore]s. */ diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/LineCartesianLayerModel.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/LineCartesianLayerModel.kt index 72e031d0b..3d9a1c22c 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/LineCartesianLayerModel.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/LineCartesianLayerModel.kt @@ -87,11 +87,39 @@ public class LineCartesianLayerModel : CartesianLayerModel { override fun copy(extraStore: ExtraStore): CartesianLayerModel = LineCartesianLayerModel(series, id, minX, maxX, minY, maxY, xDeltaGcd, extraStore) + override fun equals(other: Any?): Boolean = + this === other || + other is LineCartesianLayerModel && + series == other.series && + id == other.id && + minX == other.minX && + maxX == other.maxX && + minY == other.minY && + maxY == other.maxY && + xDeltaGcd == other.xDeltaGcd && + extraStore == other.extraStore + + override fun hashCode(): Int { + var result = series.hashCode() + result = 31 * result + id + result = 31 * result + minX.hashCode() + result = 31 * result + maxX.hashCode() + result = 31 * result + minY.hashCode() + result = 31 * result + maxY.hashCode() + result = 31 * result + xDeltaGcd.hashCode() + result = 31 * result + extraStore.hashCode() + return result + } + /** * Represents a line node at ([x], [y]). */ public class Entry internal constructor(override val x: Float, public val y: Float) : CartesianLayerModel.Entry { public constructor(x: Number, y: Number) : this(x.toFloat(), y.toFloat()) + + override fun equals(other: Any?): Boolean = this === other || other is Entry && x == other.x && y == other.y + + override fun hashCode(): Int = 31 * x.hashCode() + y.hashCode() } /** diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/ColumnCartesianLayerDrawingModel.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/ColumnCartesianLayerDrawingModel.kt index d51567680..3964aa9f4 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/ColumnCartesianLayerDrawingModel.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/ColumnCartesianLayerDrawingModel.kt @@ -23,8 +23,10 @@ import com.patrykandpatrick.vico.core.extension.orZero /** * Houses drawing information for a [ColumnCartesianLayer]. [opacity] is the columns’ opacity. */ -public class ColumnCartesianLayerDrawingModel(entries: List>, public val opacity: Float = 1f) : - DrawingModel(entries) { +public class ColumnCartesianLayerDrawingModel( + private val entries: List>, + public val opacity: Float = 1f, +) : DrawingModel(entries) { override fun transform( drawingInfo: List>, from: DrawingModel?, @@ -34,6 +36,12 @@ public class ColumnCartesianLayerDrawingModel(entries: List(private val dra fraction: Float, ): DrawingModel + abstract override fun equals(other: Any?): Boolean + + abstract override fun hashCode(): Int + /** * Houses positional information for a single [CartesianLayer] entity (e.g., a column or a point). */ @@ -49,5 +53,9 @@ public abstract class DrawingModel(private val dra from: DrawingInfo?, fraction: Float, ): DrawingInfo + + override fun equals(other: Any?): Boolean + + override fun hashCode(): Int } } diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/LineCartesianLayerDrawingModel.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/LineCartesianLayerDrawingModel.kt index 4084d78dc..2f12cfa7e 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/LineCartesianLayerDrawingModel.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/model/drawing/LineCartesianLayerDrawingModel.kt @@ -26,7 +26,7 @@ import com.patrykandpatrick.vico.core.extension.orZero * fraction of the [LineCartesianLayer]’s height. */ public class LineCartesianLayerDrawingModel( - pointInfo: List>, + private val pointInfo: List>, public val zeroY: Float, public val opacity: Float = 1f, ) : @@ -45,6 +45,20 @@ public class LineCartesianLayerDrawingModel( ) } + override fun equals(other: Any?): Boolean = + this === other || + other is LineCartesianLayerDrawingModel && + pointInfo == other.pointInfo && + zeroY == other.zeroY && + opacity == other.opacity + + override fun hashCode(): Int { + var result = pointInfo.hashCode() + result = 31 * result + zeroY.hashCode() + result = 31 * result + opacity.hashCode() + return result + } + /** * Houses positional information for a [LineCartesianLayer]’s point. [y] expresses the distance of the point from * the bottom of the [LineCartesianLayer] as a fraction of the [LineCartesianLayer]’s height. @@ -57,5 +71,9 @@ public class LineCartesianLayerDrawingModel( val oldY = (from as? PointInfo)?.y.orZero return PointInfo(oldY.lerp(y, fraction)) } + + override fun equals(other: Any?): Boolean = this === other || other is PointInfo && y == other.y + + override fun hashCode(): Int = y.hashCode() } } diff --git a/vico/core/src/test/java/com/patrykandpatrick/vico/core/model/XDeltaGcdTest.kt b/vico/core/src/test/java/com/patrykandpatrick/vico/core/model/XDeltaGcdTest.kt index 103ccdbdc..23b500d4a 100644 --- a/vico/core/src/test/java/com/patrykandpatrick/vico/core/model/XDeltaGcdTest.kt +++ b/vico/core/src/test/java/com/patrykandpatrick/vico/core/model/XDeltaGcdTest.kt @@ -16,16 +16,14 @@ package com.patrykandpatrick.vico.core.model +import io.mockk.every +import io.mockk.mockk import org.junit.Test import kotlin.test.assertEquals public class XDeltaGcdTest { private fun getEntries(vararg x: Number) = - x.map { value -> - object : CartesianLayerModel.Entry { - override val x: Float get() = value.toFloat() - } - } + x.map { value -> mockk { every { this@mockk.x } returns value.toFloat() } } @Test public fun `Ensure 1 is returned for empty collection`() {