🎨 Replace DefaultIdentity with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 9 Oct 2019 17:09:43 +0000 (19:09 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 9 Oct 2019 17:09:43 +0000 (19:09 +0200)
src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java [deleted file]
src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java
src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java
deleted file mode 100644 (file)
index 0d92d61..0000000
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * Sone - DefaultIdentity.java - Copyright Â© 2010–2019 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.freenet.wot;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * A Web of Trust identity.
- */
-public class DefaultIdentity implements Identity {
-
-       /** The ID of the identity. */
-       private final String id;
-
-       /** The nickname of the identity. */
-       private final String nickname;
-
-       /** The request URI of the identity. */
-       private final String requestUri;
-
-       /** The contexts of the identity. */
-       private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
-
-       /** The properties of the identity. */
-       private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
-
-       /** Cached trust. */
-       private final Map<OwnIdentity, Trust> trustCache = Collections.synchronizedMap(new HashMap<OwnIdentity, Trust>());
-
-       /**
-        * Creates a new identity.
-        *
-        * @param id
-        *            The ID of the identity
-        * @param nickname
-        *            The nickname of the identity
-        * @param requestUri
-        *            The request URI of the identity
-        */
-       public DefaultIdentity(String id, String nickname, String requestUri) {
-               this.id = id;
-               this.nickname = nickname;
-               this.requestUri = requestUri;
-       }
-
-       //
-       // ACCESSORS
-       //
-
-       @Override
-       public String getId() {
-               return id;
-       }
-
-       @Override
-       public String getNickname() {
-               return nickname;
-       }
-
-       @Override
-       public String getRequestUri() {
-               return requestUri;
-       }
-
-       @Override
-       public Set<String> getContexts() {
-               return Collections.unmodifiableSet(contexts);
-       }
-
-       @Override
-       public boolean hasContext(String context) {
-               return contexts.contains(context);
-       }
-
-       @Override
-       public void setContexts(Collection<String> contexts) {
-               this.contexts.clear();
-               this.contexts.addAll(contexts);
-       }
-
-       @Override
-       public Identity addContext(String context) {
-               contexts.add(context);
-               return this;
-       }
-
-       @Override
-       public Identity removeContext(String context) {
-               contexts.remove(context);
-               return this;
-       }
-
-       @Override
-       public Map<String, String> getProperties() {
-               return Collections.unmodifiableMap(properties);
-       }
-
-       @Override
-       public void setProperties(Map<String, String> properties) {
-               this.properties.clear();
-               this.properties.putAll(properties);
-       }
-
-       @Override
-       public String getProperty(String name) {
-               return properties.get(name);
-       }
-
-       @Override
-       public Identity setProperty(String name, String value) {
-               properties.put(name, value);
-               return this;
-       }
-
-       @Override
-       public Identity removeProperty(String name) {
-               properties.remove(name);
-               return this;
-       }
-
-       @Override
-       public Trust getTrust(OwnIdentity ownIdentity) {
-               return trustCache.get(ownIdentity);
-       }
-
-       @Override
-       public Identity setTrust(OwnIdentity ownIdentity, Trust trust) {
-               trustCache.put(ownIdentity, trust);
-               return this;
-       }
-
-       @Override
-       public Identity removeTrust(OwnIdentity ownIdentity) {
-               trustCache.remove(ownIdentity);
-               return this;
-       }
-
-       //
-       // OBJECT METHODS
-       //
-
-       @Override
-       public int hashCode() {
-               return getId().hashCode();
-       }
-
-       @Override
-       public boolean equals(Object object) {
-               if (!(object instanceof Identity)) {
-                       return false;
-               }
-               Identity identity = (Identity) object;
-               return identity.getId().equals(getId());
-       }
-
-       @Override
-       public String toString() {
-               return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
-       }
-
-}
index e2e3a74..de239db 100644 (file)
@@ -84,8 +84,8 @@ public class DefaultOwnIdentity extends DefaultIdentity implements OwnIdentity {
        }
 
        @Override
        }
 
        @Override
-       public boolean equals(Object object) {
-               return super.equals(object);
+       public boolean equals(Object other) {
+               return super.equals(other);
        }
 
 }
        }
 
 }
diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt
new file mode 100644 (file)
index 0000000..d50a4ba
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Sone - DefaultIdentity.java - Copyright Â© 2010–2019 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot
+
+import java.util.Collections.*
+
+/**
+ * A Web of Trust identity.
+ */
+open class DefaultIdentity(private val id: String, private val nickname: String, private val requestUri: String) : Identity {
+
+       private val contexts = mutableSetOf<String>().synchronized()
+       private val properties = mutableMapOf<String, String>().synchronized()
+       private val trustCache = mutableMapOf<OwnIdentity, Trust>().synchronized()
+
+       override fun getId() = id
+       override fun getNickname() = nickname
+       override fun getRequestUri() = requestUri
+       override fun getContexts() = synchronized(contexts) { contexts.toSet() }
+
+       override fun hasContext(context: String) = context in contexts
+
+       override fun setContexts(contexts: Collection<String>) {
+               synchronized(this.contexts) {
+                       this.contexts.clear()
+                       this.contexts.addAll(contexts)
+               }
+       }
+
+       override fun addContext(context: String): Identity = apply {
+               synchronized(this.contexts) {
+                       contexts += context
+               }
+       }
+
+       override fun removeContext(context: String): Identity = apply {
+               synchronized(this.contexts) {
+                       contexts -= context
+               }
+       }
+
+       override fun getProperties() = synchronized(properties) { properties.toMap() }
+
+       override fun setProperties(properties: Map<String, String>) {
+               synchronized(this.properties) {
+                       this.properties.clear()
+                       this.properties.putAll(properties)
+               }
+       }
+
+       override fun getProperty(name: String) = synchronized(properties) { properties[name] }
+
+       override fun setProperty(name: String, value: String): Identity = apply {
+               synchronized(properties) {
+                       properties[name] = value
+               }
+       }
+
+       override fun removeProperty(name: String): Identity = apply {
+               synchronized(properties) {
+                       properties -= name
+               }
+       }
+
+       override fun getTrust(ownIdentity: OwnIdentity) = synchronized(trustCache) {
+               trustCache[ownIdentity]
+       }
+
+       override fun setTrust(ownIdentity: OwnIdentity, trust: Trust) = apply {
+               synchronized(trustCache) {
+                       trustCache[ownIdentity] = trust
+               }
+       }
+
+       override fun removeTrust(ownIdentity: OwnIdentity) = apply {
+               synchronized(trustCache) {
+                       trustCache -= ownIdentity
+               }
+       }
+
+       override fun hashCode() = id.hashCode()
+
+       override fun equals(other: Any?) = if (other !is Identity) {
+               false
+       } else {
+               other.id == getId()
+       }
+
+       override fun toString() = "${javaClass.simpleName}[id=$id,nickname=$nickname,contexts=$contexts,properties=$properties]"
+
+}
+
+private fun <T> Set<T>.synchronized(): MutableSet<T> = synchronizedSet(this)
+private fun <K, V> Map<K, V>.synchronized(): MutableMap<K, V> = synchronizedMap(this)
index 07126bb..ca11971 100644 (file)
@@ -70,7 +70,7 @@ class IdentityLoaderTest {
        private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: List<String>, properties: Map<String, String>): OwnIdentity =
                        DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply {
                                setContexts(contexts)
        private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: List<String>, properties: Map<String, String>): OwnIdentity =
                        DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply {
                                setContexts(contexts)
-                               this.properties = properties
+                               this.setProperties(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: List<String>, properties: Map<String, String>): Identity =