From 315d2104c85ae06f3bd0a49a8789cd8115f517ce Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 26 Aug 2020 21:10:52 +0700 Subject: [PATCH] Add money config and builder pattern for its properties --- .../cubetiqs/libra/moneyutils/MoneyConfig.kt | 50 +++++++++++++++++-- src/test/kotlin/MoneyTests.kt | 14 ++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt index c41a90b..5b6c5ce 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyConfig.kt @@ -2,14 +2,12 @@ package com.cubetiqs.libra.moneyutils /** * Default money config in static object. + * Sample parse format: USD=1,KHR=4000,EUR=0.99 * * @author sombochea * @since 1.0 */ 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. * @@ -18,6 +16,22 @@ object MoneyConfig { */ private val config: MutableMap = 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 * Key is money's currency (String) @@ -27,9 +41,9 @@ object MoneyConfig { if (clearAllStates) { this.config.clear() } - val rates = config.split(SPLIT_ORD_1) + val rates = config.split(getProperties().deliSplit) rates.map { i -> - val temp = i.split(SPLIT_ORD_2) + val temp = i.split(getProperties().deliEqual) if (temp.size == 2) { val currency = temp[0].toUpperCase() val value = temp[1].toDouble() @@ -52,4 +66,30 @@ object MoneyConfig { return getConfig()[currency.toUpperCase()] ?: 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()) + } + } + } } \ No newline at end of file diff --git a/src/test/kotlin/MoneyTests.kt b/src/test/kotlin/MoneyTests.kt index 2898aa1..7fef8b8 100644 --- a/src/test/kotlin/MoneyTests.kt +++ b/src/test/kotlin/MoneyTests.kt @@ -1,4 +1,5 @@ import com.cubetiqs.libra.moneyutils.Money +import com.cubetiqs.libra.moneyutils.MoneyConfig import com.cubetiqs.libra.moneyutils.plus import com.cubetiqs.libra.moneyutils.times import com.cubetiqs.libra.moneyutils.timesAssign @@ -13,5 +14,18 @@ class MoneyTests { money *= money println((money + money2) * money2) Assert.assertEquals(10, 10) + + val properties = MoneyConfig + .MoneyConfigProperties + .MoneyConfigPropertiesBuilder() + .setDeliEqual(':') + .setDeliSplit(',') + .build() + + MoneyConfig + .setProperties(properties) + .parse("USD:1,KHR:4000") + + println(MoneyConfig.getConfig()) } } \ No newline at end of file