🚧 Try to only allow identities that have no or positive trust
[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.synchronizedMap
21 import java.util.Collections.synchronizedSet
22 import kotlin.collections.set
23
24 /**
25  * A Web of Trust identity.
26  */
27 open class DefaultIdentity(private val id: String, private val nickname: String?, private val requestUri: String) : Identity {
28
29         private val contexts = mutableSetOf<String>().synchronized()
30         private val properties = mutableMapOf<String, String>().synchronized()
31         private val trustCache = mutableMapOf<OwnIdentity, Trust>().synchronized()
32
33         override fun getId() = id
34         override fun getNickname() = nickname
35         override fun getRequestUri() = requestUri
36         override fun getContexts() = synchronized(contexts) { contexts.toSet() }
37
38         override fun hasContext(context: String) = context in contexts
39
40         override fun setContexts(contexts: Set<String>) {
41                 synchronized(this.contexts) {
42                         this.contexts.clear()
43                         this.contexts.addAll(contexts)
44                 }
45         }
46
47         override fun addContext(context: String): Identity = apply {
48                 synchronized(this.contexts) {
49                         contexts += context
50                 }
51         }
52
53         override fun removeContext(context: String): Identity = apply {
54                 synchronized(this.contexts) {
55                         contexts -= context
56                 }
57         }
58
59         override fun getProperties() = synchronized(properties) { properties.toMap() }
60
61         override fun setProperties(properties: Map<String, String>) {
62                 synchronized(this.properties) {
63                         this.properties.clear()
64                         this.properties.putAll(properties)
65                 }
66         }
67
68         override fun getProperty(name: String) = synchronized(properties) { properties[name] }
69
70         override fun setProperty(name: String, value: String): Identity = apply {
71                 synchronized(properties) {
72                         properties[name] = value
73                 }
74         }
75
76         override fun removeProperty(name: String): Identity = apply {
77                 synchronized(properties) {
78                         properties -= name
79                 }
80         }
81
82         override fun getTrust(): Map<OwnIdentity, Trust> = synchronized(trustCache) {
83                 trustCache.toMap()
84         }
85
86         override fun getTrust(ownIdentity: OwnIdentity) = synchronized(trustCache) {
87                 trustCache[ownIdentity]
88         }
89
90         override fun setTrust(ownIdentity: OwnIdentity, trust: Trust) = apply {
91                 synchronized(trustCache) {
92                         trustCache[ownIdentity] = trust
93                 }
94         }
95
96         override fun removeTrust(ownIdentity: OwnIdentity) = apply {
97                 synchronized(trustCache) {
98                         trustCache -= ownIdentity
99                 }
100         }
101
102         override fun hashCode() = id.hashCode()
103
104         override fun equals(other: Any?) = if (other !is Identity) {
105                 false
106         } else {
107                 other.id == getId()
108         }
109
110         override fun toString() = "${javaClass.simpleName}[id=$id,nickname=$nickname,contexts=$contexts,properties=$properties]"
111
112 }
113
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)