This repository was archived by the owner on Dec 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathAbstractSetting.kt
More file actions
54 lines (40 loc) · 1.55 KB
/
AbstractSetting.kt
File metadata and controls
54 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.lambda.client.setting.settings
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.lambda.client.commons.interfaces.Nameable
import kotlin.reflect.KProperty
abstract class AbstractSetting<T : Any> : Nameable {
abstract val value: T
abstract val defaultValue: T
abstract val valueClass: Class<T>
abstract val visibility: () -> Boolean
abstract val description: String
abstract val unit: String
abstract val formatter: (T) -> String
val listeners = ArrayList<() -> Unit>()
val valueListeners = ArrayList<(prev: T, input: T) -> Unit>()
val isVisible get() = visibility()
val isModified get() = this.value != this.defaultValue
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
open fun setValue(valueIn: String) {
read(parser.parse(valueIn))
}
abstract fun resetValue()
abstract fun write(): JsonElement
abstract fun read(jsonElement: JsonElement?)
override fun toString() = "${formatter(value)}$unit"
override fun equals(other: Any?) = this === other
|| (other is AbstractSetting<*>
&& this.valueClass == other.valueClass
&& this.name == other.name
&& this.value == other.value)
override fun hashCode() = valueClass.hashCode() * 31 +
name.hashCode() * 31 +
value.hashCode()
protected companion object {
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
val parser = JsonParser()
}
}