[minor] Refactor properties collection builder:
add a helper to replace the value only if it is absent or default rearrange some functions
This commit is contained in:
+14
-8
@@ -89,12 +89,26 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> PropertiesCollection.Key<T>.replaceOnlyDefault(v: T?) {
|
||||
if (!data.containsKey(this) || data[this] == this.getDefaultValue(PropertiesCollection(data))) {
|
||||
data[this] = v
|
||||
}
|
||||
}
|
||||
|
||||
// generic for lists
|
||||
|
||||
fun <T> PropertiesCollection.Key<in List<T>>.putIfAny(vals: Iterable<T>?) {
|
||||
if (vals?.any() == true) {
|
||||
data[this] = if (vals is List) vals else vals.toList()
|
||||
}
|
||||
}
|
||||
|
||||
operator fun <T> PropertiesCollection.Key<in List<T>>.invoke(vararg vals: T) {
|
||||
append(vals.asIterable())
|
||||
}
|
||||
|
||||
// generic for maps:
|
||||
|
||||
@JvmName("putIfAny_map")
|
||||
fun <K, V> PropertiesCollection.Key<in Map<K, V>>.putIfAny(vals: Iterable<Pair<K, V>>?) {
|
||||
if (vals?.any() == true) {
|
||||
@@ -108,14 +122,6 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
|
||||
}
|
||||
}
|
||||
|
||||
// generic for lists
|
||||
|
||||
operator fun <T> PropertiesCollection.Key<in List<T>>.invoke(vararg vals: T) {
|
||||
append(vals.asIterable())
|
||||
}
|
||||
|
||||
// generic for maps:
|
||||
|
||||
operator fun <K, V> PropertiesCollection.Key<Map<K, V>>.invoke(vararg vs: Pair<K, V>) {
|
||||
append(vs.asIterable())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user