2 * Sone - DefaultIdentity.kt - Copyright © 2010–2019 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.*
23 * A Web of Trust identity.
25 open class DefaultIdentity(private val id: String, private val nickname: String?, private val requestUri: String) : Identity {
27 private val contexts = mutableSetOf<String>().synchronized()
28 private val properties = mutableMapOf<String, String>().synchronized()
29 private val trustCache = mutableMapOf<OwnIdentity, Trust>().synchronized()
31 override fun getId() = id
32 override fun getNickname() = nickname
33 override fun getRequestUri() = requestUri
34 override fun getContexts() = synchronized(contexts) { contexts.toSet() }
36 override fun hasContext(context: String) = context in contexts
38 override fun setContexts(contexts: Set<String>) {
39 synchronized(this.contexts) {
41 this.contexts.addAll(contexts)
45 override fun addContext(context: String): Identity = apply {
46 synchronized(this.contexts) {
51 override fun removeContext(context: String): Identity = apply {
52 synchronized(this.contexts) {
57 override fun getProperties() = synchronized(properties) { properties.toMap() }
59 override fun setProperties(properties: Map<String, String>) {
60 synchronized(this.properties) {
61 this.properties.clear()
62 this.properties.putAll(properties)
66 override fun getProperty(name: String) = synchronized(properties) { properties[name] }
68 override fun setProperty(name: String, value: String): Identity = apply {
69 synchronized(properties) {
70 properties[name] = value
74 override fun removeProperty(name: String): Identity = apply {
75 synchronized(properties) {
80 override fun getTrust(ownIdentity: OwnIdentity) = synchronized(trustCache) {
81 trustCache[ownIdentity]
84 override fun setTrust(ownIdentity: OwnIdentity, trust: Trust) = apply {
85 synchronized(trustCache) {
86 trustCache[ownIdentity] = trust
90 override fun removeTrust(ownIdentity: OwnIdentity) = apply {
91 synchronized(trustCache) {
92 trustCache -= ownIdentity
96 override fun hashCode() = id.hashCode()
98 override fun equals(other: Any?) = if (other !is Identity) {
104 override fun toString() = "${javaClass.simpleName}[id=$id,nickname=$nickname,contexts=$contexts,properties=$properties]"
108 private fun <T> Set<T>.synchronized(): MutableSet<T> = synchronizedSet(this)
109 private fun <K, V> Map<K, V>.synchronized(): MutableMap<K, V> = synchronizedMap(this)