Skip to content

Commit

Permalink
helper methods for Money.Ratio obtained from String and Int
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Dec 5, 2024
1 parent 1e23bb1 commit 565038c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
12 changes: 12 additions & 0 deletions api/xemantic-ai-money.api
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public final class com/xemantic/ai/money/JvmMoney : com/xemantic/ai/money/Money
public fun hashCode ()I
public fun minus (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
public fun plus (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
public fun times (I)Lcom/xemantic/ai/money/Money;
public fun times (Lcom/xemantic/ai/money/Money$Ratio;)Lcom/xemantic/ai/money/Money;
public fun times (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
public fun toString ()Ljava/lang/String;
Expand All @@ -16,13 +17,15 @@ public final class com/xemantic/ai/money/JvmMoneyKt {
public static final fun getONE (Lcom/xemantic/ai/money/Money$Companion;)Lcom/xemantic/ai/money/Money;
public static final fun getONE (Lcom/xemantic/ai/money/Money$Ratio$Companion;)Lcom/xemantic/ai/money/Money$Ratio;
public static final fun getZERO (Lcom/xemantic/ai/money/Money$Companion;)Lcom/xemantic/ai/money/Money;
public static final fun toMoneyRatio (I)Lcom/xemantic/ai/money/Money$Ratio;
}

public abstract interface class com/xemantic/ai/money/Money {
public static final field Companion Lcom/xemantic/ai/money/Money$Companion;
public abstract fun compareTo (Lcom/xemantic/ai/money/Money;)I
public abstract fun minus (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
public abstract fun plus (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
public abstract fun times (I)Lcom/xemantic/ai/money/Money;
public abstract fun times (Lcom/xemantic/ai/money/Money$Ratio;)Lcom/xemantic/ai/money/Money;
public abstract fun times (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
}
Expand All @@ -31,6 +34,10 @@ public final class com/xemantic/ai/money/Money$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class com/xemantic/ai/money/Money$DefaultImpls {
public static fun times (Lcom/xemantic/ai/money/Money;I)Lcom/xemantic/ai/money/Money;
}

public abstract interface class com/xemantic/ai/money/Money$Ratio {
public static final field Companion Lcom/xemantic/ai/money/Money$Ratio$Companion;
public abstract fun times (Lcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
Expand All @@ -40,6 +47,11 @@ public final class com/xemantic/ai/money/Money$Ratio$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class com/xemantic/ai/money/MoneyKt {
public static final fun getMoneyRatio (Ljava/lang/String;)Lcom/xemantic/ai/money/Money$Ratio;
public static final fun times (ILcom/xemantic/ai/money/Money;)Lcom/xemantic/ai/money/Money;
}

public final class com/xemantic/ai/money/serialization/MoneyRatioSerializer : kotlinx/serialization/KSerializer {
public static final field INSTANCE Lcom/xemantic/ai/money/serialization/MoneyRatioSerializer;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lcom/xemantic/ai/money/Money$Ratio;
Expand Down
9 changes: 9 additions & 0 deletions src/commonMain/kotlin/Money.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public interface Money {

public operator fun times(ratio: Ratio): Money

public operator fun times(
scalar: Int
): Money = this * scalar.toMoneyRatio()

public operator fun compareTo(money: Money): Int

/**
Expand Down Expand Up @@ -80,3 +84,8 @@ public expect fun Money.Companion.Ratio(value: String): Money.Ratio

public expect val Money.Ratio.Companion.ONE: Money.Ratio

public val String.moneyRatio get() = Money.Ratio(this)

public expect fun Int.toMoneyRatio(): Money.Ratio

public operator fun Int.times(money: Money): Money = toMoneyRatio().times(money)
26 changes: 26 additions & 0 deletions src/commonTest/kotlin/MoneyRatioTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.xemantic.ai.money

import com.xemantic.ai.money.test.shouldBe
import kotlin.test.Test
import kotlin.test.assertFailsWith

class MoneyRatioTest {

Expand All @@ -26,6 +27,13 @@ class MoneyRatioTest {
Money.Ratio("1.25").toString() shouldBe "1.25"
}

@Test
fun `Should not create Money Ratio instance from invalid String`() {
assertFailsWith<NumberFormatException> {
Money.Ratio("foo")
}
}

@Test
fun `Should create Money Ratio instance with long fractional part`() {
Money.Ratio("0.0000001").toString() shouldBe "0.0000001"
Expand All @@ -43,4 +51,22 @@ class MoneyRatioTest {
Money.Ratio("0.000001") * Money("3.0") shouldBe Money("0.000003")
}

@Test
fun `Should convert valid string to Money Ratio`() {
"0.000001".moneyRatio shouldBe Money.Ratio("0.000001")
}

@Test
fun `Should not convert invalid string to Money Ratio`() {
assertFailsWith<NumberFormatException> {
"foo".moneyRatio
}
}

@Test
fun `Should convert Int to Money Ratio`() {
1.toMoneyRatio() shouldBe Money.Ratio("1")
42.toMoneyRatio() shouldBe Money.Ratio("42")
}

}
18 changes: 18 additions & 0 deletions src/commonTest/kotlin/MoneyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.xemantic.ai.money

import com.xemantic.ai.money.test.shouldBe
import kotlin.test.Test
import kotlin.test.assertFailsWith

class MoneyTest {

Expand All @@ -26,6 +27,13 @@ class MoneyTest {
Money("42.50").toString() shouldBe "42.5"
}

@Test
fun `Should not create Money instance from invalid String`() {
assertFailsWith<NumberFormatException> {
Money.Ratio("foo")
}
}

@Test
fun `Should create Money instance with long fractional part`() {
Money("42.123456789").toString() shouldBe "42.123456789"
Expand Down Expand Up @@ -84,4 +92,14 @@ class MoneyTest {
(money3 < money1) shouldBe false
}

@Test
fun `Should multiply Int and Money`() {
2 * Money("2.25") shouldBe Money("4.5")
}

@Test
fun `Should multiply Money and Int`() {
Money("2.25") * 2 shouldBe Money("4.5")
}

}
3 changes: 3 additions & 0 deletions src/jvmMain/kotlin/JvmMoney.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ public actual fun Money.Companion.Ratio(
@Suppress("ObjectPropertyName")
private val _RATIO_ONE = JvmMoney.JvmRatio(BigDecimal.ONE)
public actual val Money.Ratio.Companion.ONE: Money.Ratio get() = _RATIO_ONE

public actual fun Int.toMoneyRatio(): Money.Ratio =
JvmMoney.JvmRatio(BigDecimal(this))
13 changes: 8 additions & 5 deletions src/nonJvmMain/kotlin/NonJvmMoney.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class NonJvmMoney(private val value: BigDecimal) : Money {
)

override fun times(ratio: Money.Ratio): Money = NonJvmMoney(
(value * (ratio as NativeRatio).value)
(value * (ratio as NonJvmRatio).value)
)

override fun compareTo(
Expand All @@ -52,7 +52,7 @@ public class NonJvmMoney(private val value: BigDecimal) : Money {

override fun hashCode(): Int = value.hashCode()

internal class NativeRatio(internal val value: BigDecimal) : Money.Ratio {
internal class NonJvmRatio(internal val value: BigDecimal) : Money.Ratio {

override fun times(amount: Money): Money = NonJvmMoney(
(value * (amount as NonJvmMoney).value)
Expand All @@ -65,7 +65,7 @@ public class NonJvmMoney(private val value: BigDecimal) : Money {
override fun equals(
other: Any?
): Boolean = (other != null)
&& (other is NativeRatio)
&& (other is NonJvmRatio)
&& (value == other.value)

override fun hashCode(): Int = value.hashCode()
Expand All @@ -88,10 +88,13 @@ public actual val Money.Companion.ONE: Money get() = _ONE

public actual fun Money.Companion.Ratio(
value: String
): Money.Ratio = NonJvmMoney.NativeRatio(
): Money.Ratio = NonJvmMoney.NonJvmRatio(
BigDecimal.parseString(value)
)

@Suppress("ObjectPropertyName")
private val _RATIO_ONE = NonJvmMoney.NativeRatio(BigDecimal.ONE)
private val _RATIO_ONE = NonJvmMoney.NonJvmRatio(BigDecimal.ONE)
public actual val Money.Ratio.Companion.ONE: Money.Ratio get() = _RATIO_ONE

public actual fun Int.toMoneyRatio(): Money.Ratio =
NonJvmMoney.NonJvmRatio(BigDecimal.fromInt(this))

0 comments on commit 565038c

Please sign in to comment.