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());
}
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");
}
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);
}
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);
}
}
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();
}
Aggregations