diff --git a/build.gradle.kts b/build.gradle.kts index c060fcc..c0b0071 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,7 +37,7 @@ extra.apply { } group = "io.github.realyusufismail" -version = "1.0.0" +version = "1.0.1" repositories { mavenCentral() diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt index 8f19e6b..fa4f4c7 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt @@ -22,116 +22,258 @@ import com.fasterxml.jackson.databind.node.* import io.github.realyusufismail.jconfig.JConfig.Companion.builder /** A utility class used to get values from the config file. */ -object JConfigUtils { - private val jConfig = builder().build() - - /** - * Gets the value of the specified key. - * - * @param key The key to get the value of. - * @return The value of the specified key. - */ - fun getString(key: String): String { - return jConfig[key] as String - } +class JConfigUtils { + companion object { + private val jConfig = builder().build() - /** - * Gets the value as a String. If the value is not found, the default value will be returned. - * - * @param key The key to get the value from. - * @param defaultValue The default value to return if the value is not found. - * - * @return The value as a String. - */ - fun getString(key: String, defaultValue: String): String { - return jConfig[key, defaultValue] as String - } + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + fun getString(key: String): String { + return jConfig[key] as String + } - fun getInt(key: String): Int { - return if (jConfig[key] is IntNode) { - (jConfig[key] as IntNode).asInt() - } else { - throw JConfigException("The value at the key $key is not an integer.") + /** + * Gets the value as a String. If the value is not found, the default value will be + * returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a String. + */ + fun getString(key: String, defaultValue: String): String { + return jConfig[key, defaultValue] as String } - } - fun getInt(key: String, defaultValue: Int): Int { - return if (jConfig[key] is IntNode) { - (jConfig[key] as IntNode).intValue() - } else { - defaultValue + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + fun getInt(key: String): Int { + return if (jConfig[key] is IntNode) { + (jConfig[key] as IntNode).asInt() + } else { + throw JConfigException("The value at the key $key is not an integer.") + } } - } - fun getBoolean(key: String): Boolean { - return if (jConfig[key] is BooleanNode) { - (jConfig[key] as BooleanNode).booleanValue() - } else { - throw JConfigException("The value at the key $key is not a boolean.") + /** + * Gets the value as an Int. If the value is not found, the default value will be returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as an Int. + */ + fun getInt(key: String, defaultValue: Int): Int { + return if (jConfig[key] is IntNode) { + (jConfig[key] as IntNode).intValue() + } else { + defaultValue + } } - } - fun getBoolean(key: String, defaultValue: Boolean): Boolean { - return if (jConfig[key, defaultValue] is BooleanNode) { - (jConfig[key, defaultValue] as BooleanNode).booleanValue() - } else { - defaultValue + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + fun getBoolean(key: String): Boolean { + return if (jConfig[key] is BooleanNode) { + (jConfig[key] as BooleanNode).booleanValue() + } else { + throw JConfigException("The value at the key $key is not a boolean.") + } } - } - fun getDouble(key: String): Double { - return if (jConfig[key] is DoubleNode) { - (jConfig[key] as DoubleNode).doubleValue() - } else { - throw JConfigException("The value at the key $key is not a double.") + /** + * Gets the value as a Boolean. If the value is not found, the default value will be + * returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a Boolean. + */ + fun getBoolean(key: String, defaultValue: Boolean): Boolean { + return if (jConfig[key, defaultValue] is BooleanNode) { + (jConfig[key, defaultValue] as BooleanNode).booleanValue() + } else { + defaultValue + } } - } - fun DoubleNode(key: String, defaultValue: Double): Double { - return if (jConfig[key] is DoubleNode) { - (jConfig[key] as DoubleNode).doubleValue() - } else { - defaultValue + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + fun getDouble(key: String): Double { + return if (jConfig[key] is DoubleNode) { + (jConfig[key] as DoubleNode).doubleValue() + } else { + throw JConfigException("The value at the key $key is not a double.") + } } - } - fun getLong(key: String): Long { - return if (jConfig[key] is LongNode) { - (jConfig[key] as LongNode).longValue() - } else { - throw JConfigException("The value at the key $key is not a long.") + /** + * Gets the value as a Double. If the value is not found, the default value will be + * returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a Double. + */ + fun getDouble(key: String, defaultValue: Double): Double { + return if (jConfig[key] is DoubleNode) { + (jConfig[key] as DoubleNode).doubleValue() + } else { + defaultValue + } } - } - fun getLong(key: String, defaultValue: Long): Long { - return if (jConfig[key] is LongNode) { - (jConfig[key] as LongNode).longValue() - } else { - defaultValue + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + fun getLong(key: String): Long { + return if (jConfig[key] is LongNode) { + (jConfig[key] as LongNode).longValue() + } else { + throw JConfigException("The value at the key $key is not a long.") + } } - } - fun getFloat(key: String): Float { - return if (jConfig[key] is FloatNode) { - (jConfig[key] as FloatNode).floatValue() - } else { - throw JConfigException("The value at the key $key is not a float.") + /** + * Gets the value as a Long. If the value is not found, the default value will be returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a Long. + */ + fun getLong(key: String, defaultValue: Long): Long { + return if (jConfig[key] is LongNode) { + (jConfig[key] as LongNode).longValue() + } else { + defaultValue + } } - } - fun getFloat(key: String, defaultValue: Float): Float { - return if (jConfig[key, defaultValue] is FloatNode) { - (jConfig[key, defaultValue] as FloatNode).floatValue() - } else { - defaultValue + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + fun getFloat(key: String): Float { + return if (jConfig[key] is FloatNode) { + (jConfig[key] as FloatNode).floatValue() + } else { + throw JConfigException("The value at the key $key is not a float.") + } } - } - operator fun get(key: String): Any { - return jConfig[key] - } + /** + * Gets the value as a Float. If the value is not found, the default value will be returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a Float. + */ + fun getFloat(key: String, defaultValue: Float): Float { + return if (jConfig[key, defaultValue] is FloatNode) { + (jConfig[key, defaultValue] as FloatNode).floatValue() + } else { + defaultValue + } + } + + /** + * Gets the value as a List. + * + * @param key The key to get the value of. + * @return The value as a List. + */ + fun getListNode(key: String): List<*> { + return if (jConfig[key] is ArrayNode) { + (jConfig[key] as ArrayNode).toList() + } else { + throw JConfigException("The value at the key $key is not a list.") + } + } - operator fun get(key: String, defaultValue: Any): Any { - return jConfig[key, defaultValue] + /** + * Gets the value as a List. If the value is not found, the default value will be returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a List. + */ + fun getListNode(key: String, defaultValue: List<*>): List<*> { + return if (jConfig[key, defaultValue] is ArrayNode) { + (jConfig[key, defaultValue] as ArrayNode).toList() + } else { + defaultValue + } + } + + /** + * Gets the value as a Map. + * + * @param key The key to get the value of. + * @return The value as a Map. + */ + fun getMapNode(key: String): Map<*, *> { + return if (jConfig[key] is ObjectNode) { + (jConfig[key] as ObjectNode).fields().asSequence().associate { it.key to it.value } + } else { + throw JConfigException("The value at the key $key is not a map.") + } + } + + /** + * Gets the value as a Map. If the value is not found, the default value will be returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as a Map. + */ + fun getMapNode(key: String, defaultValue: Map<*, *>): Map<*, *> { + return if (jConfig[key, defaultValue] is ObjectNode) { + (jConfig[key, defaultValue] as ObjectNode).fields().asSequence().associate { + it.key to it.value + } + } else { + defaultValue + } + } + + /** + * Gets the value of the specified key. + * + * @param key The key to get the value of. + * @return The value of the specified key. + */ + operator fun get(key: String): Any { + return jConfig[key] + } + + /** + * Gets the value as an Any. If the value is not found, the default value will be returned. + * + * @param key The key to get the value from. + * @param defaultValue The default value to return if the value is not found. + * @return The value as an Any. + */ + operator fun get(key: String, defaultValue: Any): Any { + return jConfig[key, defaultValue] + } } }