/*
* 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)