Search in sources :

Example 91 with CurrencyUnit

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

the class ExchangeCurrencyOperatorTest method shouldExchangeCurrencyNegativeValue.

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

Example 92 with CurrencyUnit

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

the class ExchangeRateBuilderTest method equalsTest.

@Test
public void equalsTest() {
    DefaultNumberValue factor = new DefaultNumberValue(1.1);
    DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1"));
    CurrencyUnit EUR = Monetary.getCurrency("EUR");
    CurrencyUnit GBP = Monetary.getCurrency("GBP");
    ExchangeRate rate1 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(factor).build();
    ExchangeRate rate2 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(factor).build();
    ExchangeRate rate3 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(bigDecimalFactor).build();
    assertEquals(rate1, rate2, "Rates with same factor");
    assertEquals(rate1, rate3, "Rates with numerically equal factor");
    assertEquals(rate1.hashCode(), rate2.hashCode(), "Hashes with same factor");
    assertEquals(rate1.hashCode(), rate3.hashCode(), "Hashes with numerically equal factor");
}
Also used : DefaultNumberValue(org.javamoney.moneta.spi.DefaultNumberValue) CurrencyUnit(javax.money.CurrencyUnit) ExchangeRate(javax.money.convert.ExchangeRate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 93 with CurrencyUnit

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

the class ECBRateReader method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if ("Cube".equals(qName)) {
        if (attributes.getValue("time") != null) {
            // <Cube time="2015-03-13">...
            this.localDate = parseLocalDate(attributes.getValue("time"));
        }
        if (attributes.getValue("currency") != null) {
            // read data <Cube currency="USD" rate="1.3349"/>
            CurrencyUnit tgtCurrency = Monetary.getCurrency(attributes.getValue("currency"));
            addRate(tgtCurrency, this.localDate, BigDecimal.valueOf(Double.parseDouble(attributes.getValue("rate"))));
        }
    }
    super.startElement(uri, localName, qName, attributes);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 94 with CurrencyUnit

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

the class IMFRateProvider 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();
            Calendar timestamp = conversionQuery.get(Calendar.class);
            if (timestamp == null) {
                timestamp = conversionQuery.get(GregorianCalendar.class);
            }
            ExchangeRate rate1;
            ExchangeRate rate2;
            LocalDate localDate;
            if (timestamp == null) {
                localDate = LocalDate.yesterday();
                rate1 = lookupRate(currencyToSdr.get(base), localDate);
                rate2 = lookupRate(sdrToCurrency.get(term), localDate);
                if (rate1 == null || rate2 == null) {
                    localDate = LocalDate.beforeDays(2);
                }
                rate1 = lookupRate(currencyToSdr.get(base), localDate);
                rate2 = lookupRate(sdrToCurrency.get(term), localDate);
                if (rate1 == null || rate2 == null) {
                    localDate = LocalDate.beforeDays(3);
                    rate1 = lookupRate(currencyToSdr.get(base), localDate);
                    rate2 = lookupRate(sdrToCurrency.get(term), localDate);
                }
            } else {
                localDate = LocalDate.from(timestamp);
                rate1 = lookupRate(currencyToSdr.get(base), localDate);
                rate2 = lookupRate(sdrToCurrency.get(term), localDate);
            }
            if (rate1 == null || rate2 == null) {
                return null;
            }
            if (base.equals(SDR)) {
                return rate2;
            } else if (term.equals(SDR)) {
                return rate1;
            }
            ExchangeRateBuilder builder = new ExchangeRateBuilder(ConversionContext.of(CONTEXT.getProviderName(), RateType.HISTORIC));
            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) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) MonetaryException(javax.money.MonetaryException)

Example 95 with CurrencyUnit

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

the class DefaultMonetaryAmountFormat method parse.

/**
 * Fully parses the text into an instance of {@code MonetaryAmount}.
 * <p>
 * The parse must complete normally and parse the entire text. If the parse
 * completes without reading the entire length of the text, an exception is
 * thrown. If any other problem occurs during parsing, an exception is
 * thrown.
 *
 * @param text the text to parse, not null
 * @return the parsed value, never {@code null}
 * @throws UnsupportedOperationException             if the formatter is unable to parse
 * @throws javax.money.format.MonetaryParseException if there is a problem while parsing
 */
public MonetaryAmount parse(CharSequence text) throws MonetaryParseException {
    ParseContext ctx = new ParseContext(text);
    try {
        for (FormatToken token : this.positiveTokens) {
            token.parse(ctx);
        }
    } catch (Exception e) {
        // try parsing negative...
        Logger log = Logger.getLogger(getClass().getName());
        if (log.isLoggable(Level.FINEST)) {
            log.log(Level.FINEST, "Failed to parse positive pattern, trying negative for: " + text, e);
        }
        for (FormatToken token : this.negativeTokens) {
            token.parse(ctx);
        }
    }
    CurrencyUnit unit = ctx.getParsedCurrency();
    Number num = ctx.getParsedNumber();
    if (unit == null) {
        unit = this.amountFormatContext.get(CurrencyUnit.class);
    }
    if (num == null) {
        throw new MonetaryParseException(text.toString(), -1);
    }
    MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory();
    if (factory == null) {
        factory = Monetary.getDefaultAmountFactory();
    }
    return factory.setCurrency(unit).setNumber(num).create();
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) Logger(java.util.logging.Logger) IOException(java.io.IOException) MonetaryParseException(javax.money.format.MonetaryParseException) MonetaryParseException(javax.money.format.MonetaryParseException)

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