/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Java;
import java.util.EnumMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import webservice.Currency;
import webservice.CurrencyConvertor;
/**
*
* @author Geert
*/
public class GetCurrency {
private static final int GET_INTERVAL = 300000; // in msecs.
private static EnumMap<Currency, Double> conversionRates = new EnumMap<Currency, Double>(Currency.class);
/**
* Get the conversion rate of the wanted currency according to the Euro.
* @param cur the wanted currency.
* @return the conversion rate of the wanted currency.
*/
public static double getConversionRate(Currency cur) {
Double rate = null;
boolean firstTry = true;
if (conversionRates.get(cur) == null) {
startConversionRateTimer(cur);
do {
try {
rate = conversionRates.get(cur);
if (firstTry)
firstTry = false;
else
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(GetCurrency.class.getName()).log(Level.SEVERE, null, ex);
}
} while (rate == null);
}
return conversionRates.get(cur);
}
private static void startConversionRateTimer(final Currency currency) {
TimerTask task = new TimerTask() {
@Override
public void run() {
conversionRates.put(currency, new CurrencyConvertor().getCurrencyConvertorSoap().conversionRate(Currency.EUR, currency));
}
};
new Timer().schedule(task, 0, GET_INTERVAL);
}
}