Skip to content

Kotlin extensions for javax.money (Moneta) JSR 354

License

Notifications You must be signed in to change notification settings

fhchina/money-kotlin

 
 

Repository files navigation

Kotlin extensions for javax.money (Moneta) JSR 354

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.

Features

  • Kotlin extension functions for all the *Builder in the javax.money package.
  • Kotlin operator functions for the operators defined in javax.money.MonetaryAmount.

Requirements

At least Java 11.

Installation

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>

Examples

All the extension/operator functions have an associated test in src/test/kotlin.

Monetary amount

// Create a monetary amount of a specific type and currency
val money = (10.0).ofCurrency<FastMoney>("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)
}

Currency

"EUR".asCurrency()

currencyContext("PROVIDER") {
    setProviderName("PROVIDER")
}
currencyQuery {
    setCountries("NL".asCountryLocale())
}

Locale

"NL".asCountryLocale()
"NL".asCountryLocale().getCurrency()
"NL".asCountryLocale().getCurrencies()

Rounding

roundingContext("EUR", "PROVIDER") {
    setProviderName("PROVIDER")
}
roundingQuery {
    setScale(1)
}

Format

Locale.GERMANY.monetaryAmountFormat()

amountFormatContext {
    setFormatName("name")
}
amountFormatContext(Locale.GERMANY) {
    setFormatName("name")
}
amountFormatQuery("name") {
    setLocale(Locale.GERMANY)
}

Conversion

money.convertTo("EUR")
money.convertTo("EUR", provider)

providerContext("PROVIDER", RateType.ANY) {
    setProviderName("PROVIDER")
}
conversionContext {
    setRateType(RateType.ANY)
}
conversionQuery {
    setBaseCurrency("EUR")
}

Releasing and publishing

Preparation

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.

Building

Build the project

./gradlew clean build

Releasing

Release a version by tagging a Git commit

git tag -a 1.0.0 -m "Version 1.0.0"

Publishing

Publish the release to Sonatype

./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository

This will publish the artifacts to Maven Central.

About

Kotlin extensions for javax.money (Moneta) JSR 354

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%