|
3 | 3 | package com.mairwunnx.projectessentials.core.impl.configurations |
4 | 4 |
|
5 | 5 | import com.mairwunnx.projectessentials.core.api.v1.configuration.IConfiguration |
| 6 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.empty |
6 | 7 | import com.mairwunnx.projectessentials.core.api.v1.helpers.projectConfigDirectory |
7 | 8 | import org.apache.logging.log4j.LogManager |
8 | 9 | import java.io.File |
@@ -43,64 +44,58 @@ object GeneralConfiguration : IConfiguration<Properties> { |
43 | 44 |
|
44 | 45 | fun getDoubleOrDefault(key: String, value: Double): Double { |
45 | 46 | val property = properties[key] |
46 | | - if (property == null) { |
47 | | - put(key, value.toString()) |
48 | | - } |
| 47 | + if (property == null) put(key, value.toString()) |
49 | 48 | return property?.toString()?.toDouble() ?: value |
50 | 49 | } |
51 | 50 |
|
52 | 51 | fun getDouble(key: String) = properties[key].toString().toDouble() |
53 | 52 |
|
54 | 53 | fun getFloatOrDefault(key: String, value: Float): Float { |
55 | 54 | val property = properties[key] |
56 | | - if (property == null) { |
57 | | - put(key, value.toString()) |
58 | | - } |
| 55 | + if (property == null) put(key, value.toString()) |
59 | 56 | return property?.toString()?.toFloat() ?: value |
60 | 57 | } |
61 | 58 |
|
62 | 59 | fun getFloatInt(key: String) = properties[key].toString().toFloat() |
63 | 60 |
|
64 | 61 | fun getIntOrDefault(key: String, value: Int): Int { |
65 | 62 | val property = properties[key] |
66 | | - if (property == null) { |
67 | | - put(key, value.toString()) |
68 | | - } |
| 63 | + if (property == null) put(key, value.toString()) |
69 | 64 | return property?.toString()?.toInt() ?: value |
70 | 65 | } |
71 | 66 |
|
72 | 67 | fun getInt(key: String) = properties[key].toString().toInt() |
73 | 68 |
|
74 | 69 | fun getBoolOrDefault(key: String, value: Boolean): Boolean { |
75 | 70 | val property = properties[key] |
76 | | - if (property == null) { |
77 | | - put(key, value.toString()) |
78 | | - } |
| 71 | + if (property == null) put(key, value.toString()) |
79 | 72 | return property?.toString()?.toBoolean() ?: value |
80 | 73 | } |
81 | 74 |
|
82 | 75 | fun getBool(key: String) = properties[key].toString().toBoolean() |
83 | 76 |
|
84 | 77 | fun getStringOrDefault(key: String, value: String): String { |
85 | 78 | val property = properties[key] |
86 | | - if (property == null) { |
87 | | - put(key, value) |
88 | | - } |
| 79 | + if (property == null) put(key, value) |
89 | 80 | return property?.toString() ?: value |
90 | 81 | } |
91 | 82 |
|
92 | 83 | fun getString(key: String) = properties[key].toString() |
93 | 84 |
|
94 | 85 | fun getList( |
95 | | - key: String, |
96 | | - defaultValue: ArrayList<String> = arrayListOf() |
| 86 | + key: String, defaultValue: ArrayList<String> = arrayListOf() |
97 | 87 | ): 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 |
102 | 98 | } |
103 | | - return array.trim('[', ']').replace("\"", "").split(',').map { it.trim() } |
104 | 99 | } |
105 | 100 |
|
106 | 101 | fun putList(key: String, value: List<String>) = properties.set(key, value.toString()) |
|
0 commit comments