Commit d4321152 authored by jiahua.liu's avatar jiahua.liu

Thread safety

parent 425de4ae
...@@ -284,15 +284,8 @@ open class ConfigSectionImpl() : ConcurrentHashMap<String, Any>(), ConfigSection ...@@ -284,15 +284,8 @@ open class ConfigSectionImpl() : ConcurrentHashMap<String, Any>(), ConfigSection
} }
} }
interface FileConfig : Config {
fun deserialize(content: String): ConfigSection
fun serialize(config: ConfigSection): String
}
open class ConfigSectionDelegation( open class ConfigSectionDelegation(
val delegation: MutableMap<String, Any> private val delegation: MutableMap<String, Any>
) : ConfigSection, MutableMap<String, Any> by delegation { ) : ConfigSection, MutableMap<String, Any> by delegation {
override fun set(key: String, value: Any) { override fun set(key: String, value: Any) {
delegation.put(key, value) delegation.put(key, value)
...@@ -307,6 +300,14 @@ open class ConfigSectionDelegation( ...@@ -307,6 +300,14 @@ open class ConfigSectionDelegation(
} }
} }
interface FileConfig : Config {
fun deserialize(content: String): ConfigSection
fun serialize(config: ConfigSection): String
}
abstract class FileConfigImpl internal constructor( abstract class FileConfigImpl internal constructor(
private val file: File private val file: File
) : FileConfig, ConfigSection { ) : FileConfig, ConfigSection {
...@@ -315,18 +316,10 @@ abstract class FileConfigImpl internal constructor( ...@@ -315,18 +316,10 @@ abstract class FileConfigImpl internal constructor(
deserialize(file.readText()) deserialize(file.readText())
} }
override val size: Int override val size: Int get() = content.size
get() = content.size override val entries: MutableSet<MutableMap.MutableEntry<String, Any>> get() = content.entries
override val keys: MutableSet<String> get() = content.keys
override val entries: MutableSet<MutableMap.MutableEntry<String, Any>> override val values: MutableCollection<Any> get() = content.values
get() = content.entries
override val keys: MutableSet<String>
get() = content.keys
override val values: MutableCollection<Any>
get() = content.values
override fun containsKey(key: String): Boolean = content.containsKey(key) override fun containsKey(key: String): Boolean = content.containsKey(key)
override fun containsValue(value: Any): Boolean = content.containsValue(value) override fun containsValue(value: Any): Boolean = content.containsValue(value)
override fun put(key: String, value: Any): Any? = content.put(key, value) override fun put(key: String, value: Any): Any? = content.put(key, value)
...@@ -335,7 +328,6 @@ abstract class FileConfigImpl internal constructor( ...@@ -335,7 +328,6 @@ abstract class FileConfigImpl internal constructor(
override fun clear() = content.clear() override fun clear() = content.clear()
override fun remove(key: String): Any? = content.remove(key) override fun remove(key: String): Any? = content.remove(key)
override fun save() { override fun save() {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile() file.createNewFile()
...@@ -401,7 +393,11 @@ class TomlConfig internal constructor(file: File) : FileConfigImpl(file) { ...@@ -401,7 +393,11 @@ class TomlConfig internal constructor(file: File) : FileConfigImpl(file) {
if (content.isEmpty() || content.isBlank()) { if (content.isEmpty() || content.isBlank()) {
return ConfigSectionImpl() return ConfigSectionImpl()
} }
return ConfigSectionDelegation(Toml().read(content).toMap()) return ConfigSectionDelegation(
Collections.synchronizedMap(
Toml().read(content).toMap()
)
)
} }
override fun serialize(config: ConfigSection): String { override fun serialize(config: ConfigSection): String {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment