2 * Sone - DefaultIdentity.kt - Copyright © 2010–2020 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot
20 import java.util.Collections.synchronizedMap
21 import java.util.Collections.synchronizedSet
22 import kotlin.collections.set
25 * A Web of Trust identity.
27 open class DefaultIdentity(private val id: String, private val nickname: String?, private val requestUri: String) : Identity {
29 private val contexts = mutableSetOf<String>().synchronized()
30 private val properties = mutableMapOf<String, String>().synchronized()
31 private val trustCache = mutableMapOf<OwnIdentity, Trust>().synchronized()
33 override fun getId() = id
34 override fun getNickname() = nickname
35 override fun getRequestUri() = requestUri
36 override fun getContexts() = synchronized(contexts) { contexts.toSet() }
38 override fun hasContext(context: String) = context in contexts
40 override fun setContexts(contexts: Set<String>) {
41 synchronized(this.contexts) {
43 this.contexts.addAll(contexts)
47 override fun addContext(context: String): Identity = apply {
48 synchronized(this.contexts) {
53 override fun removeContext(context: String): Identity = apply {
54 synchronized(this.contexts) {
59 override fun getProperties() = synchronized(properties) { properties.toMap() }
61 override fun setProperties(properties: Map<String, String>) {
62 synchronized(this.properties) {
63 this.properties.clear()
64 this.properties.putAll(properties)
68 override fun getProperty(name: String) = synchronized(properties) { properties[name] }
70 override fun setProperty(name: String, value: String): Identity = apply {
71 synchronized(properties) {
72 properties[name] = value
76 override fun removeProperty(name: String): Identity = apply {
77 synchronized(properties) {
82 override fun getTrust(): Map<OwnIdentity, Trust> = synchronized(trustCache) {
86 override fun getTrust(ownIdentity: OwnIdentity) = synchronized(trustCache) {
87 trustCache[ownIdentity]
90 override fun setTrust(ownIdentity: OwnIdentity, trust: Trust) = apply {
91 synchronized(trustCache) {
92 trustCache[ownIdentity] = trust
96 override fun removeTrust(ownIdentity: OwnIdentity) = apply {
97 synchronized(trustCache) {
98 trustCache -= ownIdentity
102 override fun hashCode() = id.hashCode()
104 override fun equals(other: Any?) = if (other !is Identity) {
110 override fun toString() = "${javaClass.simpleName}[id=$id,nickname=$nickname,contexts=$contexts,properties=$properties]"
114 private fun <T> Set<T>.synchronized(): MutableSet<T> = synchronizedSet(this)
115 private fun <K, V> Map<K, V>.synchronized(): MutableMap<K, V> = synchronizedMap(this)