Add and refactoring money module for module package

This commit is contained in:
2021-02-08 11:24:14 +07:00
parent e82fb280ab
commit 19130727e3
28 changed files with 27 additions and 527 deletions

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
open class Money(
var value: Double,

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
fun StdMoney.addMoney(value: Double, currency: String): StdMoney {
return this + Money.create(value, currency)

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
/**
* Default money config in static object.
@@ -18,7 +18,7 @@ object MoneyConfig {
// validate the config, if have it's valid
fun isValid(): Boolean {
return this.config.isNotEmpty()
return config.isNotEmpty()
}
/**
@@ -33,7 +33,7 @@ object MoneyConfig {
}
fun setProperties(properties: MoneyConfigProperties): MoneyConfig {
this.properties = properties
MoneyConfig.properties = properties
return MoneyConfig
}
@@ -44,7 +44,7 @@ object MoneyConfig {
*/
fun parse(config: String, clearAllStates: Boolean = true) {
if (clearAllStates) {
this.config.clear()
MoneyConfig.config.clear()
}
val rates = config.split(getProperties().deliSplit)
rates.map { i ->
@@ -52,10 +52,10 @@ object MoneyConfig {
if (temp.size == 2) {
val currency = temp[0].toUpperCase()
val value = temp[1].toDouble()
if (this.config.containsKey(currency)) {
this.config.replace(currency, value)
if (MoneyConfig.config.containsKey(currency)) {
MoneyConfig.config.replace(currency, value)
} else {
this.config.put(currency, value)
MoneyConfig.config.put(currency, value)
}
} else {
throw MoneyCurrencyStateException("money config format is not valid!")
@@ -64,7 +64,7 @@ object MoneyConfig {
}
// all currencies with its rate
fun getConfig() = this.config
fun getConfig() = config
@Throws(MoneyCurrencyStateException::class)
fun getRate(currency: String): Double {

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
enum class MoneyCurrency {
USD,

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
/**
* Default money currency state exception

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
interface MoneyExchangeAdapter {
fun getRate(currency: String): Double

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
object MoneyExchangeUtils {
fun exchange(exchangeFrom: StdMoney, exchangeToCurrency: String): StdMoney {

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
fun StdMoney.exchangeTo(currency: String): StdMoney {
return MoneyExchangeUtils.exchange(this, currency)

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
operator fun StdMoney.unaryMinus() = (-getMoneyValue())
operator fun StdMoney.unaryPlus() = (+getMoneyValue())

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
import java.lang.reflect.Field

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
/**
* Default standard money style

View File

@@ -1,4 +1,4 @@
package com.cubetiqs.libra.moneyutils
package com.cubetiqs.money
interface StdMoneyFormation {
fun format(): String

View File

@@ -1,8 +1,8 @@
import com.cubetiqs.libra.moneyutils.Money
import com.cubetiqs.libra.moneyutils.MoneyConfig
import com.cubetiqs.libra.moneyutils.MoneyCurrency
import com.cubetiqs.libra.moneyutils.MoneyExchangeUtils
import com.cubetiqs.libra.moneyutils.SpecialStringProcessor
import com.cubetiqs.money.Money
import com.cubetiqs.money.MoneyConfig
import com.cubetiqs.money.MoneyCurrency
import com.cubetiqs.money.MoneyExchangeUtils
import com.cubetiqs.money.SpecialStringProcessor
import org.junit.Assert
import org.junit.Test

View File

@@ -1,37 +0,0 @@
import org.junit.Test
class ObjectTests {
@Test
fun builder_object_test() {
val person = Person
.builder()
.name("Sambo Chea")
.id(10)
.build()
println(person)
}
}
data class Person(val id: Long? = null, val name: String? = null) {
companion object {
fun builder(): PersonBuilder {
return PersonBuilder()
}
}
}
class PersonBuilder {
private var id: Long? = null
private var name: String? = null
fun id(id: Long?) = apply { this.id = id }
fun name(name: String?) = apply { this.name = name }
fun build(): Person {
return Person(id, name)
}
}