Add money config and builder pattern for its properties

This commit is contained in:
Sambo Chea 2020-08-26 21:10:52 +07:00
parent 9204d68e44
commit 315d2104c8
2 changed files with 59 additions and 5 deletions

View File

@ -2,14 +2,12 @@ package com.cubetiqs.libra.moneyutils
/** /**
* Default money config in static object. * Default money config in static object.
* Sample parse format: USD=1,KHR=4000,EUR=0.99
* *
* @author sombochea * @author sombochea
* @since 1.0 * @since 1.0
*/ */
object MoneyConfig { object MoneyConfig {
private const val SPLIT_ORD_1 = ','
private const val SPLIT_ORD_2 = '='
/** /**
* All money currencies and its rate are stored in memory state for exchange or compute the value. * All money currencies and its rate are stored in memory state for exchange or compute the value.
* *
@ -18,6 +16,22 @@ object MoneyConfig {
*/ */
private val config: MutableMap<String, Double> = mutableMapOf() private val config: MutableMap<String, Double> = mutableMapOf()
/**
* 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
}
/** /**
* Parse the config string to currency's map within rates * Parse the config string to currency's map within rates
* Key is money's currency (String) * Key is money's currency (String)
@ -27,9 +41,9 @@ object MoneyConfig {
if (clearAllStates) { if (clearAllStates) {
this.config.clear() this.config.clear()
} }
val rates = config.split(SPLIT_ORD_1) val rates = config.split(getProperties().deliSplit)
rates.map { i -> rates.map { i ->
val temp = i.split(SPLIT_ORD_2) val temp = i.split(getProperties().deliEqual)
if (temp.size == 2) { if (temp.size == 2) {
val currency = temp[0].toUpperCase() val currency = temp[0].toUpperCase()
val value = temp[1].toDouble() val value = temp[1].toDouble()
@ -52,4 +66,30 @@ object MoneyConfig {
return getConfig()[currency.toUpperCase()] return getConfig()[currency.toUpperCase()]
?: throw MoneyCurrencyStateException("money currency $currency is not valid!") ?: throw MoneyCurrencyStateException("money currency $currency is not valid!")
} }
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())
}
}
}
} }

View File

@ -1,4 +1,5 @@
import com.cubetiqs.libra.moneyutils.Money import com.cubetiqs.libra.moneyutils.Money
import com.cubetiqs.libra.moneyutils.MoneyConfig
import com.cubetiqs.libra.moneyutils.plus import com.cubetiqs.libra.moneyutils.plus
import com.cubetiqs.libra.moneyutils.times import com.cubetiqs.libra.moneyutils.times
import com.cubetiqs.libra.moneyutils.timesAssign import com.cubetiqs.libra.moneyutils.timesAssign
@ -13,5 +14,18 @@ class MoneyTests {
money *= money money *= money
println((money + money2) * money2) println((money + money2) * money2)
Assert.assertEquals(10, 10) Assert.assertEquals(10, 10)
val properties = MoneyConfig
.MoneyConfigProperties
.MoneyConfigPropertiesBuilder()
.setDeliEqual(':')
.setDeliSplit(',')
.build()
MoneyConfig
.setProperties(properties)
.parse("USD:1,KHR:4000")
println(MoneyConfig.getConfig())
} }
} }