task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java
changeset 72 c6b50876b5cf
parent 71 978f6a78c22e
child 73 e8b0f13fd4fb
child 80 067f86d76ac7
     1.1 --- a/task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Fri Oct 17 17:54:38 2008 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,297 +0,0 @@
     1.4 -package org.apidesign.apifest08.currency;
     1.5 -
     1.6 -import java.util.ArrayList;
     1.7 -import java.util.Calendar;
     1.8 -import java.util.Currency;
     1.9 -import java.util.Date;
    1.10 -import java.util.Hashtable;
    1.11 -import java.util.List;
    1.12 -
    1.13 -import org.apidesign.apifest08.currency.exceptions.ConvertorException;
    1.14 -import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
    1.15 -import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
    1.16 -
    1.17 -/**
    1.18 - * This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
    1.19 - * (currently in Task1Test.java) file.
    1.20 - * <p>
    1.21 - * Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
    1.22 - * package. Do not spread it outside to other packages.
    1.23 - */
    1.24 -public class Convertor {
    1.25 -
    1.26 -  private static Hashtable<String, List<ExchangeRate>> exchangeRates;
    1.27 -
    1.28 -  private List<ExchangeRateInstance> exchangeRateInstances;
    1.29 -
    1.30 -  /**
    1.31 -   * Constructor. Checks if all selected currencies are not null and has defined exchange rates for both
    1.32 -   * directions.
    1.33 -   * @param currencies currencies for new instance of convertor
    1.34 -   * @throws UnknownConvertorException if there are not defined exchange rates for both directions for each
    1.35 -   * pair of currencies
    1.36 -   */
    1.37 -  private Convertor(Currency[] currencies) throws UnknownConvertorException {
    1.38 -    exchangeRateInstances = new ArrayList<ExchangeRateInstance>();
    1.39 -
    1.40 -    for (Currency currency1 : currencies) {
    1.41 -      for (Currency currency2 : currencies) {
    1.42 -        if(currency1 == null || currency2 == null) {
    1.43 -          throw new ConvertorException("None of the currencies should be null!!!");
    1.44 -        }
    1.45 -          
    1.46 -        if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
    1.47 -          String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.48 -          if (!exchangeRates.containsKey(key)) {
    1.49 -            throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
    1.50 -                + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
    1.51 -          }
    1.52 -          
    1.53 -          exchangeRateInstances.add(new ExchangeRateInstance(key, null, null));            
    1.54 -        }
    1.55 -      }
    1.56 -    }
    1.57 -  }
    1.58 -
    1.59 -  /**
    1.60 -   * Sets convertor rate for selected currencies.
    1.61 -   * @param currency1
    1.62 -   *          one of the currencies we want to convert to/from
    1.63 -   * @param currency2
    1.64 -   *          the other currency
    1.65 -   * @param rate
    1.66 -   *          exchange rate from currency1 to currency2
    1.67 -   * @param unit
    1.68 -   *          unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
    1.69 -   *          100SKK)
    1.70 -   */
    1.71 -  public static void setConvertorRates(Currency currency1, Currency currency2, double rate, double unit) {
    1.72 -    if (currency1 == null || currency2 == null) {
    1.73 -      throw new ConvertorException("None of the currencies should be null!!!");
    1.74 -    }
    1.75 -
    1.76 -    if (rate <= 0 || unit <= 0) {
    1.77 -      throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
    1.78 -    }
    1.79 -
    1.80 -    if (exchangeRates == null) {
    1.81 -      exchangeRates = new Hashtable<String, List<ExchangeRate>>();
    1.82 -    }
    1.83 -
    1.84 -    String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.85 -    String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    1.86 -    double recountedRate = (unit / rate) * unit;
    1.87 -
    1.88 -    exchangeRates.put(key12, addNewExchangeRate(key12, currency1, currency2, rate, unit, null, null));
    1.89 -    exchangeRates.put(key21, addNewExchangeRate(key21, currency2, currency1, recountedRate, unit, null, null));
    1.90 -  }
    1.91 -  
    1.92 -  public static Convertor limitExchangeRatesValidity(Convertor convertor, Date validFrom, Date validTo) {
    1.93 -    if(convertor == null || validFrom == null || validTo == null) {
    1.94 -      throw new ConvertorException("None of parameters of method limitExchangeRatesValidity should be null!!!");
    1.95 -    }
    1.96 -    
    1.97 -    List<ExchangeRateInstance> exchangeRateInstances = convertor.getExchangeRateInstances();
    1.98 -    for (ExchangeRateInstance exchangeRateInstance : exchangeRateInstances) {
    1.99 -      // get actual convertor rates for actual validity
   1.100 -      ExchangeRate actualExchangeRate = getExchangeRate(exchangeRateInstance.getKey(), exchangeRateInstance.getValidFrom(), exchangeRateInstance.getValidTo());
   1.101 -      // set new validity for theese rates
   1.102 -      actualExchangeRate.setValidFrom(validFrom);
   1.103 -      actualExchangeRate.setValidTo(validTo);
   1.104 -      // and for selected currency convertor
   1.105 -      exchangeRateInstance.setValidFrom(validFrom);
   1.106 -      exchangeRateInstance.setValidTo(substractSecond(validTo));
   1.107 -    }
   1.108 -
   1.109 -    return convertor;
   1.110 -  }
   1.111 -  
   1.112 -  /**
   1.113 -   * Merge exchange rates of actual convertor with exchange rates from selected 
   1.114 -   * convertor. If there are same currencies in both convertors, these from selected
   1.115 -   * convertor rewrites these in actual convertor.
   1.116 -   * @param convertor convertor to merge with actual one
   1.117 -   * @return convertor with merged exchange rates 
   1.118 -   */
   1.119 -  public Convertor merge(Convertor convertor) {
   1.120 -    if(convertor == null) {
   1.121 -      throw new ConvertorException("It's impossible to merge with null convertor!!!");
   1.122 -    }
   1.123 -    
   1.124 -    exchangeRateInstances.addAll(convertor.getExchangeRateInstances());
   1.125 -	  return this;
   1.126 -  }
   1.127 -
   1.128 -  /**
   1.129 -   * Creates new instance of convertor.
   1.130 -   * @param currency1
   1.131 -   *          one of the currencies we want to convert to/from
   1.132 -   * @param currency2
   1.133 -   *          the other currency
   1.134 -   * @return new instance of convertor
   1.135 -   * @throws UnknownConvertorException
   1.136 -   *           thrown if convertor for selected currencies has not been defined
   1.137 -   */
   1.138 -  public static Convertor getConvertorInstance(Currency... currencies) throws UnknownConvertorException {
   1.139 -   	if(currencies.length < 2) {
   1.140 -   		throw new ConvertorException("To get convertor instance, you have to select at least two currencies!!!");
   1.141 -   	}
   1.142 -    
   1.143 -    return new Convertor(currencies);
   1.144 -  }
   1.145 -
   1.146 -  /**
   1.147 -   * Converts selected amout of selected currency to other currency of this convertor instance.
   1.148 -   * @param amount
   1.149 -   *          amount to convert
   1.150 -   * @param originalCurrency
   1.151 -   *          currency of this amount
   1.152 -   * @param newCurrency
   1.153 -   *          currency to which we want convert
   1.154 -   * @return converted amount
   1.155 -   * @throws InvalidCurrencyException
   1.156 -   *           while one or both currencies doesn't fit for this convertor
   1.157 -   */
   1.158 -  public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   1.159 -    ExchangeRate actualyUsedExchangeRate = null;
   1.160 -
   1.161 -    if (originalCurrency == null) {
   1.162 -      throw new ConvertorException("Original currency is null!!!");
   1.163 -    }
   1.164 -
   1.165 -    if (newCurrency == null) {
   1.166 -      throw new ConvertorException("Destination currency is null!!!");
   1.167 -    }
   1.168 -
   1.169 -    actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
   1.170 -
   1.171 -    return countResult(actualyUsedExchangeRate, amount);
   1.172 -  }
   1.173 -
   1.174 -  private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
   1.175 -    return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
   1.176 -  }
   1.177 -
   1.178 -  /**
   1.179 -   * Decides the direction of conversion and returns actual exchange rate
   1.180 -   * for selected currencies and actual moment.
   1.181 -   * @param actualCurrency
   1.182 -   *          actual currency we want to convert
   1.183 -   * @return actual exchange rate of this convertor for selected currency
   1.184 -   */
   1.185 -  private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   1.186 -    ExchangeRate actualyUsedExchangeRate = null;
   1.187 -    
   1.188 -    String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode();
   1.189 -
   1.190 -    ExchangeRateInstance exchangeRateInstance = findExchangeRateInstance(key);
   1.191 -    if(exchangeRateInstance != null) {
   1.192 -      actualyUsedExchangeRate = getExchangeRate(exchangeRateInstance.getKey(), exchangeRateInstance.getValidFrom(), exchangeRateInstance.getValidTo());
   1.193 -    } else {
   1.194 -      throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
   1.195 -          + newCurrency.getCurrencyCode() + ") !!!");
   1.196 -    }
   1.197 -
   1.198 -    return actualyUsedExchangeRate;
   1.199 -  }
   1.200 -  
   1.201 -  /**
   1.202 -   * Finds instance of exchange rate for actual instance of convertor and actual moment.
   1.203 -   * @param key exchange rate instance key
   1.204 -   * @return exchange rate instance
   1.205 -   */
   1.206 -  private ExchangeRateInstance findExchangeRateInstance(String key) {
   1.207 -    ExchangeRateInstance instance = null;
   1.208 -    
   1.209 -    Date now = new Date();
   1.210 -    for (ExchangeRateInstance item : exchangeRateInstances) {
   1.211 -      if(item.getKey().equals(key) && item.getValidFrom() == null && item.getValidTo() == null) {
   1.212 -        instance = item;
   1.213 -        break;
   1.214 -      } else if(item.getKey().equals(key) && item.getValidFrom().compareTo(now) <= 0 && item.getValidTo().compareTo(now) >= 0) {
   1.215 -        instance = item;
   1.216 -        break;
   1.217 -      }
   1.218 -    }
   1.219 -    
   1.220 -    return instance;
   1.221 -  }
   1.222 -  
   1.223 -  
   1.224 -  /**
   1.225 -   * Returns currency convertors for actual instance of convertor.
   1.226 -   * @return currency convertors for actual instance of convertor
   1.227 -   */
   1.228 -  private List<ExchangeRateInstance> getExchangeRateInstances() {
   1.229 -    return exchangeRateInstances;
   1.230 -  }
   1.231 -  
   1.232 -  private static ExchangeRate getExchangeRate(String key, Date validFrom, Date validTo) {
   1.233 -    ExchangeRate exchangeRate = null;
   1.234 -    
   1.235 -    List<ExchangeRate> currencyExchangeRates = exchangeRates.get(key);
   1.236 -    for (ExchangeRate currencyExchangeRate : currencyExchangeRates) {
   1.237 -      Date actValidFrom = currencyExchangeRate.getValidFrom();
   1.238 -      Date actValidTo = currencyExchangeRate.getValidTo();
   1.239 -      
   1.240 -      if(actValidFrom == null && validFrom == null && actValidTo == null && validTo == null) {
   1.241 -        exchangeRate = currencyExchangeRate;
   1.242 -        break;
   1.243 -      } else if(actValidFrom != null && validFrom != null && actValidTo != null && validTo != null && actValidFrom.compareTo(validFrom) <= 0 && actValidTo.compareTo(validTo) > 0) {
   1.244 -        exchangeRate = currencyExchangeRate;
   1.245 -        break;
   1.246 -      }
   1.247 -    }
   1.248 -    
   1.249 -    if(exchangeRate == null) {
   1.250 -      throw new ConvertorException("Exchange rate for actual exchange rate instance not found!!!");
   1.251 -    }
   1.252 -    
   1.253 -    return exchangeRate;
   1.254 -  }
   1.255 -  
   1.256 -  private static List<ExchangeRate> addNewExchangeRate(String key, Currency currency1, Currency currency2, double rate, double unit, Date validFrom, Date validTo) {
   1.257 -    List<ExchangeRate> actualExchangeRates = new ArrayList<ExchangeRate>();
   1.258 -    ExchangeRate newExchangeRate = null;
   1.259 -    
   1.260 -    if(exchangeRates.containsKey(key)) {
   1.261 -      List<ExchangeRate> rates = exchangeRates.get(key);
   1.262 -      for (ExchangeRate exchangeRate : rates) {
   1.263 -        if(exchangeRate.getValidFrom() == null && exchangeRate.getValidTo() == null && validFrom == null && validTo == null) {
   1.264 -          newExchangeRate = exchangeRate;
   1.265 -          break;
   1.266 -        } else if(exchangeRate.getValidFrom() != null && exchangeRate.getValidTo() != null && validFrom != null && validTo != null) {
   1.267 -          if(exchangeRate.getValidFrom().compareTo(validFrom) == 0 && exchangeRate.getValidTo().compareTo(validTo) == 0) {
   1.268 -            newExchangeRate = exchangeRate;
   1.269 -            break;
   1.270 -          }
   1.271 -        }
   1.272 -      }
   1.273 -      actualExchangeRates.addAll(rates);
   1.274 -    }
   1.275 -    
   1.276 -    if(newExchangeRate == null) {
   1.277 -      actualExchangeRates.add(new ExchangeRate(currency1, currency2, rate, unit, validFrom, validTo));
   1.278 -    } else {
   1.279 -      newExchangeRate.setRate(rate);
   1.280 -      newExchangeRate.setUnit(unit);
   1.281 -      actualExchangeRates.add(newExchangeRate);
   1.282 -    }
   1.283 -    
   1.284 -    return actualExchangeRates;
   1.285 -  }
   1.286 -  
   1.287 -  /**
   1.288 -   * Substracts one second from selected date.
   1.289 -   * @param date date
   1.290 -   * @return date -1s
   1.291 -   */
   1.292 -  private static Date substractSecond(Date date) {
   1.293 -    Calendar calendar = Calendar.getInstance();
   1.294 -    calendar.setTime(date);
   1.295 -    calendar.set(Calendar.SECOND, -1);
   1.296 -    
   1.297 -    return calendar.getTime();
   1.298 -  }
   1.299 -  
   1.300 -}