|
| 1 | +# Kotlin extensions for `javax.money` (Moneta) JSR 354 |
| 2 | + |
| 3 | +Extensions which makes working with [`javax.money` JSR 354](https://jcp.org/en/jsr/detail?id=354) with easier when using Kotlin. |
| 4 | + |
| 5 | +This code builds upon the Money API of the JSR proposal, not the [Moneta classes](https://github.com/JavaMoney/jsr354-ri#readme) in the reference implementation. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Kotlin extension functions for all the `*Builder` in the `javax.money` package. |
| 10 | +- Kotlin operator functions for the operators defined in `javax.money.MonetaryAmount`. |
| 11 | + |
| 12 | +## Examples |
| 13 | + |
| 14 | +All the extension/operator functions have an associated test in [`src/test/kotlin`](src/test/kotlin). |
| 15 | + |
| 16 | +### Monetary amount |
| 17 | + |
| 18 | +```kotlin |
| 19 | +// Create a monetary amount of a specific type and currency |
| 20 | +val money = (10.0).ofCurrency<FastMoney>("EUR") |
| 21 | + |
| 22 | +// add |
| 23 | +money + money |
| 24 | ++money |
| 25 | +// subtract & negate |
| 26 | +money - money |
| 27 | +-money |
| 28 | +// multiply |
| 29 | +money * 2 |
| 30 | +2 * money |
| 31 | +// divide & remainder |
| 32 | +money / 2.0 |
| 33 | +money % 2.0 |
| 34 | + |
| 35 | +monetaryAmountFactoryQuery { |
| 36 | + setFixedScale(true) |
| 37 | +} |
| 38 | +monetaryContext { |
| 39 | + setPrecision(1) |
| 40 | +} |
| 41 | +typedMonetaryContext<FastMoney> { |
| 42 | + setPrecision(1) |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Currency |
| 47 | + |
| 48 | +```kotlin |
| 49 | +"EUR".asCurrency() |
| 50 | + |
| 51 | +currencyContext("PROVIDER") { |
| 52 | + setProviderName("PROVIDER") |
| 53 | +} |
| 54 | +currencyQuery { |
| 55 | + setCountries("NL".asCountryLocale()) |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Locale |
| 60 | + |
| 61 | +``kotlin |
| 62 | +"NL".asCountryLocale() |
| 63 | +"NL".asCountryLocale().getCurrency() |
| 64 | +"NL".asCountryLocale().getCurrencies() |
| 65 | +`` |
| 66 | + |
| 67 | +### Rounding |
| 68 | + |
| 69 | +```kotlin |
| 70 | +roundingContext("EUR", "PROVIDER") { |
| 71 | + setProviderName("PROVIDER") |
| 72 | +} |
| 73 | +roundingQuery { |
| 74 | + setScale(1) |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Format |
| 79 | + |
| 80 | +```kotlin |
| 81 | +Locale.GERMANY.monetaryAmountFormat() |
| 82 | + |
| 83 | +amountFormatContext { |
| 84 | + setFormatName("name") |
| 85 | +} |
| 86 | +amountFormatContext(Locale.GERMANY) { |
| 87 | + setFormatName("name") |
| 88 | +} |
| 89 | +amountFormatQuery("name") { |
| 90 | + setLocale(Locale.GERMANY) |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Conversion |
| 95 | + |
| 96 | +```kotlin |
| 97 | +money.convertTo("EUR") |
| 98 | +money.convertTo("EUR", provider) |
| 99 | + |
| 100 | +providerContext("PROVIDER", RateType.ANY) { |
| 101 | + setProviderName("PROVIDER") |
| 102 | +} |
| 103 | +conversionContext { |
| 104 | + setRateType(RateType.ANY) |
| 105 | +} |
| 106 | +conversionQuery { |
| 107 | + setBaseCurrency("EUR") |
| 108 | +} |
| 109 | +``` |
0 commit comments