Skip to content

Commit 0cb8802

Browse files
committed
refactor: make ConfigMap/ConfigList implement collection interfaces
1 parent d2e23e0 commit 0cb8802

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

app/src/main/java/com/osfans/trime/util/config/ConfigTypes.kt

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,66 @@ class ConfigValue(private val scalar: YamlScalar) : ConfigItem(scalar) {
7474
}
7575

7676
/** The wrapper of [YamlList] */
77-
class ConfigList(private val list: YamlList) : ConfigItem(list) {
77+
class ConfigList(private val list: YamlList) : ConfigItem(list), List<ConfigItem?> {
7878
constructor(item: ConfigItem) : this(item.node.yamlList)
7979

80-
val items get() = list.items.map { convertFromYaml(it) }
80+
private val items = list.items.map { convertFromYaml(it) }
8181

82-
operator fun iterator() = items.iterator()
82+
override val size = list.items.size
83+
84+
override fun containsAll(elements: Collection<ConfigItem?>): Boolean = items.containsAll(elements)
85+
86+
override fun contains(element: ConfigItem?): Boolean = items.contains(element)
87+
88+
override operator fun iterator() = items.iterator()
89+
90+
override fun listIterator(): ListIterator<ConfigItem?> = items.listIterator()
91+
92+
override fun listIterator(index: Int): ListIterator<ConfigItem?> = items.listIterator(index)
93+
94+
override fun subList(
95+
fromIndex: Int,
96+
toIndex: Int,
97+
): List<ConfigItem?> = items.subList(fromIndex, toIndex)
98+
99+
override fun lastIndexOf(element: ConfigItem?): Int = items.lastIndexOf(element)
83100

84101
override fun isEmpty() = list.items.isEmpty()
85102

86103
override fun contentToString(): String = list.contentToString()
87104

88-
operator fun get(index: Int) = items[index]
105+
override operator fun get(index: Int): ConfigItem? = items[index]
106+
107+
override fun indexOf(element: ConfigItem?): Int = items.indexOf(element)
89108

90109
fun getValue(index: Int) = get(index)?.configValue
91110
}
92111

93-
class ConfigMap(private val map: YamlMap) : ConfigItem(map) {
112+
class ConfigMap(private val map: YamlMap) : ConfigItem(map), Map<String, ConfigItem?> {
94113
constructor(item: ConfigItem) : this(item.node.yamlMap)
95114

96115
override fun isEmpty() = map.entries.isEmpty()
97116

117+
override val size: Int = map.entries.size
118+
98119
override fun contentToString(): String = map.contentToString()
99120

100-
fun containsKey(key: String) = map.getKey(key) != null
121+
override fun containsKey(key: String) = map.getKey(key) != null
122+
123+
override fun containsValue(value: ConfigItem?): Boolean = entries.any { it.value == value }
101124

102-
val entries get() =
125+
override val entries: Set<Map.Entry<String, ConfigItem?>> =
103126
map.entries.entries.associate { (s, n) ->
104127
s.content to convertFromYaml(n)
105-
}
128+
}.entries
129+
130+
override val keys: Set<String> = entries.map { it.key }.toSet()
131+
132+
override val values: Collection<ConfigItem?> = entries.map { it.value }
106133

107134
operator fun iterator() = entries.iterator()
108135

109-
@Suppress("UNCHECKED_CAST")
110-
operator fun <T : ConfigItem> get(key: String): T? = entries.entries.firstOrNull { it.key == key }?.value as T?
136+
override operator fun get(key: String): ConfigItem? = entries.firstOrNull { it.key == key }?.value
111137

112-
fun getValue(key: String): ConfigValue? = get<ConfigValue>(key)?.configValue
138+
fun getValue(key: String): ConfigValue? = get(key)?.configValue
113139
}

0 commit comments

Comments
 (0)