org.javamoney.moneta.internal.convert.ECBRateReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moneta-bp Show documentation
Show all versions of moneta-bp Show documentation
JSR 354 provides an API for representing, transporting, and performing comprehensive calculations with
Money and Currency.
This module implements JSR 354.
/*
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.javamoney.moneta.internal.convert;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import javax.money.convert.ConversionContextBuilder;
import javax.money.convert.ExchangeRate;
import javax.money.convert.ProviderContext;
import javax.money.convert.RateType;
import org.javamoney.moneta.convert.ExchangeRateBuilder;
import org.javamoney.moneta.spi.DefaultNumberValue;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* SAX Event Handler that reads the quotes.
*
* Format:
* Reference rates
* European Central Bank
* ... ...
* ...
*
* @author Anatole Tresch
* @author otaviojava
*/
class ECBRateReader extends DefaultHandler {
/**
* Current timestamp for the given section.
*/
private LocalDate localDate;
private final Map> historicRates;
private final ProviderContext context;
public ECBRateReader(Map> historicRates, ProviderContext context) {
this.historicRates = historicRates;
this.context = context;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if ("Cube".equals(qName)) {
if (attributes.getValue("time")!=null) {
// ...
this.localDate = parseLocalDate(attributes.getValue("time"));
}
if (attributes.getValue("currency")!=null) {
// read data
CurrencyUnit tgtCurrency = Monetary
.getCurrency(attributes.getValue("currency"));
addRate(tgtCurrency, this.localDate, BigDecimal.valueOf(Double
.parseDouble(attributes.getValue("rate"))));
}
}
super.startElement(uri, localName, qName, attributes);
}
private LocalDate parseLocalDate(String date) {
// 2015-03-13
String[] parts = date.split("-");
return new LocalDate(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
/**
* Method to add a currency exchange rate.
*
* @param term the term (target) currency, mapped from EUR.
* @param localDate The target day.
* @param rate The rate.
*/
void addRate(CurrencyUnit term, LocalDate localDate, Number rate) {
RateType rateType = RateType.HISTORIC;
ExchangeRateBuilder builder;
if (localDate!=null) {
// TODO check/test!
if (localDate.equals(LocalDate.now())) {
rateType = RateType.DEFERRED;
}
builder = new ExchangeRateBuilder(
ConversionContextBuilder.create(context, rateType).set(localDate).build());
} else {
builder = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).build());
}
builder.setBase(ECBHistoricRateProvider.BASE_CURRENCY);
builder.setTerm(term);
builder.setFactor(DefaultNumberValue.of(rate));
ExchangeRate exchangeRate = builder.build();
Map rateMap = this.historicRates.get(localDate);
if (rateMap==null) {
synchronized (this.historicRates) {
rateMap = this.historicRates.get(localDate);
if(rateMap==null) {
rateMap = new ConcurrentHashMap<>();
this.historicRates.put(localDate, rateMap);
}
}
}
rateMap.put(term.getCurrencyCode(), exchangeRate);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy