From 7497e11ee365264cacc72070ed14fc4ac2b5f612 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 9 Feb 2021 09:38:11 +0700 Subject: [PATCH] Add money mixin cast to and updated money extension and add money view for view in web deployer and try to updated and look around the casting --- .../kotlin/com/cubetiqs/money/MoneyExtension.kt | 17 +++++++++++++++-- .../kotlin/com/cubetiqs/money/MoneyMixin.kt | 9 +++++++++ .../kotlin/com/cubetiqs/money/MoneyObject.kt | 9 ++++++--- src/main/kotlin/com/cubetiqs/money/StdMoney.kt | 6 +++++- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/com/cubetiqs/money/MoneyMixin.kt diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt index 32d343f..a3a0cde 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyExtension.kt @@ -94,8 +94,21 @@ fun String?.fromStringToMoney(): StdMoney { } } -fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) = - this.getCurrency().getCurrency().equals(currency.getCurrency(), ignoreCase = true) +// check the money currency +fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) = this.getCurrency().isEquals(currency) + +// transfer to any money mixin +fun StdMoney.tryToCastToMixin(): MoneyMixin { + return object : MoneyMixin { + fun getValue(): Double { + return this@tryToCastToMixin.getValue() + } + + fun getCurrency(): String { + return this@tryToCastToMixin.getCurrency().getCurrency() + } + } +} inline fun buildMoneyConfigProperties( builderAction: MoneyConfig.MoneyConfigProperties.MoneyConfigPropertiesBuilder.() -> Unit diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyMixin.kt b/src/main/kotlin/com/cubetiqs/money/MoneyMixin.kt new file mode 100644 index 0000000..f88da4a --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/money/MoneyMixin.kt @@ -0,0 +1,9 @@ +package com.cubetiqs.money + +/** + * Money Mixin Interface + * + * @since 1.0 + * @author sombochea + */ +interface MoneyMixin \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt b/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt index f59fa97..3044098 100644 --- a/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt +++ b/src/main/kotlin/com/cubetiqs/money/MoneyObject.kt @@ -8,15 +8,15 @@ import java.util.* * @since 1.0 * @author sombochea */ -class MoneyObject( +open class MoneyObject( private var value: Double, private var currency: String, // operator, used with "with" below // example: if we have next we will use this operator to compute the value with - var operator: MoneyOperator? = null, + private var operator: MoneyOperator? = null, // the value to compute with current object - var with: MoneyObject? = null, + private var with: MoneyObject? = null, ) : StdMoney { override fun getCurrency(): StdMoney.Currency { return StdMoney.initCurrency(currency) @@ -26,6 +26,9 @@ class MoneyObject( return value } + open fun getOperator(): MoneyOperator? = operator + open fun getWith(): MoneyObject? = with + fun appendWithNext(with: MoneyObject?) { if (with == null) { return diff --git a/src/main/kotlin/com/cubetiqs/money/StdMoney.kt b/src/main/kotlin/com/cubetiqs/money/StdMoney.kt index 260ff26..58ae491 100644 --- a/src/main/kotlin/com/cubetiqs/money/StdMoney.kt +++ b/src/main/kotlin/com/cubetiqs/money/StdMoney.kt @@ -7,7 +7,7 @@ package com.cubetiqs.money * @author sombochea * @since 1.0 */ -interface StdMoney { +interface StdMoney : MoneyMixin { /** * Get money's value from current state. * @@ -27,6 +27,10 @@ interface StdMoney { */ interface Currency { fun getCurrency(): String + + fun isEquals(other: Currency): Boolean { + return this.getCurrency().equals(other.getCurrency(), ignoreCase = true) + } } interface ExchangeOperator {