Skip to content

Commit

Permalink
Update selected classes to override equals and hashCode
Browse files Browse the repository at this point in the history
Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Dec 22, 2023
1 parent 729e038 commit 3c1295e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*/
Expand All @@ -73,6 +77,10 @@ public interface CartesianLayerModel {
* The _x_ coordinate.
*/
public val x: Float

override fun equals(other: Any?): Boolean

override fun hashCode(): Int
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<Float, ColumnInfo>>, public val opacity: Float = 1f) :
DrawingModel<ColumnCartesianLayerDrawingModel.ColumnInfo>(entries) {
public class ColumnCartesianLayerDrawingModel(
private val entries: List<Map<Float, ColumnInfo>>,
public val opacity: Float = 1f,
) : DrawingModel<ColumnCartesianLayerDrawingModel.ColumnInfo>(entries) {
override fun transform(
drawingInfo: List<Map<Float, ColumnInfo>>,
from: DrawingModel<ColumnInfo>?,
Expand All @@ -34,6 +36,12 @@ public class ColumnCartesianLayerDrawingModel(entries: List<Map<Float, ColumnInf
return ColumnCartesianLayerDrawingModel(drawingInfo, oldOpacity.lerp(opacity, fraction))
}

override fun equals(other: Any?): Boolean =
this === other ||
other is ColumnCartesianLayerDrawingModel && entries == other.entries && opacity == other.opacity

override fun hashCode(): Int = 31 * entries.hashCode() + opacity.hashCode()

/**
* Houses positional information for a [ColumnCartesianLayer]’s column. [height] expresses the column’s height as a
* fraction of the [ColumnCartesianLayer]’s height.
Expand All @@ -46,5 +54,9 @@ public class ColumnCartesianLayerDrawingModel(entries: List<Map<Float, ColumnInf
val oldHeight = (from as? ColumnInfo)?.height.orZero
return ColumnInfo(oldHeight.lerp(height, fraction))
}

override fun equals(other: Any?): Boolean = this === other || other is ColumnInfo && height == other.height

override fun hashCode(): Int = height.hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public abstract class DrawingModel<T : DrawingModel.DrawingInfo>(private val dra
fraction: Float,
): DrawingModel<T>

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).
*/
Expand All @@ -49,5 +53,9 @@ public abstract class DrawingModel<T : DrawingModel.DrawingInfo>(private val dra
from: DrawingInfo?,
fraction: Float,
): DrawingInfo

override fun equals(other: Any?): Boolean

override fun hashCode(): Int
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.patrykandpatrick.vico.core.extension.orZero
* fraction of the [LineCartesianLayer]’s height.
*/
public class LineCartesianLayerDrawingModel(
pointInfo: List<Map<Float, PointInfo>>,
private val pointInfo: List<Map<Float, PointInfo>>,
public val zeroY: Float,
public val opacity: Float = 1f,
) :
Expand All @@ -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.
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CartesianLayerModel.Entry> { every { this@mockk.x } returns value.toFloat() } }

@Test
public fun `Ensure 1 is returned for empty collection`() {
Expand Down

0 comments on commit 3c1295e

Please sign in to comment.