2020-08-26 20:06:06 +07:00
|
|
|
package com.cubetiqs.libra.moneyutils
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default money config in static object.
|
2020-08-26 21:10:52 +07:00
|
|
|
* Sample parse format: USD=1,KHR=4000,EUR=0.99
|
2020-08-26 20:06:06 +07:00
|
|
|
*
|
|
|
|
* @author sombochea
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
|
|
|
object MoneyConfig {
|
|
|
|
/**
|
2020-08-26 20:19:06 +07:00
|
|
|
* All money currencies and its rate are stored in memory state for exchange or compute the value.
|
|
|
|
*
|
2020-08-26 20:06:06 +07:00
|
|
|
* Key is the currency
|
|
|
|
* Value is the rate
|
|
|
|
*/
|
2020-08-26 20:19:06 +07:00
|
|
|
private val config: MutableMap<String, Double> = mutableMapOf()
|
2020-08-27 08:48:18 +07:00
|
|
|
|
|
|
|
// validate the config, if have it's valid
|
|
|
|
fun isValid(): Boolean {
|
|
|
|
return this.config.isNotEmpty()
|
|
|
|
}
|
2020-08-26 20:06:06 +07:00
|
|
|
|
2020-08-26 21:10:52 +07:00
|
|
|
/**
|
|
|
|
* Money properties for money config format
|
|
|
|
*/
|
|
|
|
private var properties: MoneyConfigProperties? = null
|
|
|
|
|
|
|
|
val propertiesBuilder = MoneyConfigProperties.MoneyConfigPropertiesBuilder()
|
|
|
|
|
|
|
|
private fun getProperties(): MoneyConfigProperties {
|
|
|
|
return properties ?: propertiesBuilder.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setProperties(properties: MoneyConfigProperties): MoneyConfig {
|
|
|
|
this.properties = properties
|
|
|
|
return MoneyConfig
|
|
|
|
}
|
|
|
|
|
2020-08-26 20:19:06 +07:00
|
|
|
/**
|
|
|
|
* Parse the config string to currency's map within rates
|
|
|
|
* Key is money's currency (String)
|
|
|
|
* Value is money's value (Double)
|
|
|
|
*/
|
|
|
|
fun parse(config: String, clearAllStates: Boolean = true) {
|
|
|
|
if (clearAllStates) {
|
|
|
|
this.config.clear()
|
|
|
|
}
|
2020-08-26 21:10:52 +07:00
|
|
|
val rates = config.split(getProperties().deliSplit)
|
2020-08-26 20:06:06 +07:00
|
|
|
rates.map { i ->
|
2020-08-26 21:10:52 +07:00
|
|
|
val temp = i.split(getProperties().deliEqual)
|
2020-08-26 20:06:06 +07:00
|
|
|
if (temp.size == 2) {
|
2020-08-26 20:19:06 +07:00
|
|
|
val currency = temp[0].toUpperCase()
|
2020-08-26 20:06:06 +07:00
|
|
|
val value = temp[1].toDouble()
|
2020-08-26 20:19:06 +07:00
|
|
|
if (this.config.containsKey(currency)) {
|
|
|
|
this.config.replace(currency, value)
|
|
|
|
} else {
|
|
|
|
this.config.put(currency, value)
|
|
|
|
}
|
2020-08-26 20:06:06 +07:00
|
|
|
} else {
|
2020-08-26 20:19:06 +07:00
|
|
|
throw MoneyCurrencyStateException("money config format is not valid!")
|
2020-08-26 20:06:06 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all currencies with its rate
|
|
|
|
fun getConfig() = this.config
|
|
|
|
|
2020-08-26 20:19:06 +07:00
|
|
|
@Throws(MoneyCurrencyStateException::class)
|
2020-08-26 20:06:06 +07:00
|
|
|
fun getRate(currency: String): Double {
|
2020-08-26 20:19:06 +07:00
|
|
|
return getConfig()[currency.toUpperCase()]
|
|
|
|
?: throw MoneyCurrencyStateException("money currency $currency is not valid!")
|
2020-08-26 20:06:06 +07:00
|
|
|
}
|
2020-08-26 21:10:52 +07:00
|
|
|
|
|
|
|
class MoneyConfigProperties(
|
|
|
|
val deliEqual: Char,
|
|
|
|
val deliSplit: Char,
|
|
|
|
) {
|
|
|
|
class MoneyConfigPropertiesBuilder(
|
|
|
|
private var deliEqual: Char? = null,
|
|
|
|
private var deliSplit: Char? = null,
|
|
|
|
) {
|
|
|
|
fun setDeliEqual(deliEqual: Char) = apply { this.deliEqual = deliEqual }
|
|
|
|
|
|
|
|
private fun getDeliEqual(): Char {
|
|
|
|
return deliEqual ?: '='
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setDeliSplit(deliSplit: Char) = apply { this.deliSplit = deliSplit }
|
|
|
|
|
|
|
|
private fun getDeliSplit(): Char {
|
|
|
|
return deliSplit ?: ','
|
|
|
|
}
|
|
|
|
|
|
|
|
fun build(): MoneyConfigProperties {
|
|
|
|
return MoneyConfigProperties(deliEqual = getDeliEqual(), deliSplit = getDeliSplit())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 20:06:06 +07:00
|
|
|
}
|