Skip to content

Commit

Permalink
Add JVM and nonJVM Money implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Dec 5, 2024
1 parent 425c90f commit 37ee3bf
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/jvmMain/kotlin/JvmMoney.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2024 Kazimierz Pogoda / Xemantic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.xemantic.ai.money

import java.math.BigDecimal
import kotlin.minus
import kotlin.plus
import kotlin.times

public class JvmMoney(private val value: BigDecimal) : Money {

override fun plus(amount: Money): Money = JvmMoney(
value + (amount as JvmMoney).value
)

override fun minus(amount: Money): Money = JvmMoney(
value - (amount as JvmMoney).value
)

override fun times(amount: Money): Money = JvmMoney(
(value * (amount as JvmMoney).value).stripTrailingZeros()
)

override fun times(ratio: Money.Ratio): Money = JvmMoney(
(value * (ratio as JvmRatio).value).stripTrailingZeros()
)

override fun compareTo(
other: Money
): Int = value.compareTo((other as JvmMoney).value)

override fun toString(): String {
return value.toPlainString()
}

override fun equals(
other: Any?
): Boolean = (other != null)
&& (other is JvmMoney)
&& (value == other.value)

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

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

override fun times(amount: Money): Money = JvmMoney(
(value * (amount as JvmMoney).value).stripTrailingZeros()
)

override fun toString(): String {
return value.toPlainString()
}

override fun equals(
other: Any?
): Boolean = (other != null)
&& (other is JvmRatio)
&& (value == other.value)

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

}

}

public actual fun Money(amount: String): Money = JvmMoney(
BigDecimal(amount).stripTrailingZeros()
)

@Suppress("ObjectPropertyName")
private val _ZERO = JvmMoney(BigDecimal.ZERO)
public actual val Money.Companion.ZERO: Money get() = _ZERO

@Suppress("ObjectPropertyName")
private val _ONE = JvmMoney(BigDecimal.ONE)
public actual val Money.Companion.ONE: Money get() = _ONE

public actual fun Money.Companion.Ratio(
value: String
): Money.Ratio = JvmMoney.JvmRatio(
BigDecimal(value).stripTrailingZeros()
)

@Suppress("ObjectPropertyName")
private val _RATIO_ONE = JvmMoney.JvmRatio(BigDecimal.ONE)
public actual val Money.Ratio.Companion.ONE: Money.Ratio get() = _RATIO_ONE
97 changes: 97 additions & 0 deletions src/nonJvmMain/kotlin/NonJvmMoney.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2024 Kazimierz Pogoda / Xemantic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.xemantic.ai.money

import com.ionspin.kotlin.bignum.decimal.BigDecimal

public class NonJvmMoney(private val value: BigDecimal) : Money {

override fun plus(amount: Money): Money = NonJvmMoney(
value + (amount as NonJvmMoney).value
)

override fun minus(amount: Money): Money = NonJvmMoney(
value - (amount as NonJvmMoney).value
)

override fun times(amount: Money): Money = NonJvmMoney(
(value * (amount as NonJvmMoney).value)
)

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

override fun compareTo(
other: Money
): Int = value.compareTo((other as NonJvmMoney).value)

override fun toString(): String {
return value.toPlainString()
}

override fun equals(
other: Any?
): Boolean = (other != null)
&& (other is NonJvmMoney)
&& (value == other.value)

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

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

override fun times(amount: Money): Money = NonJvmMoney(
(value * (amount as NonJvmMoney).value)
)

override fun toString(): String {
return value.toPlainString()
}

override fun equals(
other: Any?
): Boolean = (other != null)
&& (other is NativeRatio)
&& (value == other.value)

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

}

}

public actual fun Money(amount: String): Money = NonJvmMoney(
BigDecimal.parseString(amount)
)

@Suppress("ObjectPropertyName")
private val _ZERO = NonJvmMoney(BigDecimal.ZERO)
public actual val Money.Companion.ZERO: Money get() = _ZERO

@Suppress("ObjectPropertyName")
private val _ONE = NonJvmMoney(BigDecimal.ONE)
public actual val Money.Companion.ONE: Money get() = _ONE

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

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

0 comments on commit 37ee3bf

Please sign in to comment.