🔖 Set version to 81
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / DefaultIdentity.kt
1 /*
2  * Sone - DefaultIdentity.kt - Copyright Â© 2010–2020 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
20 import java.util.Collections.*
21
22 /**
23  * A Web of Trust identity.
24  */
25 open class DefaultIdentity(private val id: String, private val nickname: String?, private val requestUri: String) : Identity {
26
27         private val contexts = mutableSetOf<String>().synchronized()
28         private val properties = mutableMapOf<String, String>().synchronized()
29         private val trustCache = mutableMapOf<OwnIdentity, Trust>().synchronized()
30
31         override fun getId() = id
32         override fun getNickname() = nickname
33         override fun getRequestUri() = requestUri
34         override fun getContexts() = synchronized(contexts) { contexts.toSet() }
35
36         override fun hasContext(context: String) = context in contexts
37
38         override fun setContexts(contexts: Set<String>) {
39                 synchronized(this.contexts) {
40                         this.contexts.clear()
41                         this.contexts.addAll(contexts)
42                 }
43         }
44
45         override fun addContext(context: String): Identity = apply {
46                 synchronized(this.contexts) {
47                         contexts += context
48                 }
49         }
50
51         override fun removeContext(context: String): Identity = apply {
52                 synchronized(this.contexts) {
53                         contexts -= context
54                 }
55         }
56
57         override fun getProperties() = synchronized(properties) { properties.toMap() }
58
59         override fun setProperties(properties: Map<String, String>) {
60                 synchronized(this.properties) {
61                         this.properties.clear()
62                         this.properties.putAll(properties)
63                 }
64         }
65
66         override fun getProperty(name: String) = synchronized(properties) { properties[name] }
67
68         override fun setProperty(name: String, value: String): Identity = apply {
69                 synchronized(properties) {
70                         properties[name] = value
71                 }
72         }
73
74         override fun removeProperty(name: String): Identity = apply {
75                 synchronized(properties) {
76                         properties -= name
77                 }
78         }
79
80         override fun getTrust(ownIdentity: OwnIdentity) = synchronized(trustCache) {
81                 trustCache[ownIdentity]
82         }
83
84         override fun setTrust(ownIdentity: OwnIdentity, trust: Trust) = apply {
85                 synchronized(trustCache) {
86                         trustCache[ownIdentity] = trust
87                 }
88         }
89
90         override fun removeTrust(ownIdentity: OwnIdentity) = apply {
91                 synchronized(trustCache) {
92                         trustCache -= ownIdentity
93                 }
94         }
95
96         override fun hashCode() = id.hashCode()
97
98         override fun equals(other: Any?) = if (other !is Identity) {
99                 false
100         } else {
101                 other.id == getId()
102         }
103
104         override fun toString() = "${javaClass.simpleName}[id=$id,nickname=$nickname,contexts=$contexts,properties=$properties]"
105
106 }
107
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)