Skip to content

Commit 2b8a773

Browse files
committed
getList fixed some bugs.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 677a230 commit 2b8a773

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

src/main/kotlin/com/mairwunnx/projectessentials/core/impl/configurations/GeneralConfiguration.kt

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.mairwunnx.projectessentials.core.impl.configurations
44

55
import com.mairwunnx.projectessentials.core.api.v1.configuration.IConfiguration
6+
import com.mairwunnx.projectessentials.core.api.v1.extensions.empty
67
import com.mairwunnx.projectessentials.core.api.v1.helpers.projectConfigDirectory
78
import org.apache.logging.log4j.LogManager
89
import java.io.File
@@ -43,64 +44,58 @@ object GeneralConfiguration : IConfiguration<Properties> {
4344

4445
fun getDoubleOrDefault(key: String, value: Double): Double {
4546
val property = properties[key]
46-
if (property == null) {
47-
put(key, value.toString())
48-
}
47+
if (property == null) put(key, value.toString())
4948
return property?.toString()?.toDouble() ?: value
5049
}
5150

5251
fun getDouble(key: String) = properties[key].toString().toDouble()
5352

5453
fun getFloatOrDefault(key: String, value: Float): Float {
5554
val property = properties[key]
56-
if (property == null) {
57-
put(key, value.toString())
58-
}
55+
if (property == null) put(key, value.toString())
5956
return property?.toString()?.toFloat() ?: value
6057
}
6158

6259
fun getFloatInt(key: String) = properties[key].toString().toFloat()
6360

6461
fun getIntOrDefault(key: String, value: Int): Int {
6562
val property = properties[key]
66-
if (property == null) {
67-
put(key, value.toString())
68-
}
63+
if (property == null) put(key, value.toString())
6964
return property?.toString()?.toInt() ?: value
7065
}
7166

7267
fun getInt(key: String) = properties[key].toString().toInt()
7368

7469
fun getBoolOrDefault(key: String, value: Boolean): Boolean {
7570
val property = properties[key]
76-
if (property == null) {
77-
put(key, value.toString())
78-
}
71+
if (property == null) put(key, value.toString())
7972
return property?.toString()?.toBoolean() ?: value
8073
}
8174

8275
fun getBool(key: String) = properties[key].toString().toBoolean()
8376

8477
fun getStringOrDefault(key: String, value: String): String {
8578
val property = properties[key]
86-
if (property == null) {
87-
put(key, value)
88-
}
79+
if (property == null) put(key, value)
8980
return property?.toString() ?: value
9081
}
9182

9283
fun getString(key: String) = properties[key].toString()
9384

9485
fun getList(
95-
key: String,
96-
defaultValue: ArrayList<String> = arrayListOf()
86+
key: String, defaultValue: ArrayList<String> = arrayListOf()
9787
): List<String> {
98-
val array = getStringOrDefault(key, "")
99-
if (array.isEmpty()) {
100-
putList(key, defaultValue)
101-
return defaultValue
88+
val rawList = getStringOrDefault(key, String.empty)
89+
if (rawList.isBlank()) return defaultValue.also { putList(key, defaultValue) }
90+
return rawList.trim('[', ']').replace("\"", "").let {
91+
val list = mutableListOf<String>()
92+
if (it.contains(',') && it.count() > 3) {
93+
it.split(',').forEach { value -> list.add(value.trim()) }
94+
} else if (it.count() >= 1) {
95+
list.add(it.trim())
96+
}
97+
return@let list
10298
}
103-
return array.trim('[', ']').replace("\"", "").split(',').map { it.trim() }
10499
}
105100

106101
fun putList(key: String, value: List<String>) = properties.set(key, value.toString())

0 commit comments

Comments
 (0)