1.1 --- a/task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java Tue Oct 07 11:05:34 2008 +0200
1.2 +++ b/task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 10 21:48:49 2008 +0200
1.3 @@ -20,19 +20,35 @@
1.4
1.5 private static Hashtable<String, ExchangeRate> exchangeRates;
1.6
1.7 - private Hashtable<String, ExchangeRate> convertorInstanceExchangeRates;
1.8 + private List<String> currencyConvertors;
1.9
1.10 - private Convertor(List<Currency> currencies) throws UnknownConvertorException {
1.11 - convertorInstanceExchangeRates = new Hashtable<String, ExchangeRate>();
1.12 - for (Currency currency1 : currencies) {
1.13 - for (Currency currency2 : currencies) {
1.14 - if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.15 - String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.16 - ExchangeRate exchangeRate = exchangeRates.get(key);
1.17 - convertorInstanceExchangeRates.put(key, exchangeRate);
1.18 - }
1.19 - }
1.20 - }
1.21 + /**
1.22 + * Constructor. Checks if all selected currencies are not null and has defined exchange rates for both
1.23 + * directions.
1.24 + * @param currencies currencies for new instance of convertor
1.25 + * @throws UnknownConvertorException if there are not defined exchange rates for both directions for each
1.26 + * pair of currencies
1.27 + */
1.28 + private Convertor(Currency[] currencies) throws UnknownConvertorException {
1.29 + currencyConvertors = new ArrayList<String>();
1.30 +
1.31 + for (Currency currency1 : currencies) {
1.32 + for (Currency currency2 : currencies) {
1.33 + if(currency1 == null || currency2 == null) {
1.34 + throw new ConvertorException("None of the currencies should be null!!!");
1.35 + }
1.36 +
1.37 + if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.38 + String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.39 + if (!exchangeRates.containsKey(key)) {
1.40 + throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
1.41 + + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
1.42 + }
1.43 +
1.44 + currencyConvertors.add(key);
1.45 + }
1.46 + }
1.47 + }
1.48 }
1.49
1.50 /**
1.51 @@ -77,7 +93,11 @@
1.52 * @return convertor with merged exchange rates
1.53 */
1.54 public Convertor merge(Convertor convertor) {
1.55 - convertorInstanceExchangeRates.putAll(convertor.getInstanceExchangeRates());
1.56 + if(convertor == null) {
1.57 + throw new ConvertorException("It's impossible to merge with null convertor!!!");
1.58 + }
1.59 +
1.60 + currencyConvertors.addAll(convertor.getCurrencyConvertors());
1.61 return this;
1.62 }
1.63
1.64 @@ -92,32 +112,11 @@
1.65 * thrown if convertor for selected currencies has not been defined
1.66 */
1.67 public static Convertor getConvertorInstance(Currency... currencies) throws UnknownConvertorException {
1.68 - List<Currency> currencyList = new ArrayList<Currency>();
1.69 -
1.70 if(currencies.length < 2) {
1.71 throw new ConvertorException("To get convertor instance, you have to select at least two currencies!!!");
1.72 }
1.73 -
1.74 - for (Currency currency1 : currencies) {
1.75 - for (Currency currency2 : currencies) {
1.76 - if(currency1 == null || currency2 == null) {
1.77 - throw new ConvertorException("None of the currencies should be null!!!");
1.78 - }
1.79 -
1.80 - if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.81 - String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.82 - String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
1.83 - if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
1.84 - throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "<->"
1.85 - + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
1.86 - }
1.87 - }
1.88 - }
1.89 -
1.90 - currencyList.add(currency1);
1.91 - }
1.92
1.93 - return new Convertor(currencyList);
1.94 + return new Convertor(currencies);
1.95 }
1.96
1.97 /**
1.98 @@ -163,8 +162,8 @@
1.99
1.100 String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode();
1.101
1.102 - if(convertorInstanceExchangeRates.containsKey(key)) {
1.103 - actualyUsedExchangeRate = convertorInstanceExchangeRates.get(key);
1.104 + if(currencyConvertors.contains(key)) {
1.105 + actualyUsedExchangeRate = exchangeRates.get(key);
1.106 } else {
1.107 throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
1.108 + newCurrency.getCurrencyCode() + ") !!!");
1.109 @@ -174,11 +173,11 @@
1.110 }
1.111
1.112 /**
1.113 - * Returns exchange rates for actual instance of convertor.
1.114 - * @return exchange rates for actual instance of convertor
1.115 + * Returns currency convertors for actual instance of convertor.
1.116 + * @return currency convertors for actual instance of convertor
1.117 */
1.118 - Hashtable<String, ExchangeRate> getInstanceExchangeRates() {
1.119 - return convertorInstanceExchangeRates;
1.120 + List<String> getCurrencyConvertors() {
1.121 + return currencyConvertors;
1.122 }
1.123
1.124 }