# HG changeset patch # User japod@localhost # Date 1222783892 -7200 # Node ID 36331f7244bd809e22809d17a729e7a68cc28935 # Parent 724f953a5f3e2a691ade506cae4e32d66663677c finishing update of solution 04 to 1.5 diff -r 724f953a5f3e -r 36331f7244bd task1/solution04/src/org/apidesign/apifest08/currency/CurrencyValues.java --- a/task1/solution04/src/org/apidesign/apifest08/currency/CurrencyValues.java Tue Sep 30 15:17:24 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,122 +0,0 @@ -package org.apidesign.apifest08.currency; - - -import java.math.BigDecimal; -import java.util.Currency; -import java.util.HashMap; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; - - -/** - * Keeps track of the current value for each currency as USD. - * - * @author D'Arcy Smith - * @version 1.0 - */ -class CurrencyValues -{ - /** - * The values expressed in USD. - */ - private static Map values; - - /** - * Update the values periodically - */ - private static final Timer refresher; - - static - { - final Refresher refresherTask; - final long delay; - - // load the map NOW! (don't use the scheduler to do it just because we want - // to be 100% certain it is loaded before anything else can be called. - refresh(); - - refresherTask = new Refresher(); - refresher = new Timer("CurrencyValues Refresher", true); - - // update once an hour - delay = 1000 * 60 * 60; - refresher.scheduleAtFixedRate(refresherTask, delay, delay); - } - - /** - * Prevent accidental creation. - */ - private CurrencyValues() - { - } - - /** - * Refresh the currency values. - */ - static void refresh() - { - Map newValues; - Currency currency; - - newValues = new HashMap(); - - // these would update from a data source, database, web service, something... - currency = Currency.getInstance("USD"); - newValues.put(currency, BigDecimal.valueOf(1.0).setScale(2)); - - currency = Currency.getInstance("CZK"); - newValues.put(currency, BigDecimal.valueOf(17.0)); - - currency = Currency.getInstance("SKK"); - newValues.put(currency, BigDecimal.valueOf(21.25)); - - // don't sycnhronize all of it because clients can use slightly out of - // date information. - synchronized(CurrencyValues.class) - { - values = newValues; - } - } - - /** - * Get the value of the specified currency in USD. - * - * @param currency the corrency to get. - * @return the value of the currency in USD. - * @throws IllegalArgumentException if currency is null. - */ - static BigDecimal getValue(final Currency currency) - { - final BigDecimal value; - - if(currency == null) - { - throw new IllegalArgumentException("currencyName cannot be null"); - } - - // make sure we are not updating the map right now - synchronized(CurrencyValues.class) - { - value = values.get(currency); - } - - return (value); - } - - /** - * Used to update the currency map periodically. - */ - private static class Refresher - extends TimerTask - { - /** - * call the refresh method. - */ - @Override - public void run() - { - refresh(); - } - } -}