From 392c4ab7bb87fa31b0d77377694df60a8f647652 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 1 Nov 2019 23:38:43 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Use=20set=20for=20an=20iden?= =?utf8?q?tity=E2=80=99s=20contexts?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/freenet/wot/Identity.java | 2 +- .../pterodactylus/sone/freenet/wot/DefaultIdentity.kt | 2 +- .../sone/freenet/wot/PluginWebOfTrustConnector.kt | 6 +++--- .../sone/freenet/wot/DefaultIdentityTest.kt | 2 +- .../net/pterodactylus/sone/freenet/wot/Identities.kt | 8 ++++---- .../sone/freenet/wot/IdentityChangeDetectorTest.kt | 8 ++++---- .../sone/freenet/wot/IdentityChangeEventSenderTest.kt | 14 +++++++------- .../sone/freenet/wot/IdentityLoaderTest.kt | 18 +++++++++--------- 8 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java index b4ca58c..3460516 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java @@ -83,7 +83,7 @@ public interface Identity { * @param contexts * All contexts of the identity */ - public void setContexts(Collection contexts); + public void setContexts(Set contexts); /** * Removes the given context from this identity. diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt index d50a4ba..3398e7f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt @@ -35,7 +35,7 @@ open class DefaultIdentity(private val id: String, private val nickname: String, override fun hasContext(context: String) = context in contexts - override fun setContexts(contexts: Collection) { + override fun setContexts(contexts: Set) { synchronized(this.contexts) { this.contexts.clear() this.contexts.addAll(contexts) diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt index b0dfa37..b409616 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt @@ -45,7 +45,7 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: val insertUri = fields.get("InsertURI$ownIdentityCounter") val nickname = fields.get("Nickname$ownIdentityCounter") val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri) - ownIdentity.setContexts(fields.contexts("Contexts$ownIdentityCounter.")) + ownIdentity.contexts = fields.contexts("Contexts$ownIdentityCounter.") ownIdentity.properties = fields.properties("Properties$ownIdentityCounter.") ownIdentities.add(ownIdentity) } @@ -61,7 +61,7 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: val nickname = fields.get("Nickname$identityCounter") val requestUri = fields.get("RequestURI$identityCounter") val identity = DefaultIdentity(id, nickname, requestUri) - identity.setContexts(fields.contexts("Contexts$identityCounter.")) + identity.contexts = fields.contexts("Contexts$identityCounter.") identity.properties = fields.properties("Properties$identityCounter.") val trust = parseInt(fields.get("Trust$identityCounter"), null) val score = parseInt(fields.get("Score$identityCounter"), 0)!! @@ -149,7 +149,7 @@ private fun SimpleFieldSet.contexts(prefix: String) = generateSequence(0, Int::inc) .map { get("${prefix}Context$it") } .takeWhile { it != null } - .toList() + .toSet() private fun SimpleFieldSet.properties(prefix: String) = generateSequence(0, Int::inc) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt index 1c12b33..2cd7bda 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt @@ -64,7 +64,7 @@ open class DefaultIdentityTest { @Test fun `contexts are set correctly in bulk`() { identity.addContext("Test") - identity.setContexts(listOf("Test1", "Test2")) + identity.contexts = setOf("Test1", "Test2") assertThat(identity.contexts, containsInAnyOrder("Test1", "Test2")) assertThat(identity.hasContext("Test"), equalTo(false)) assertThat(identity.hasContext("Test1"), equalTo(true)) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/Identities.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/Identities.kt index 57ab8c5..9d74b65 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/Identities.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/Identities.kt @@ -17,19 +17,19 @@ package net.pterodactylus.sone.freenet.wot -fun createOwnIdentity(id: String, contexts: Collection, vararg properties: Pair): OwnIdentity { +fun createOwnIdentity(id: String, contexts: Set, vararg properties: Pair): OwnIdentity { val ownIdentity = DefaultOwnIdentity(id, "Nickname$id", "Request$id", "Insert$id") setContextsAndPropertiesOnIdentity(ownIdentity, contexts, mapOf(*properties)) return ownIdentity } -fun createIdentity(id: String, contexts: Collection, vararg properties: Pair): Identity { +fun createIdentity(id: String, contexts: Set, vararg properties: Pair): Identity { val identity = DefaultIdentity(id, "Nickname$id", "Request$id") setContextsAndPropertiesOnIdentity(identity, contexts, mapOf(*properties)) return identity } -private fun setContextsAndPropertiesOnIdentity(identity: Identity, contexts: Collection, properties: Map) { - identity.setContexts(contexts) +private fun setContextsAndPropertiesOnIdentity(identity: Identity, contexts: Set, properties: Map) { + identity.contexts = contexts identity.properties = properties } diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt index aee99b7..e9f8092 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt @@ -140,15 +140,15 @@ class IdentityChangeDetectorTest { listOf(createIdentity1(), createIdentity2(), createIdentity3()) private fun createIdentity1() = - createIdentity("Test1", listOf("Context A", "Context B"), "Key A" to "Value A", "Key B" to "Value B") + createIdentity("Test1", setOf("Context A", "Context B"), "Key A" to "Value A", "Key B" to "Value B") private fun createIdentity2() = - createIdentity("Test2", listOf("Context C", "Context D"), "Key C" to "Value C", "Key D" to "Value D") + createIdentity("Test2", setOf("Context C", "Context D"), "Key C" to "Value C", "Key D" to "Value D") private fun createIdentity3() = - createIdentity("Test3", listOf("Context E", "Context F"), "Key E" to "Value E", "Key F" to "Value F") + createIdentity("Test3", setOf("Context E", "Context F"), "Key E" to "Value E", "Key F" to "Value F") private fun createIdentity4() = - createIdentity("Test4", listOf("Context G", "Context H"), "Key G" to "Value G", "Key H" to "Value H") + createIdentity("Test4", setOf("Context G", "Context H"), "Key G" to "Value G", "Key H" to "Value H") } diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt index 5a4e853..19fdda0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt @@ -31,15 +31,15 @@ class IdentityChangeEventSenderTest { private val eventBus = mock() private val ownIdentities = listOf( - createOwnIdentity("O1", listOf("Test"), "KeyA" to "ValueA"), - createOwnIdentity("O2", listOf("Test2"), "KeyB" to "ValueB"), - createOwnIdentity("O3", listOf("Test3"), "KeyC" to "ValueC") + createOwnIdentity("O1", setOf("Test"), "KeyA" to "ValueA"), + createOwnIdentity("O2", setOf("Test2"), "KeyB" to "ValueB"), + createOwnIdentity("O3", setOf("Test3"), "KeyC" to "ValueC") ) private val identities = listOf( - createIdentity("I1", listOf()), - createIdentity("I2", listOf()), - createIdentity("I3", listOf()), - createIdentity("I2", listOf("Test")) + createIdentity("I1", setOf()), + createIdentity("I2", setOf()), + createIdentity("I3", setOf()), + createIdentity("I2", setOf("Test")) ) private val identityChangeEventSender = IdentityChangeEventSender(eventBus, createOldIdentities()) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt index f5fada8..780288d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt @@ -70,33 +70,33 @@ class IdentityLoaderTest { } private fun createOwnIdentities() = listOf( - createOwnIdentity("O1", "ON1", "OR1", "OI1", listOf("Test", "Test2"), mapOf("KeyA" to "ValueA", "KeyB" to "ValueB")), - createOwnIdentity("O2", "ON2", "OR2", "OI2", listOf("Test"), mapOf("KeyC" to "ValueC")), - createOwnIdentity("O3", "ON3", "OR3", "OI3", listOf("Test2"), mapOf("KeyE" to "ValueE", "KeyD" to "ValueD")), - createOwnIdentity("O4", "ON4", "OR$", "OI4", listOf("Test"), mapOf("KeyA" to "ValueA", "KeyD" to "ValueD")) + createOwnIdentity("O1", "ON1", "OR1", "OI1", setOf("Test", "Test2"), mapOf("KeyA" to "ValueA", "KeyB" to "ValueB")), + createOwnIdentity("O2", "ON2", "OR2", "OI2", setOf("Test"), mapOf("KeyC" to "ValueC")), + createOwnIdentity("O3", "ON3", "OR3", "OI3", setOf("Test2"), mapOf("KeyE" to "ValueE", "KeyD" to "ValueD")), + createOwnIdentity("O4", "ON4", "OR$", "OI4", setOf("Test"), mapOf("KeyA" to "ValueA", "KeyD" to "ValueD")) ) private fun createTrustedIdentitiesForFirstOwnIdentity() = setOf( - createIdentity("I11", "IN11", "IR11", listOf("Test"), mapOf("KeyA" to "ValueA")) + createIdentity("I11", "IN11", "IR11", setOf("Test"), mapOf("KeyA" to "ValueA")) ) private fun createTrustedIdentitiesForSecondOwnIdentity() = setOf( - createIdentity("I21", "IN21", "IR21", listOf("Test", "Test2"), mapOf("KeyB" to "ValueB")) + createIdentity("I21", "IN21", "IR21", setOf("Test", "Test2"), mapOf("KeyB" to "ValueB")) ) private fun createTrustedIdentitiesForThirdOwnIdentity() = setOf( - createIdentity("I31", "IN31", "IR31", listOf("Test", "Test3"), mapOf("KeyC" to "ValueC")) + createIdentity("I31", "IN31", "IR31", setOf("Test", "Test3"), mapOf("KeyC" to "ValueC")) ) private fun createTrustedIdentitiesForFourthOwnIdentity(): Set = emptySet() -private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: List, properties: Map): OwnIdentity = +private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Set, properties: Map): OwnIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply { setContexts(contexts) this.properties = properties } -private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: List, properties: Map): Identity = +private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: Set, properties: Map): Identity = DefaultIdentity(id, nickname, requestUri).apply { setContexts(contexts) this.properties = properties -- 2.7.4