Add adavanced config and properties build for money

This commit is contained in:
Sambo Chea 2021-02-08 13:09:54 +07:00
parent 31760ee901
commit faf80ba967
5 changed files with 49 additions and 58 deletions

View File

@ -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()
}

View File

@ -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)
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)
}

View File

@ -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)

View File

@ -1,37 +0,0 @@
package com.cubetiqs.money
import java.lang.reflect.Field
class SpecialStringProcessor: ISerializer {
override fun <T> 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 <T> serialize(data: T?): T?
}

View File

@ -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())