From e82fb280abc6f1b7156dbcf3e1be10b128f5fc84 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 27 Aug 2020 21:42:05 +0700 Subject: [PATCH] Add special string for annotation and its processor for only field Updated std money and add money arithemtic --- .../com/cubetiqs/libra/moneyutils/Money.kt | 4 +- .../libra/moneyutils/MoneyArithmetic.kt | 5 +++ .../libra/moneyutils/MoneyExtension.kt | 6 ++- .../libra/moneyutils/SpecialString.kt | 5 +++ .../moneyutils/SpecialStringProcessor.kt | 37 +++++++++++++++++++ .../com/cubetiqs/libra/moneyutils/StdMoney.kt | 1 + src/test/kotlin/MoneyTests.kt | 19 +++++----- src/test/kotlin/ObjectTests.kt | 37 +++++++++++++++++++ 8 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyArithmetic.kt create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialString.kt create mode 100644 src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialStringProcessor.kt create mode 100644 src/test/kotlin/ObjectTests.kt diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt index a452b8e..30dfe34 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/Money.kt @@ -2,7 +2,7 @@ package com.cubetiqs.libra.moneyutils open class Money( var value: Double, - private var currency: String = "USD" + @SpecialString(trim = true, upperCase = true) private var currency: String = "USD" ) : StdMoney { //////////////////// - PROPERTIES - //////////////////// @@ -12,7 +12,7 @@ open class Money( } override fun getMoneyCurrency(): String { - return this.currency.toUpperCase() + return this.currency } //////////////////// - GENERIC - //////////////////// diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyArithmetic.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyArithmetic.kt new file mode 100644 index 0000000..81c529d --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyArithmetic.kt @@ -0,0 +1,5 @@ +package com.cubetiqs.libra.moneyutils + +fun StdMoney.addMoney(value: Double, currency: String): StdMoney { + return this + Money.create(value, currency) +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExtension.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExtension.kt index 05caf96..12defd7 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExtension.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/MoneyExtension.kt @@ -1,3 +1,7 @@ package com.cubetiqs.libra.moneyutils -fun StdMoney \ No newline at end of file +fun StdMoney.exchangeTo(currency: String): StdMoney { + return MoneyExchangeUtils.exchange(this, currency) +} + +fun StdMoney.isMatchedCurrency(currency: String) = this.getMoneyCurrency().equals(currency, ignoreCase = true) \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialString.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialString.kt new file mode 100644 index 0000000..3b4b644 --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialString.kt @@ -0,0 +1,5 @@ +package com.cubetiqs.libra.moneyutils + +@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/libra/moneyutils/SpecialStringProcessor.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialStringProcessor.kt new file mode 100644 index 0000000..b33652b --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/SpecialStringProcessor.kt @@ -0,0 +1,37 @@ +package com.cubetiqs.libra.moneyutils + +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/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt b/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt index 81ed748..e000a4e 100644 --- a/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt +++ b/src/main/kotlin/com/cubetiqs/libra/moneyutils/StdMoney.kt @@ -20,5 +20,6 @@ interface StdMoney { * * @return String */ + @SpecialString fun getMoneyCurrency(): String } \ No newline at end of file diff --git a/src/test/kotlin/MoneyTests.kt b/src/test/kotlin/MoneyTests.kt index f9991c4..475c150 100644 --- a/src/test/kotlin/MoneyTests.kt +++ b/src/test/kotlin/MoneyTests.kt @@ -2,20 +2,21 @@ 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.plus -import com.cubetiqs.libra.moneyutils.times -import com.cubetiqs.libra.moneyutils.timesAssign +import com.cubetiqs.libra.moneyutils.SpecialStringProcessor import org.junit.Assert import org.junit.Test class MoneyTests { @Test fun money_operator_test() { - val money = Money(10.0) - val money2 = Money(20.0) - money *= money - println((money + money2) * money2) - Assert.assertEquals(100.0, money.value, 0.0) + // val money = Money(10.0) + // val money2 = Money(20.0) + // money *= money + // println((money + money2) * money2) + // Assert.assertEquals(100.0, money.value, 0.0) + + val test = SpecialStringProcessor().serialize(Money(1.0, " usd ")) + println(test) } @Test @@ -47,7 +48,7 @@ class MoneyTests { .setDeliEqual('=') .setDeliSplit(';') - MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99") + MoneyConfig.parse("USD:1,KHR=4000,EUR=0.99") val moneyUsd = Money.ONE val moneyKhr = Money.create(20000.0, MoneyCurrency.KHR) diff --git a/src/test/kotlin/ObjectTests.kt b/src/test/kotlin/ObjectTests.kt new file mode 100644 index 0000000..6ea44db --- /dev/null +++ b/src/test/kotlin/ObjectTests.kt @@ -0,0 +1,37 @@ +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) + } + + +} \ No newline at end of file