Skip to content

Commit

Permalink
fix: obtainBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan authored and WhiredPlanck committed Jan 23, 2024
1 parent d097e87 commit c998c88
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions app/src/main/java/com/osfans/trime/util/CollectionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ object CollectionUtils {
fun <K, V> getOrDefault(
map: Map<K, V>,
key: K,
defaultValue: V,
): V = map[key] ?: defaultValue
defValue: V,
): V = map[key] ?: defValue

@Suppress("UNCHECKED_CAST")
@JvmStatic
fun obtainValue(
map: Map<String, Any?>?,
vararg: String?,
): Any? {
if (map.isNullOrEmpty() || vararg == null || !map.containsKey(vararg)) return null
if (map.isNullOrEmpty() || vararg.isNullOrEmpty()) return null
val keys = vararg.split('/')
var v: Any? = map
for (key in keys) {
Expand All @@ -34,7 +34,7 @@ object CollectionUtils {
key: String,
defValue: String = "",
): String {
if (map.isNullOrEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue
if (map.isNullOrEmpty() || key.isEmpty()) return defValue
val v = obtainValue(map, key)
return v?.toString() ?: defValue
}
Expand All @@ -45,10 +45,11 @@ object CollectionUtils {
key: String,
defValue: Int = 0,
): Int {
if (map.isNullOrEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue
if (map.isNullOrEmpty() || key.isEmpty()) return defValue
val nm = obtainString(map, key)
return runCatching { if (nm.isNotEmpty()) java.lang.Long.decode(nm).toInt() else defValue }
.getOrDefault(defValue)
return runCatching {
if (nm.isNotEmpty()) java.lang.Long.decode(nm).toInt() else defValue
}.getOrDefault(defValue)
}

@JvmStatic
Expand All @@ -57,9 +58,11 @@ object CollectionUtils {
key: String,
defValue: Float = 0f,
): Float {
if (map.isNullOrEmpty() || key.isEmpty() || !map.containsKey(key)) return defValue
if (map.isNullOrEmpty() || key.isEmpty()) return defValue
val s = obtainString(map, key)
return runCatching { if (s.isNotEmpty()) s.toFloat() else defValue }.getOrDefault(defValue)
return runCatching {
if (s.isNotEmpty()) s.toFloat() else defValue
}.getOrDefault(defValue)
}

@JvmStatic
Expand All @@ -68,10 +71,8 @@ object CollectionUtils {
key: String,
defValue: Boolean = false,
): Boolean {
return if (map.isNullOrEmpty() || key.isEmpty() || !map.containsKey(key)) {
defValue
} else {
obtainString(map, key).toBoolean()
}
if (map.isNullOrEmpty() || key.isEmpty()) return defValue
val s = obtainString(map, key)
return if (s.isNotEmpty()) s.toBoolean() else defValue
}
}

0 comments on commit c998c88

Please sign in to comment.