Search in sources :

Example 81 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class CurrenciesTest method testGetCurrencyLocale.

/**
 * Test method for
 * {@link javax.money.Monetary#getCurrencies(java.util.Locale, String...)}.
 */
@Test
public void testGetCurrencyLocale() {
    Set<CurrencyUnit> cur = Monetary.getCurrencies(Locale.US);
    assertNotNull(cur);
    assertTrue(cur.size() == 1);
    Currency jdkCurrency = Currency.getInstance(Locale.US);
    CurrencyUnit unit = cur.iterator().next();
    assertEquals(jdkCurrency.getCurrencyCode(), unit.getCurrencyCode());
    assertEquals(jdkCurrency.getNumericCode(), unit.getNumericCode());
    assertEquals(jdkCurrency.getDefaultFractionDigits(), unit.getDefaultFractionDigits());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) Currency(java.util.Currency) Test(org.testng.annotations.Test)

Example 82 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class JDKCurrencyProvider method loadCurrencies.

private static Map<String, CurrencyUnit> loadCurrencies() {
    Set<Currency> availableCurrencies = Currency.getAvailableCurrencies();
    Map<String, CurrencyUnit> result = new HashMap<>(availableCurrencies.size());
    for (Currency jdkCurrency : availableCurrencies) {
        CurrencyUnit cu = new JDKCurrencyAdapter(jdkCurrency);
        result.put(cu.getCurrencyCode(), cu);
    }
    return Collections.unmodifiableMap(result);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 83 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class IMFAbstractRateProvider method getExchangeRate.

@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
    try {
        if (loadLock.await(30, TimeUnit.SECONDS)) {
            if (currencyToSdr.isEmpty()) {
                return null;
            }
            if (!isAvailable(conversionQuery)) {
                return null;
            }
            CurrencyUnit base = conversionQuery.getBaseCurrency();
            CurrencyUnit term = conversionQuery.getCurrency();
            LocalDate[] times = getQueryDates(conversionQuery);
            ExchangeRate rate1 = getExchangeRate(currencyToSdr.get(base), times);
            ExchangeRate rate2 = getExchangeRate(sdrToCurrency.get(term), times);
            if (base.equals(SDR)) {
                return rate2;
            } else if (term.equals(SDR)) {
                return rate1;
            }
            if (Objects.isNull(rate1) || Objects.isNull(rate2)) {
                return null;
            }
            ConversionContext context = getExchangeContext("imf.digit.fraction");
            ExchangeRateBuilder builder = new ExchangeRateBuilder(context);
            builder.setBase(base);
            builder.setTerm(term);
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);
            return builder.build();
        } else {
            // Lets wait for a successful load only once, then answer requests as data is present.
            loadLock.countDown();
            throw new MonetaryException("Failed to load currency conversion data: " + loadState);
        }
    } catch (InterruptedException e) {
        throw new MonetaryException("Failed to load currency conversion data: Load task has been interrupted.", e);
    }
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) CurrencyUnit(javax.money.CurrencyUnit) ExchangeRate(javax.money.convert.ExchangeRate) ConversionContext(javax.money.convert.ConversionContext) LocalDate(java.time.LocalDate) MonetaryException(javax.money.MonetaryException)

Example 84 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ConversionOperatorsTest method shouldExchangeCurrencyPositiveValue.

