♻️ Use set for an identity’s contexts
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Nov 2019 22:38:43 +0000 (23:38 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Nov 2019 22:38:43 +0000 (23:38 +0100)
src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java
src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt
src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/Identities.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt

index b4ca58c..3460516 100644 (file)
@@ -83,7 +83,7 @@ public interface Identity {
         * @param contexts
         *            All contexts of the identity
         */
-       public void setContexts(Collection<String> contexts);
+       public void setContexts(Set<String> contexts);
 
        /**
         * Removes the given context from this identity.
index d50a4ba..3398e7f 100644 (file)
@@ -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<String>) {
+       override fun setContexts(contexts: Set<String>) {
                synchronized(this.contexts) {
                        this.contexts.clear()
                        this.contexts.addAll(contexts)
index b0dfa37..b409616 100644 (file)
@@ -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)
index 1c12b33..2cd7bda 100644 (file)
@@ -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))
index 57ab8c5..9d74b65 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot
 
-fun createOwnIdentity(id: String, contexts: Collection<String>, vararg properties: Pair<String, String>): OwnIdentity {
+fun createOwnIdentity(id: String, contexts: Set<String>, vararg properties: Pair<String, String>): OwnIdentity {
        val ownIdentity = DefaultOwnIdentity(id, "Nickname$id", "Request$id", "Insert$id")
        setContextsAndPropertiesOnIdentity(ownIdentity, contexts, mapOf(*properties))
        return ownIdentity
 }
 
-fun createIdentity(id: String, contexts: Collection<String>, vararg properties: Pair<String, String>): Identity {
+fun createIdentity(id: String, contexts: Set<String>, vararg properties: Pair<String, String>): Identity {
        val identity = DefaultIdentity(id, "Nickname$id", "Request$id")
        setContextsAndPropertiesOnIdentity(identity, contexts, mapOf(*properties))
        return identity
 }
 
-private fun setContextsAndPropertiesOnIdentity(identity: Identity, contexts: Collection<String>, properties: Map<String, String>) {
-       identity.setContexts(contexts)
+private fun setContextsAndPropertiesOnIdentity(identity: Identity, contexts: Set<String>, properties: Map<String, String>) {
+       identity.contexts = contexts
        identity.properties = properties
 }
index aee99b7..e9f8092 100644 (file)
@@ -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")
 
 }
index 5a4e853..19fdda0 100644 (file)
@@ -31,15 +31,15 @@ class IdentityChangeEventSenderTest {
 
        private val eventBus = mock<EventBus>()
        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())
 
index f5fada8..780288d 100644 (file)
@@ -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<Identity> = emptySet()
 
-private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: List<String>, properties: Map<String, String>): OwnIdentity =
+private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Set<String>, properties: Map<String, String>): OwnIdentity =
                DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply {
                        setContexts(contexts)
                        this.properties = properties
                }
 
-private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: List<String>, properties: Map<String, String>): Identity =
+private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: Set<String>, properties: Map<String, String>): Identity =
                DefaultIdentity(id, nickname, requestUri).apply {
                        setContexts(contexts)
                        this.properties = properties