Extensions which makes working with javax.money
JSR 354 with easier when using Kotlin.
This code builds upon the Money API of the JSR proposal, not the Moneta classes in the reference implementation.
- Kotlin extension functions for all the
*Builder
in thejavax.money
package. - Kotlin operator functions for the operators defined in
javax.money.MonetaryAmount
.
At least Java 21.
At least Kotlin 2.
Find the latest version on Maven Central.
Add the money-kotlin
dependency to your project.
Gradle:
implementation("nl.hiddewieringa:money-kotlin:$moneyKotlinVersion")
Maven:
<dependency>
<groupId>nl.hiddewieringa</groupId>
<artifactId>money-kotlin</artifactId>
<version>${moneyKotlin.version}</version>
</dependency>
This library depends only on the Money API of the JSR proposal. Ensure you add a dependency on a specific implementation such as org.javamoney:moneta:<version>
.
All the extension/operator functions have an associated test in src/test/kotlin
.
// Create a monetary amount of a specific type and currency
val money = (10.0).ofCurrency<FastMoney>("EUR")
// Create a monetary amount of a default type for the provider and currency.
// If using Moneta, its default type is Money and can be changed by specifying
// org.javamoney.moneta.Money.defaults.amountType in the javamoney.properties.
val money = (10.0).ofCurrency("EUR")
//or
val money = (10.0).ofCurrency<MonetaryAmount>("EUR")
// add
money + money
+money
// subtract & negate
money - money
-money
// multiply
money * 2
2 * money
// divide & remainder
money / 2.0
money % 2.0
monetaryAmountFactoryQuery {
setFixedScale(true)
}
monetaryContext {
setPrecision(1)
}
typedMonetaryContext<FastMoney> {
setPrecision(1)
}
"EUR".asCurrency()
currencyContext("PROVIDER") {
setProviderName("PROVIDER")
}
currencyQuery {
setCountries("NL".asCountryLocale())
}
"NL".asCountryLocale()
"NL".asCountryLocale().getCurrency()
"NL".asCountryLocale().getCurrencies()
roundingContext("EUR", "PROVIDER") {
setProviderName("PROVIDER")
}
roundingQuery {
setScale(1)
}
Locale.GERMANY.monetaryAmountFormat()
amountFormatContext {
setFormatName("name")
}
amountFormatContext(Locale.GERMANY) {
setFormatName("name")
}
amountFormatQuery("name") {
setLocale(Locale.GERMANY)
}
money.convertTo("EUR")
money.convertTo("EUR", provider)
providerContext("PROVIDER", RateType.ANY) {
setProviderName("PROVIDER")
}
conversionContext {
setRateType(RateType.ANY)
}
conversionQuery {
setBaseCurrency("EUR")
}
Ensure that the properties signing.keyId
, signing.password
and signing.secretKeyRingFile=
are set in ~/.gradle/gradle.properties
.
These properties are neccesary for signing releases.
Ensure that the properties sonatypeUsername
and sonatypePassword
are set in ~/.gradle/gradle.properties
.
These properties are credentials for publishing to Maven Central.
Build the project
./gradlew clean build
Release a version by tagging a Git commit
git tag -a 1.0.0 -m "Version 1.0.0"
Publish the release to Sonatype
./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository
This will publish the artifacts to Maven Central.