Skip to content

Commit

Permalink
Fix equality and hashcode in identifiable collections (#132)
Browse files Browse the repository at this point in the history
* Fix equality amd hashcode in identifiable collections

* Bump version
  • Loading branch information
micbakos-rdx authored May 9, 2024
1 parent e4e8e46 commit 3d3dec8
Show file tree
Hide file tree
Showing 28 changed files with 884 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "0.7.9"
version = "0.7.10"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Account
import com.radixdlt.sargon.AccountAddress
import com.radixdlt.sargon.annotation.KoverIgnore

class Accounts private constructor(
array: IdentifiedArray<AccountAddress, Account>
private val array: IdentifiedArray<AccountAddress, Account>
) : IdentifiedArray<AccountAddress, Account> by array {

constructor(accounts: List<Account>) : this(
Expand All @@ -15,5 +16,26 @@ class Accounts private constructor(
)

constructor(vararg account: Account) : this(accounts = account.asList())

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Accounts

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "Accounts(array=$array)"
}

}

fun List<Account>.asIdentifiable() = Accounts(accounts = this)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AssetException
import com.radixdlt.sargon.ResourceAddress
import com.radixdlt.sargon.annotation.KoverIgnore

class AssetsExceptionList private constructor(
array: IdentifiedArray<ResourceAddress, AssetException>
private val array: IdentifiedArray<ResourceAddress, AssetException>
) : IdentifiedArray<ResourceAddress, AssetException> by array {

constructor(assetExceptions: List<AssetException>) : this(
Expand All @@ -17,4 +18,26 @@ class AssetsExceptionList private constructor(
constructor(
vararg assetException: AssetException
) : this(assetExceptions = assetException.asList())
}

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as AssetsExceptionList

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "AssetsExceptionList(array=$array)"
}

}

fun List<AssetException>.asIdentifiable() = AssetsExceptionList(assetExceptions = this)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AccountAddress
import com.radixdlt.sargon.AuthorizedDapp
import com.radixdlt.sargon.annotation.KoverIgnore

class AuthorizedDapps private constructor(
array: IdentifiedArray<AccountAddress, AuthorizedDapp>
private val array: IdentifiedArray<AccountAddress, AuthorizedDapp>
) : IdentifiedArray<AccountAddress, AuthorizedDapp> by array {

constructor(authorizedDapps: List<AuthorizedDapp>) : this(
Expand All @@ -17,4 +18,26 @@ class AuthorizedDapps private constructor(
constructor(
vararg authorizedDapp: AuthorizedDapp
) : this(authorizedDapps = authorizedDapp.asList())
}

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as AuthorizedDapps

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "AuthorizedDapps(array=$array)"
}

}

fun List<AuthorizedDapp>.asIdentifiable() = AuthorizedDapps(authorizedDapps = this)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.ResourceOrNonFungible
import com.radixdlt.sargon.annotation.KoverIgnore

class DepositorsAllowList private constructor(
array: IdentifiedArray<ResourceOrNonFungible, ResourceOrNonFungible>
private val array: IdentifiedArray<ResourceOrNonFungible, ResourceOrNonFungible>
) : IdentifiedArray<ResourceOrNonFungible, ResourceOrNonFungible> by array {

constructor(resourcesOrNonFungibles: List<ResourceOrNonFungible>) : this(
Expand All @@ -16,4 +17,27 @@ class DepositorsAllowList private constructor(
constructor(vararg resourceOrNonFungible: ResourceOrNonFungible) : this(
resourcesOrNonFungibles = resourceOrNonFungible.asList()
)
}

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as DepositorsAllowList

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "DepositorsAllowList(array=$array)"
}

}

fun List<ResourceOrNonFungible>.asIdentifiable() =
DepositorsAllowList(resourcesOrNonFungibles = this)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.EntityFlag
import com.radixdlt.sargon.annotation.KoverIgnore

class EntityFlags private constructor(
array: IdentifiedArray<EntityFlag, EntityFlag>
private val array: IdentifiedArray<EntityFlag, EntityFlag>
) : IdentifiedArray<EntityFlag, EntityFlag> by array {

constructor(entityFlags: List<EntityFlag>) : this(
Expand All @@ -15,5 +16,25 @@ class EntityFlags private constructor(

constructor(vararg entityFlag: EntityFlag) : this(entityFlags = entityFlag.asList())

companion object
}
@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as EntityFlags

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "EntityFlags(array=$array)"
}

}

fun List<EntityFlag>.asIdentifiable() = EntityFlags(entityFlags = this)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.FactorSourceId
import com.radixdlt.sargon.annotation.KoverIgnore

class FactorSources private constructor(
array: IdentifiedArray<FactorSourceId, FactorSource>
private val array: IdentifiedArray<FactorSourceId, FactorSource>
) : IdentifiedArray<FactorSourceId, FactorSource> by array {

constructor(factorSources: List<FactorSource>) : this(
Expand All @@ -17,5 +18,26 @@ class FactorSources private constructor(
constructor(vararg factorSource: FactorSource) : this(
factorSources = factorSource.asList()
)

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as FactorSources

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "FactorSources(array=$array)"
}

}

fun List<FactorSource>.asIdentifiable() = FactorSources(factorSources = this)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Gateway
import com.radixdlt.sargon.Url
import com.radixdlt.sargon.annotation.KoverIgnore

class Gateways private constructor(
array: IdentifiedArray<Url, Gateway>
private val array: IdentifiedArray<Url, Gateway>
) : IdentifiedArray<Url, Gateway> by array {

constructor(gateways: List<Gateway>) : this(
Expand All @@ -15,4 +16,27 @@ class Gateways private constructor(
)

constructor(vararg gateway: Gateway) : this(gateways = gateway.asList())

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Gateways

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "Gateways(array=$array)"
}

}

fun List<Gateway>.asIdentifiable() = Gateways(gateways = this)

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.radixdlt.sargon.extensions

internal interface IdentifiedArray<Identifier, Element> {
import com.radixdlt.sargon.annotation.KoverIgnore

interface IdentifiedArray<Identifier, Element> {
val size: Int

fun asList(): List<Element>
Expand All @@ -17,7 +19,15 @@ internal interface IdentifiedArray<Identifier, Element> {

operator fun get(index: Int): Element

operator fun contains(element: Element): Boolean

fun removeBy(identifier: Identifier): IdentifiedArray<Identifier, Element>

override fun equals(other: Any?): Boolean

override fun hashCode(): Int

override fun toString(): String
}


Expand All @@ -42,6 +52,8 @@ internal class IdentifiedArrayImpl<Identifier, Element>(

override fun get(index: Int): Element = inner.values.elementAt(index)

override fun contains(element: Element): Boolean = inner.contains(identifier(element))

override fun append(element: Element) = apply {
val identifier = identifier(element)
if (!inner.contains(identifier)) {
Expand Down Expand Up @@ -82,4 +94,23 @@ internal class IdentifiedArrayImpl<Identifier, Element>(
override fun removeBy(identifier: Identifier) = apply {
inner.remove(identifier)
}

@KoverIgnore
override fun equals(other: Any?): Boolean {
if (this === other) return true

return inner == (other as? IdentifiedArrayImpl<*, *>)?.inner
}

@KoverIgnore
override fun hashCode(): Int {
return inner.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "IdentifiedArrayImpl(inner=$inner)"
}


}
Loading

0 comments on commit 3d3dec8

Please sign in to comment.