X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FDefaultIdentity.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FDefaultIdentity.kt;h=fe8458409e3d2aa056f28bc1450eff8ee0f3ed9b;hp=0000000000000000000000000000000000000000;hb=d50730f6a330439e0e7ef97ca9329dffe72d5640;hpb=97fe04482ebb8a08e43294acde041c2975cbd8ee 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 index 0000000..fe84584 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentity.kt @@ -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 . + */ + +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().synchronized() + private val properties = mutableMapOf().synchronized() + private val trustCache = mutableMapOf().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: Set) { + 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) { + 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 Set.synchronized(): MutableSet = synchronizedSet(this) +private fun Map.synchronized(): MutableMap = synchronizedMap(this)