@Test
public void shouldExchangeCurrencyPositiveValue() {
    CurrencyUnit real = Monetary.getCurrency("BRL");
    MonetaryAmount money = Money.parse("EUR 2.35");
    MonetaryAmount result = ConversionOperators.exchange(real).apply(money);
    assertNotNull(result);
    assertEquals(result.getCurrency(), real);
    assertEquals(Double.valueOf(2.35), result.getNumber().doubleValue());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Example 85 with CurrencyUnit

use of javax.money.CurrencyUnit in project rulesservice by genny-project.

the class QRules method calcDriverFee.

public Money calcDriverFee(Money input) {
    // TODO, why is this here?
    CurrencyUnit DEFAULT_CURRENCY_TYPE = input.getCurrency();
    Number inputNum = input.getNumber();
    Money driverFee = Money.of(0, DEFAULT_CURRENCY_TYPE);
    Number RANGE_1 = 999.99;
    Number RANGE_2 = 2999.99;
    Number RANGE_3 = 4999.99;
    Number FEE_1 = 0.15;
    Number FEE_2 = 0.125;
    Number FEE_3 = 0.09;
    Number FEE_4 = 0.05;
    Number ONE = 1;
    // const REVERSE_FEE_MULTIPLIER_1 = ( RANGE_2 - RANGE_1 ) * FEE_2;
    // const REVERSE_FEE_MULTIPLIER_2 = ( RANGE_3 - RANGE_2 ) * FEE_3;
    Number subtract01 = MoneyHelper.sub(RANGE_2, RANGE_1);
    Number subtract02 = MoneyHelper.sub(RANGE_3, RANGE_2);
    Number REVERSE_FEE_MULTIPLIER_1 = MoneyHelper.mul(subtract01, FEE_2);
    Number REVERSE_FEE_MULTIPLIER_2 = MoneyHelper.mul(subtract02, FEE_3);
    // const REVERSE_FEE_BOUNDARY_1 = RANGE_1 - ( RANGE_1 * FEE_1 );
    // const REVERSE_FEE_BOUNDARY_2 = RANGE_2 - REVERSE_FEE_MULTIPLIER_1 - ( RANGE_1
    // * FEE_1 );
    // const REVERSE_FEE_BOUNDARY_3 = RANGE_3 - REVERSE_FEE_MULTIPLIER_2 -
    // REVERSE_FEE_MULTIPLIER_1 - ( RANGE_1 * FEE_1 );
    Number multiply01 = MoneyHelper.mul(RANGE_1, FEE_1);
    Number REVERSE_FEE_BOUNDARY_1 = MoneyHelper.sub(RANGE_1, multiply01);
    Number subtract03 = MoneyHelper.sub(RANGE_2, REVERSE_FEE_MULTIPLIER_1);
    Number REVERSE_FEE_BOUNDARY_2 = MoneyHelper.sub(subtract03, multiply01);
    Number subtract04 = MoneyHelper.sub(RANGE_3, REVERSE_FEE_MULTIPLIER_2);
    Number subtract05 = MoneyHelper.sub(subtract04, REVERSE_FEE_MULTIPLIER_1);
    Number REVERSE_FEE_BOUNDARY_3 = MoneyHelper.sub(subtract05, multiply01);
    if (inputNum.doubleValue() < REVERSE_FEE_BOUNDARY_1.doubleValue()) {
        // return calcOwnerFee( inputNum * (1 / (1 - FEE_1)));
        Number subtract = MoneyHelper.sub(ONE, FEE_1);
        Number divide = MoneyHelper.div(ONE, subtract);
        Money multiply = MoneyHelper.mul(input, divide);
        driverFee = calcOwnerFee(multiply);
        println("zone 1 ");
    }
    if (inputNum.doubleValue() >= REVERSE_FEE_BOUNDARY_1.doubleValue() && inputNum.doubleValue() < REVERSE_FEE_BOUNDARY_2.doubleValue()) {
        // calcFee(( input ) * (1 / (1 - (( REVERSE_FEE_BOUNDARY_1 * FEE_1 ) + (( input
        // - REVERSE_FEE_BOUNDARY_1 ) * FEE_2 )) / input )));
        Money subtract1 = MoneyHelper.sub(input, REVERSE_FEE_BOUNDARY_1);
        Money multiply1 = MoneyHelper.mul(subtract1, FEE_2);
        Number multiply2 = MoneyHelper.mul(FEE_1, REVERSE_FEE_BOUNDARY_1);
        Money addition1 = MoneyHelper.add(multiply1, multiply2);
        Money divide1 = MoneyHelper.div(addition1, input);
        Money subtract2 = MoneyHelper.sub(ONE, divide1);
        Money divide2 = MoneyHelper.div(ONE, subtract2);
        Money multiply3 = MoneyHelper.mul(input, divide2);
        driverFee = calcOwnerFee(multiply3);
        println("zone 2 ");
    }
    if (inputNum.doubleValue() >= REVERSE_FEE_BOUNDARY_2.doubleValue() && inputNum.doubleValue() < REVERSE_FEE_BOUNDARY_3.doubleValue()) {
        // calcFee(( input ) * (1 / (1 - (( REVERSE_FEE_BOUNDARY_1 * FEE_1 ) +
        // REVERSE_FEE_MULTIPLIER_1 + (( input - REVERSE_FEE_BOUNDARY_2 ) * FEE_3 )) /
        // input )))
        Money subtract1 = MoneyHelper.sub(input, REVERSE_FEE_BOUNDARY_2);
        Money multiply1 = MoneyHelper.mul(subtract1, FEE_3);
        Number multiply2 = MoneyHelper.mul(REVERSE_FEE_BOUNDARY_1, FEE_1);
        Number addition1 = MoneyHelper.add(multiply2, REVERSE_FEE_MULTIPLIER_1);
        Money addition2 = MoneyHelper.add(multiply1, addition1);
        Money divide1 = MoneyHelper.div(addition2, input);
        Money subtract2 = MoneyHelper.sub(ONE, divide1);
        Money divide2 = MoneyHelper.div(ONE, subtract2);
        Money multiply3 = MoneyHelper.mul(input, divide2);
        driverFee = calcOwnerFee(multiply3);
        println("zone 3 ");
    }
    if (inputNum.doubleValue() >= REVERSE_FEE_BOUNDARY_3.doubleValue()) {
        // calcFee(( input ) * (1 / (1 - (( REVERSE_FEE_BOUNDARY_1 * FEE_1 ) +
        // REVERSE_FEE_MULTIPLIER_1 + REVERSE_FEE_MULTIPLIER_2 + (( input -
        // REVERSE_FEE_BOUNDARY_3 ) * FEE_4 )) / input )))
        Money subtract1 = MoneyHelper.sub(input, REVERSE_FEE_BOUNDARY_3);
        Money multiply1 = MoneyHelper.mul(subtract1, FEE_4);
        Number multiply2 = MoneyHelper.mul(REVERSE_FEE_BOUNDARY_1, FEE_1);
        Number addition1 = MoneyHelper.add(multiply2, REVERSE_FEE_MULTIPLIER_1);
        Number addition2 = MoneyHelper.add(addition1, REVERSE_FEE_MULTIPLIER_2);
        Money addition3 = MoneyHelper.add(multiply1, addition2);
        Money divide1 = MoneyHelper.div(addition3, input);
        Money subtract2 = MoneyHelper.sub(ONE, divide1);
        Money divide2 = MoneyHelper.div(ONE, subtract2);
        Money multiply3 = MoneyHelper.mul(input, divide2);
        driverFee = calcOwnerFee(multiply3);
        println("zone 4 ");
    }
    return driverFee;
}
Also used : AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) CurrencyUnit(javax.money.CurrencyUnit)

Aggregations

CurrencyUnit (javax.money.CurrencyUnit)116 Test (org.testng.annotations.Test)78 MonetaryAmount (javax.money.MonetaryAmount)57 BigDecimal (java.math.BigDecimal)14 Currency (java.util.Currency)8 MonetaryException (javax.money.MonetaryException)6 ExchangeRate (javax.money.convert.ExchangeRate)6 RoundedMoney (org.javamoney.moneta.RoundedMoney)6 IOException (java.io.IOException)5 MathContext (java.math.MathContext)5 MonetaryParseException (javax.money.format.MonetaryParseException)5 Money (org.javamoney.moneta.Money)4 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)3 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2