From faf80ba9673eae3d9dadc97ffb2c0c8aadb245d1 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Mon, 8 Feb 2021 13:09:54 +0700 Subject: [PATCH] Add adavanced config and properties build for money --- .../kotlin/com/cubetiqs/money/MoneyConfig.kt | 17 ++++++++- .../com/cubetiqs/money/MoneyExtension.kt | 17 ++++++++- .../com/cubetiqs/money/SpecialString.kt | 5 --- .../cubetiqs/money/SpecialStringProcessor.kt | 37 ------------------- src/test/kotlin/MoneyTests.kt | 31 +++++++++------- 5 files changed, 49 insertions(+), 58 deletions(-) delete mode 100644 src/main/kotlin/com/cubetiqs/money/SpecialString.kt delete mode 100644 src/main/kotlin/com/cubetiqs/money/SpecialStringProcessor.kt diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt b/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt index 9f3b433..16b7ada 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyConfig.kt @@ -26,7 +26,7 @@ object MoneyConfig { */ private var properties: MoneyConfigProperties? = null - val propertiesBuilder = MoneyConfigProperties.MoneyConfigPropertiesBuilder() + private val propertiesBuilder = MoneyConfigProperties.MoneyConfigPropertiesBuilder() private fun getProperties(): MoneyConfigProperties { return properties ?: propertiesBuilder.build() @@ -43,14 +43,16 @@ object MoneyConfig { * Value is money's value (Double) */ fun parse(config: String, clearAllStates: Boolean = true) { + // remove all states, if needed if (clearAllStates) { MoneyConfig.config.clear() } + val rates = config.split(getProperties().deliSplit) rates.map { i -> val temp = i.split(getProperties().deliEqual) if (temp.size == 2) { - val currency = temp[0].toUpperCase() + val currency = temp[0].toUpperCase().trim() val value = temp[1].toDouble() if (MoneyConfig.config.containsKey(currency)) { MoneyConfig.config.replace(currency, value) @@ -63,6 +65,15 @@ object MoneyConfig { } } + fun appendRate(currency: String, rate: Double) = apply { + val currencyKey = currency.toUpperCase().trim() + if (config.containsKey(currencyKey)) { + config.replace(currencyKey, rate) + } else { + config[currencyKey] = rate + } + } + // all currencies with its rate fun getConfig() = config @@ -97,4 +108,6 @@ object MoneyConfig { } } } + + fun builder() = MoneyConfigProperties.MoneyConfigPropertiesBuilder() } \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt index 2afabf3..baa1fbb 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt @@ -4,4 +4,19 @@ fun StdMoney.exchangeTo(currency: StdMoney.Currency): StdMoney { return MoneyExchangeUtils.exchange(this, currency) } -fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) = this.getMoneyCurrency().getCurrency().equals(currency.getCurrency(), ignoreCase = true) \ No newline at end of file +fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) = + this.getMoneyCurrency().getCurrency().equals(currency.getCurrency(), ignoreCase = true) + +inline fun buildMoneyConfigProperties( + builderAction: MoneyConfig.MoneyConfigProperties.MoneyConfigPropertiesBuilder.() -> Unit +): MoneyConfig.MoneyConfigProperties { + return MoneyConfig + .builder().apply(builderAction) + .build() +} + +inline fun applyMoneyConfig( + builderAction: MoneyConfig.() -> Unit, +) { + MoneyConfig.apply(builderAction) +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/money/SpecialString.kt b/src/main/kotlin/com/cubetiqs/money/SpecialString.kt deleted file mode 100644 index 0637f57..0000000 --- a/src/main/kotlin/com/cubetiqs/money/SpecialString.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.cubetiqs.money - -@Target(AnnotationTarget.FIELD) -@Retention(AnnotationRetention.RUNTIME) -annotation class SpecialString(val value: String = "", val upperCase: Boolean = true, val trim: Boolean = true) \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/money/SpecialStringProcessor.kt b/src/main/kotlin/com/cubetiqs/money/SpecialStringProcessor.kt deleted file mode 100644 index 71e7cf9..0000000 --- a/src/main/kotlin/com/cubetiqs/money/SpecialStringProcessor.kt +++ /dev/null @@ -1,37 +0,0 @@ -package com.cubetiqs.money - -import java.lang.reflect.Field - -class SpecialStringProcessor: ISerializer { - override fun serialize(data: T?): T? { - if (data == null) return null - val clazz = data.javaClass - clazz.declaredFields.forEach { - it.isAccessible = true - if (it.isAnnotationPresent(SpecialString::class.java) && it.genericType == String::class.java) { - val annotationValues = it.getAnnotation(SpecialString::class.java) - var value = it.get(data).toString() - if (annotationValues.trim) { - value = value.trim() - } - if (annotationValues.upperCase) { - value = value.toUpperCase() - } - it.set(data, value) - } - } - - return data - } - - private fun getSerializeKey(field: Field): String { - val annotationValue = field.getAnnotation(SpecialString::class.java).value - return if (annotationValue.isEmpty()) { - field.name - } else annotationValue - } -} - -interface ISerializer { - fun serialize(data: T?): T? -} \ No newline at end of file diff --git a/src/test/kotlin/MoneyTests.kt b/src/test/kotlin/MoneyTests.kt index 353e46c..7e2854b 100644 --- a/src/test/kotlin/MoneyTests.kt +++ b/src/test/kotlin/MoneyTests.kt @@ -1,23 +1,28 @@ -import com.cubetiqs.money.Money -import com.cubetiqs.money.MoneyConfig -import com.cubetiqs.money.MoneyCurrency -import com.cubetiqs.money.MoneyExchangeUtils +import com.cubetiqs.money.* import org.junit.Assert import org.junit.Test class MoneyTests { @Test fun exchange_2usd_to_khr_test() { - val properties = MoneyConfig - .MoneyConfigProperties - .MoneyConfigPropertiesBuilder() - .setDeliEqual(':') - .setDeliSplit(',') - .build() +// val properties = MoneyConfig +// .MoneyConfigProperties +// .MoneyConfigPropertiesBuilder() +// .setDeliEqual(':') +// .setDeliSplit(',') +// .build() - MoneyConfig - .setProperties(properties) - .parse("USD:1,KHR:4000") + val properties = buildMoneyConfigProperties { + setDeliEqual(':') + setDeliSplit(',') + } + + applyMoneyConfig { + setProperties(properties) + // parse("USD:1,KHR:4000") + appendRate("usd", 1.0) + appendRate("khr", 4000.0) + } // Is valid for money config? Assert.assertTrue(MoneyConfig.isValid())