Skip to content

Commit a537676

Browse files
committed
kdoc see ref and fixed hackDelegates load json
1 parent 2ac9b91 commit a537676

20 files changed

+94
-42
lines changed

common/src/main/kotlin/com/lambda/config/AbstractSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import kotlin.reflect.KProperty
7272
*/
7373
abstract class AbstractSetting<T : Any>(
7474
private val defaultValue: T,
75-
private val type: Type,
75+
protected val type: Type,
7676
val description: String,
7777
val visibility: () -> Boolean,
7878
) : Jsonable, Nameable {

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ abstract class Configurable(
278278
/**
279279
* Creates a [ByteSetting] with the provided parameters and adds it to the [settings].
280280
*
281+
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
282+
*
281283
* @param name The unique identifier for the setting.
282284
* @param defaultValue The default [Byte] value of the setting.
283285
* @param range The range within which the setting's value must fall.
@@ -303,6 +305,8 @@ abstract class Configurable(
303305
/**
304306
* Creates a [DoubleSetting] with the provided parameters and adds it to the [settings].
305307
*
308+
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
309+
*
306310
* @param name The unique identifier for the setting.
307311
* @param defaultValue The default [Double] value of the setting.
308312
* @param range The range within which the setting's value must fall.
@@ -328,6 +332,8 @@ abstract class Configurable(
328332
/**
329333
* Creates a [FloatSetting] with the provided parameters and adds it to the [settings].
330334
*
335+
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
336+
*
331337
* @param name The unique identifier for the setting.
332338
* @param defaultValue The default [Float] value of the setting.
333339
* @param range The range within which the setting's value must fall.
@@ -353,6 +359,8 @@ abstract class Configurable(
353359
/**
354360
* Creates an [IntegerSetting] with the provided parameters and adds it to the [settings].
355361
*
362+
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
363+
*
356364
* @param name The unique identifier for the setting.
357365
* @param defaultValue The default [Int] value of the setting.
358366
* @param range The range within which the setting's value must fall.
@@ -378,6 +386,8 @@ abstract class Configurable(
378386
/**
379387
* Creates a [LongSetting] with the provided parameters and adds it to the [settings].
380388
*
389+
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
390+
*
381391
* @param name The unique identifier for the setting.
382392
* @param defaultValue The default [Long] value of the setting.
383393
* @param range The range within which the setting's value must fall.
@@ -403,6 +413,8 @@ abstract class Configurable(
403413
/**
404414
* Creates a [ShortSetting] with the provided parameters and adds it to the [settings].
405415
*
416+
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
417+
*
406418
* @param name The unique identifier for the setting.
407419
* @param defaultValue The default [Short] value of the setting.
408420
* @param range The range within which the setting's value must fall.

common/src/main/kotlin/com/lambda/config/settings/CharSetting.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ import com.google.gson.reflect.TypeToken
2121
import com.lambda.config.AbstractSetting
2222

2323
/**
24-
* Represents a [Char] setting.
25-
*
26-
* @property name The [name] of the setting.
27-
* @property defaultValue The default [Char] [value] of the setting.
28-
* @property visibility A function that determines whether the setting [isVisible].
29-
* @property description A [description] of the setting.
24+
* @see [com.lambda.config.Configurable]
3025
*/
3126
class CharSetting(
3227
override val name: String,

common/src/main/kotlin/com/lambda/config/settings/NumericSetting.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@ import java.util.*
2424
import kotlin.reflect.KProperty
2525

2626
/**
27-
* Represents a [NumericSetting] with a specific [range] and [step].
28-
*
29-
* The [value] of the setting is coerced into the specified [range] and rounded to the nearest [step].
30-
* The [visibility] and [description] of the setting are inherited from [AbstractSetting].
31-
*
32-
* @property range The range within which the setting's [value] must fall.
33-
* @property step The [step] to which the setting's [value] is rounded.
34-
* @property visibility A function that determines whether the setting [isVisible].
35-
* @property description A [description] of the setting.
36-
* @property unit The unit of the setting's [value].
27+
* @see [com.lambda.config.Configurable]
3728
*/
3829
abstract class NumericSetting<T>(
3930
value: T,

common/src/main/kotlin/com/lambda/config/settings/StringSetting.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ import com.google.gson.reflect.TypeToken
2121
import com.lambda.config.AbstractSetting
2222

2323
/**
24-
* Represents a [String] setting.
25-
*
26-
* @property name The [name] of the setting.
27-
* @property defaultValue The default [String] [value] of the setting.
28-
* @property description A [description] of the setting.
29-
* @property visibility A function that determines whether the setting [isVisible].
24+
* @see [com.lambda.config.Configurable]
3025
*/
3126
class StringSetting(
3227
override val name: String,

common/src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
package com.lambda.config.settings.collections
1919

2020
import com.google.gson.JsonElement
21+
import com.lambda.Lambda.gson
2122
import com.lambda.config.AbstractSetting
2223
import java.lang.reflect.Type
2324

25+
/**
26+
* @see [com.lambda.config.Configurable]
27+
*/
2428
class ListSetting<T : Any>(
2529
override val name: String,
2630
private val defaultValue: MutableList<T>,
@@ -35,7 +39,15 @@ class ListSetting<T : Any>(
3539
visibility
3640
) {
3741
override fun toJson(): JsonElement {
38-
if (hackDelegates) value = defaultValue
39-
return super.toJson()
42+
return if (hackDelegates) gson.toJsonTree(defaultValue, type)
43+
else super.toJson()
44+
}
45+
46+
override fun loadFromJson(serialized: JsonElement) {
47+
if (hackDelegates) {
48+
defaultValue.addAll(gson.fromJson(serialized, type))
49+
setValue(this, ::value, defaultValue.distinct().toMutableList())
50+
}
51+
else super.loadFromJson(serialized)
4052
}
4153
}

common/src/main/kotlin/com/lambda/config/settings/collections/MapSetting.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,36 @@
1818
package com.lambda.config.settings.collections
1919

2020
import com.google.gson.JsonElement
21+
import com.lambda.Lambda.gson
2122
import com.lambda.config.AbstractSetting
2223
import java.lang.reflect.Type
2324

25+
/**
26+
* @see [com.lambda.config.Configurable]
27+
*/
2428
class MapSetting<K, V>(
2529
override val name: String,
26-
private val defaultValue: Map<K, V>,
30+
private val defaultValue: MutableMap<K, V>,
2731
type: Type,
2832
description: String,
2933
private val hackDelegates: Boolean,
3034
visibility: () -> Boolean,
31-
) : AbstractSetting<Map<K, V>>(
35+
) : AbstractSetting<MutableMap<K, V>>(
3236
defaultValue,
3337
type,
3438
description,
3539
visibility
3640
) {
3741
override fun toJson(): JsonElement {
38-
if (hackDelegates) value = defaultValue
39-
return super.toJson()
42+
return if (hackDelegates) gson.toJsonTree(defaultValue, type)
43+
else super.toJson()
44+
}
45+
46+
override fun loadFromJson(serialized: JsonElement) {
47+
if (hackDelegates) {
48+
defaultValue.putAll(gson.fromJson(serialized, type))
49+
setValue(this, ::value, defaultValue)
50+
}
51+
else super.loadFromJson(serialized)
4052
}
4153
}

common/src/main/kotlin/com/lambda/config/settings/collections/SetSetting.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
package com.lambda.config.settings.collections
1919

2020
import com.google.gson.JsonElement
21+
import com.lambda.Lambda.gson
2122
import com.lambda.config.AbstractSetting
2223
import java.lang.reflect.Type
2324

25+
/**
26+
* @see [com.lambda.config.Configurable]
27+
*/
2428
class SetSetting<T : Any>(
2529
override val name: String,
2630
private val defaultValue: MutableSet<T>,
@@ -35,7 +39,15 @@ class SetSetting<T : Any>(
3539
visibility
3640
) {
3741
override fun toJson(): JsonElement {
38-
if (hackDelegates) value = defaultValue
39-
return super.toJson()
42+
return if (hackDelegates) gson.toJsonTree(defaultValue, type)
43+
else super.toJson()
44+
}
45+
46+
override fun loadFromJson(serialized: JsonElement) {
47+
if (hackDelegates) {
48+
defaultValue.addAll(gson.fromJson(serialized, type))
49+
setValue(this, ::value, defaultValue.distinct().toMutableSet())
50+
}
51+
else super.loadFromJson(serialized)
4052
}
4153
}

common/src/main/kotlin/com/lambda/config/settings/comparable/BooleanSetting.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package com.lambda.config.settings.comparable
2020
import com.google.gson.reflect.TypeToken
2121
import com.lambda.config.AbstractSetting
2222

23+
/**
24+
* @see [com.lambda.config.Configurable]
25+
*/
2326
class BooleanSetting(
2427
override val name: String,
2528
defaultValue: Boolean,

common/src/main/kotlin/com/lambda/config/settings/comparable/EnumSetting.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package com.lambda.config.settings.comparable
2020
import com.google.gson.reflect.TypeToken
2121
import com.lambda.config.AbstractSetting
2222

23+
/**
24+
* @see [com.lambda.config.Configurable]
25+
*/
2326
class EnumSetting<T : Enum<T>>(
2427
override val name: String,
2528
defaultValue: T,

0 commit comments

Comments
 (0)