diff -r 978f6a78c22e -r c6b50876b5cf task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java
--- a/task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 17 17:54:38 2008 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,297 +0,0 @@
-package org.apidesign.apifest08.currency;
-
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Currency;
-import java.util.Date;
-import java.util.Hashtable;
-import java.util.List;
-
-import org.apidesign.apifest08.currency.exceptions.ConvertorException;
-import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
-import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
-
-/**
- * This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
- * (currently in Task1Test.java) file.
- *
- * Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
- * package. Do not spread it outside to other packages.
- */
-public class Convertor {
-
- private static Hashtable> exchangeRates;
-
- private List exchangeRateInstances;
-
- /**
- * Constructor. Checks if all selected currencies are not null and has defined exchange rates for both
- * directions.
- * @param currencies currencies for new instance of convertor
- * @throws UnknownConvertorException if there are not defined exchange rates for both directions for each
- * pair of currencies
- */
- private Convertor(Currency[] currencies) throws UnknownConvertorException {
- exchangeRateInstances = new ArrayList();
-
- for (Currency currency1 : currencies) {
- for (Currency currency2 : currencies) {
- if(currency1 == null || currency2 == null) {
- throw new ConvertorException("None of the currencies should be null!!!");
- }
-
- if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
- String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
- if (!exchangeRates.containsKey(key)) {
- throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
- + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
- }
-
- exchangeRateInstances.add(new ExchangeRateInstance(key, null, null));
- }
- }
- }
- }
-
- /**
- * Sets convertor rate for selected currencies.
- * @param currency1
- * one of the currencies we want to convert to/from
- * @param currency2
- * the other currency
- * @param rate
- * exchange rate from currency1 to currency2
- * @param unit
- * unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
- * 100SKK)
- */
- public static void setConvertorRates(Currency currency1, Currency currency2, double rate, double unit) {
- if (currency1 == null || currency2 == null) {
- throw new ConvertorException("None of the currencies should be null!!!");
- }
-
- if (rate <= 0 || unit <= 0) {
- throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
- }
-
- if (exchangeRates == null) {
- exchangeRates = new Hashtable>();
- }
-
- String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
- String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
- double recountedRate = (unit / rate) * unit;
-
- exchangeRates.put(key12, addNewExchangeRate(key12, currency1, currency2, rate, unit, null, null));
- exchangeRates.put(key21, addNewExchangeRate(key21, currency2, currency1, recountedRate, unit, null, null));
- }
-
- public static Convertor limitExchangeRatesValidity(Convertor convertor, Date validFrom, Date validTo) {
- if(convertor == null || validFrom == null || validTo == null) {
- throw new ConvertorException("None of parameters of method limitExchangeRatesValidity should be null!!!");
- }
-
- List exchangeRateInstances = convertor.getExchangeRateInstances();
- for (ExchangeRateInstance exchangeRateInstance : exchangeRateInstances) {
- // get actual convertor rates for actual validity
- ExchangeRate actualExchangeRate = getExchangeRate(exchangeRateInstance.getKey(), exchangeRateInstance.getValidFrom(), exchangeRateInstance.getValidTo());
- // set new validity for theese rates
- actualExchangeRate.setValidFrom(validFrom);
- actualExchangeRate.setValidTo(validTo);
- // and for selected currency convertor
- exchangeRateInstance.setValidFrom(validFrom);
- exchangeRateInstance.setValidTo(substractSecond(validTo));
- }
-
- return convertor;
- }
-
- /**
- * Merge exchange rates of actual convertor with exchange rates from selected
- * convertor. If there are same currencies in both convertors, these from selected
- * convertor rewrites these in actual convertor.
- * @param convertor convertor to merge with actual one
- * @return convertor with merged exchange rates
- */
- public Convertor merge(Convertor convertor) {
- if(convertor == null) {
- throw new ConvertorException("It's impossible to merge with null convertor!!!");
- }
-
- exchangeRateInstances.addAll(convertor.getExchangeRateInstances());
- return this;
- }
-
- /**
- * Creates new instance of convertor.
- * @param currency1
- * one of the currencies we want to convert to/from
- * @param currency2
- * the other currency
- * @return new instance of convertor
- * @throws UnknownConvertorException
- * thrown if convertor for selected currencies has not been defined
- */
- public static Convertor getConvertorInstance(Currency... currencies) throws UnknownConvertorException {
- if(currencies.length < 2) {
- throw new ConvertorException("To get convertor instance, you have to select at least two currencies!!!");
- }
-
- return new Convertor(currencies);
- }
-
- /**
- * Converts selected amout of selected currency to other currency of this convertor instance.
- * @param amount
- * amount to convert
- * @param originalCurrency
- * currency of this amount
- * @param newCurrency
- * currency to which we want convert
- * @return converted amount
- * @throws InvalidCurrencyException
- * while one or both currencies doesn't fit for this convertor
- */
- public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
- ExchangeRate actualyUsedExchangeRate = null;
-
- if (originalCurrency == null) {
- throw new ConvertorException("Original currency is null!!!");
- }
-
- if (newCurrency == null) {
- throw new ConvertorException("Destination currency is null!!!");
- }
-
- actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
-
- return countResult(actualyUsedExchangeRate, amount);
- }
-
- private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
- return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
- }
-
- /**
- * Decides the direction of conversion and returns actual exchange rate
- * for selected currencies and actual moment.
- * @param actualCurrency
- * actual currency we want to convert
- * @return actual exchange rate of this convertor for selected currency
- */
- private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
- ExchangeRate actualyUsedExchangeRate = null;
-
- String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode();
-
- ExchangeRateInstance exchangeRateInstance = findExchangeRateInstance(key);
- if(exchangeRateInstance != null) {
- actualyUsedExchangeRate = getExchangeRate(exchangeRateInstance.getKey(), exchangeRateInstance.getValidFrom(), exchangeRateInstance.getValidTo());
- } else {
- throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
- + newCurrency.getCurrencyCode() + ") !!!");
- }
-
- return actualyUsedExchangeRate;
- }
-
- /**
- * Finds instance of exchange rate for actual instance of convertor and actual moment.
- * @param key exchange rate instance key
- * @return exchange rate instance
- */
- private ExchangeRateInstance findExchangeRateInstance(String key) {
- ExchangeRateInstance instance = null;
-
- Date now = new Date();
- for (ExchangeRateInstance item : exchangeRateInstances) {
- if(item.getKey().equals(key) && item.getValidFrom() == null && item.getValidTo() == null) {
- instance = item;
- break;
- } else if(item.getKey().equals(key) && item.getValidFrom().compareTo(now) <= 0 && item.getValidTo().compareTo(now) >= 0) {
- instance = item;
- break;
- }
- }
-
- return instance;
- }
-
-
- /**
- * Returns currency convertors for actual instance of convertor.
- * @return currency convertors for actual instance of convertor
- */
- private List getExchangeRateInstances() {
- return exchangeRateInstances;
- }
-
- private static ExchangeRate getExchangeRate(String key, Date validFrom, Date validTo) {
- ExchangeRate exchangeRate = null;
-
- List currencyExchangeRates = exchangeRates.get(key);
- for (ExchangeRate currencyExchangeRate : currencyExchangeRates) {
- Date actValidFrom = currencyExchangeRate.getValidFrom();
- Date actValidTo = currencyExchangeRate.getValidTo();
-
- if(actValidFrom == null && validFrom == null && actValidTo == null && validTo == null) {
- exchangeRate = currencyExchangeRate;
- break;
- } else if(actValidFrom != null && validFrom != null && actValidTo != null && validTo != null && actValidFrom.compareTo(validFrom) <= 0 && actValidTo.compareTo(validTo) > 0) {
- exchangeRate = currencyExchangeRate;
- break;
- }
- }
-
- if(exchangeRate == null) {
- throw new ConvertorException("Exchange rate for actual exchange rate instance not found!!!");
- }
-
- return exchangeRate;
- }
-
- private static List addNewExchangeRate(String key, Currency currency1, Currency currency2, double rate, double unit, Date validFrom, Date validTo) {
- List actualExchangeRates = new ArrayList();
- ExchangeRate newExchangeRate = null;
-
- if(exchangeRates.containsKey(key)) {
- List rates = exchangeRates.get(key);
- for (ExchangeRate exchangeRate : rates) {
- if(exchangeRate.getValidFrom() == null && exchangeRate.getValidTo() == null && validFrom == null && validTo == null) {
- newExchangeRate = exchangeRate;
- break;
- } else if(exchangeRate.getValidFrom() != null && exchangeRate.getValidTo() != null && validFrom != null && validTo != null) {
- if(exchangeRate.getValidFrom().compareTo(validFrom) == 0 && exchangeRate.getValidTo().compareTo(validTo) == 0) {
- newExchangeRate = exchangeRate;
- break;
- }
- }
- }
- actualExchangeRates.addAll(rates);
- }
-
- if(newExchangeRate == null) {
- actualExchangeRates.add(new ExchangeRate(currency1, currency2, rate, unit, validFrom, validTo));
- } else {
- newExchangeRate.setRate(rate);
- newExchangeRate.setUnit(unit);
- actualExchangeRates.add(newExchangeRate);
- }
-
- return actualExchangeRates;
- }
-
- /**
- * Substracts one second from selected date.
- * @param date date
- * @return date -1s
- */
- private static Date substractSecond(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.set(Calendar.SECOND, -1);
-
- return calendar.getTime();
- }
-
-}