mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 19:44:05 +09:00
com.torvald → net.torvald
Former-commit-id: 375604da8a20a6ba7cd0a8d05a44add02b2d04f4 Former-commit-id: 287287c5920b07618174d7a7573f049d350ded66
This commit is contained in:
97
src/net/torvald/terrarum/KVHashMap.kt
Normal file
97
src/net/torvald/terrarum/KVHashMap.kt
Normal file
@@ -0,0 +1,97 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
import com.google.gson.JsonPrimitive
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-19.
|
||||
*/
|
||||
open class KVHashMap {
|
||||
|
||||
private val hashMap: HashMap<String, Any>
|
||||
|
||||
init {
|
||||
hashMap = HashMap<String, Any>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Add key-value pair to the configuration table.
|
||||
* If key does not exist on the table, new key will be generated.
|
||||
* If key already exists, the value will be overwritten.
|
||||
|
||||
* @param key case insensitive
|
||||
* *
|
||||
* @param value
|
||||
*/
|
||||
operator fun set(key: String, value: Any) {
|
||||
hashMap.put(key.toLowerCase(), value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value using key from configuration table.
|
||||
|
||||
* @param key case insensitive
|
||||
* *
|
||||
* @return Object value
|
||||
*/
|
||||
operator fun get(key: String): Any? {
|
||||
return hashMap[key.toLowerCase()]
|
||||
}
|
||||
|
||||
fun getAsInt(key: String): Int? {
|
||||
val value = get(key)
|
||||
|
||||
if (value is JsonPrimitive)
|
||||
return value.asInt
|
||||
|
||||
return get(key) as Int?
|
||||
}
|
||||
|
||||
fun getAsFloat(key: String): Float? {
|
||||
val value = get(key)
|
||||
|
||||
if (value is Int)
|
||||
return value.toFloat()
|
||||
else if (value is JsonPrimitive)
|
||||
return value.asFloat
|
||||
|
||||
return value as Float?
|
||||
}
|
||||
|
||||
fun getAsDouble(key: String): Double? {
|
||||
val value = get(key)
|
||||
|
||||
if (value is Int)
|
||||
return value.toDouble()
|
||||
else if (value is JsonPrimitive)
|
||||
return value.asDouble
|
||||
|
||||
return value as Double?
|
||||
}
|
||||
|
||||
fun getAsString(key: String): String? {
|
||||
val value = get(key)
|
||||
|
||||
if (value is JsonPrimitive)
|
||||
return value.asString
|
||||
|
||||
return value as String?
|
||||
}
|
||||
|
||||
fun getAsBoolean(key: String): Boolean? {
|
||||
val value = get(key)
|
||||
|
||||
if (value is JsonPrimitive)
|
||||
return value.asBoolean
|
||||
|
||||
return value as Boolean?
|
||||
}
|
||||
|
||||
fun hasKey(key: String): Boolean {
|
||||
return hashMap.containsKey(key)
|
||||
}
|
||||
|
||||
val keySet: Set<Any>
|
||||
get() = hashMap.keys
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user