1.1 --- a/task3/solution11/src/org/apidesign/apifest08/currency/Convertor.java Tue Oct 07 11:05:34 2008 +0200
1.2 +++ b/task3/solution11/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 10 21:58:12 2008 +0200
1.3 @@ -20,31 +20,47 @@
1.4 * existing convertors into new convertor's instance.
1.5 * No time for javadoc these features, sorry.
1.6 *
1.7 + * In Task3's version supports reading of current exchange rates
1.8 + * from data sources. Data sources are merged during convertors' merging
1.9 + * as well as static exchange rates.
1.10 + * No time for javadoc, again.
1.11 + *
1.12 * @author ked
1.13 */
1.14 public final class Convertor<AmountType, IdentifierType> {
1.15
1.16 Computer<AmountType> computer;
1.17 + // each static exchange rate could be a special case of an exchange rate data source
1.18 List<ExchangeRateValue<AmountType, IdentifierType>> exchangeRates = new ArrayList<ExchangeRateValue<AmountType, IdentifierType>>();
1.19 + List<ExchangeRateDataSource<AmountType, IdentifierType>> exchangeRateDataSources = new ArrayList<ExchangeRateDataSource<AmountType, IdentifierType>>();
1.20
1.21 - Convertor(
1.22 - Computer<AmountType> computer,
1.23 - Collection<ExchangeRateValue<AmountType, IdentifierType>> exchangeRates) {
1.24 + Convertor(Computer<AmountType> computer) {
1.25 this.computer = computer;
1.26 -
1.27 + }
1.28 +
1.29 + void addExchangeRates(Collection<ExchangeRateValue<AmountType, IdentifierType>> exchangeRates) {
1.30 for (ExchangeRateValue<AmountType, IdentifierType> exchangeRate : exchangeRates) {
1.31 - if (findExchangeRate(
1.32 - this.exchangeRates,
1.33 + if (isExchangeRate(
1.34 exchangeRate.getCurrencyA().getIdentifier(),
1.35 - exchangeRate.getCurrencyB().getIdentifier()) != null) {
1.36 + exchangeRate.getCurrencyB().getIdentifier())) {
1.37 throw new IllegalArgumentException("Duplicate exchange rate!");
1.38 }
1.39 this.exchangeRates.add(exchangeRate);
1.40 }
1.41 }
1.42 -
1.43 - private ExchangeRateValue<AmountType, IdentifierType> findExchangeRate(
1.44 - Collection<ExchangeRateValue<AmountType, IdentifierType>> exchangeRates,
1.45 +
1.46 + void addExchangeRateDataSources(Collection<ExchangeRateDataSource<AmountType, IdentifierType>> exchangeRateDataSources) {
1.47 + for (ExchangeRateDataSource<AmountType, IdentifierType> exchangeRateDataSource : exchangeRateDataSources) {
1.48 + if (isExchangeRate(
1.49 + exchangeRateDataSource.getCurrencyAIdentifier(),
1.50 + exchangeRateDataSource.getCurrencyBIdentifier())) {
1.51 + throw new IllegalArgumentException("Duplicate exchange rate!");
1.52 + }
1.53 + this.exchangeRateDataSources.add(exchangeRateDataSource);
1.54 + }
1.55 + }
1.56 +
1.57 + ExchangeRateValue<AmountType, IdentifierType> findExchangeRate(
1.58 IdentifierType currencyA,
1.59 IdentifierType currencyB) {
1.60 for (ExchangeRateValue<AmountType, IdentifierType> exchangeRate : exchangeRates) {
1.61 @@ -53,8 +69,32 @@
1.62 return exchangeRate;
1.63 }
1.64 }
1.65 + for (ExchangeRateDataSource<AmountType, IdentifierType> exchangeRateDataSource : exchangeRateDataSources) {
1.66 + if ((exchangeRateDataSource.getCurrencyAIdentifier().equals(currencyA) && exchangeRateDataSource.getCurrencyBIdentifier().equals(currencyB)) ||
1.67 + (exchangeRateDataSource.getCurrencyAIdentifier().equals(currencyB) && exchangeRateDataSource.getCurrencyBIdentifier().equals(currencyA))) {
1.68 + return exchangeRateDataSource.getExchangeRate();
1.69 + }
1.70 + }
1.71 return null;
1.72 }
1.73 +
1.74 + boolean isExchangeRate(
1.75 + IdentifierType currencyA,
1.76 + IdentifierType currencyB) {
1.77 + for (ExchangeRateValue<AmountType, IdentifierType> exchangeRate : exchangeRates) {
1.78 + if ((exchangeRate.getCurrencyA().getIdentifier().equals(currencyA) && exchangeRate.getCurrencyB().getIdentifier().equals(currencyB)) ||
1.79 + (exchangeRate.getCurrencyA().getIdentifier().equals(currencyB) && exchangeRate.getCurrencyB().getIdentifier().equals(currencyA))) {
1.80 + return true;
1.81 + }
1.82 + }
1.83 + for (ExchangeRateDataSource<AmountType, IdentifierType> exchangeRateDataSource : exchangeRateDataSources) {
1.84 + if ((exchangeRateDataSource.getCurrencyAIdentifier().equals(currencyA) && exchangeRateDataSource.getCurrencyBIdentifier().equals(currencyB)) ||
1.85 + (exchangeRateDataSource.getCurrencyAIdentifier().equals(currencyB) && exchangeRateDataSource.getCurrencyBIdentifier().equals(currencyA))) {
1.86 + return true;
1.87 + }
1.88 + }
1.89 + return false;
1.90 + }
1.91
1.92 /**
1.93 * Convert an amount of the one currency to an amount of the another one currency
1.94 @@ -68,7 +108,7 @@
1.95 IdentifierType targetCurrency,
1.96 CurrencyValue<AmountType, IdentifierType> currencyValue) {
1.97 ExchangeRateValue<AmountType, IdentifierType> exchangeRate =
1.98 - findExchangeRate(exchangeRates, currencyValue.getIdentifier(), targetCurrency);
1.99 + findExchangeRate(currencyValue.getIdentifier(), targetCurrency);
1.100 if (exchangeRate == null) {
1.101 throw new IllegalArgumentException("Inappropriate currencies to convert!");
1.102 }
1.103 @@ -87,7 +127,9 @@
1.104 targetCurrencyRef = exchangeRate.getCurrencyB().getIdentifier();
1.105 }
1.106
1.107 - ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
1.108 + ComputerResponse<AmountType> computerResponse = new ComputerResponse<AmountType>();
1.109 + computer.compute(computerRequest, computerResponse);
1.110 +
1.111 return CurrencyValue.getCurrencyValue(
1.112 computerResponse.getResult(),
1.113 targetCurrencyRef);
1.114 @@ -100,10 +142,18 @@
1.115 Computer<AmountType> computer,
1.116 Collection<Convertor<AmountType, IdentifierType>> convertors) {
1.117 Set<ExchangeRateValue<AmountType, IdentifierType>> exchangeRatesSet = new HashSet<ExchangeRateValue<AmountType, IdentifierType>>();
1.118 + Set<ExchangeRateDataSource<AmountType, IdentifierType>> exchangeRateDataSourcesSet = new HashSet<ExchangeRateDataSource<AmountType, IdentifierType>>();
1.119 for (Convertor<AmountType, IdentifierType> convertor : convertors) {
1.120 exchangeRatesSet.addAll(convertor.exchangeRates);
1.121 }
1.122 - return getConvertor(computer, exchangeRatesSet);
1.123 + for (Convertor<AmountType, IdentifierType> convertor : convertors) {
1.124 + exchangeRateDataSourcesSet.addAll(convertor.exchangeRateDataSources);
1.125 + }
1.126 +
1.127 + Convertor<AmountType, IdentifierType> c = new Convertor<AmountType, IdentifierType>(computer);
1.128 + c.addExchangeRates(exchangeRatesSet);
1.129 + c.addExchangeRateDataSources(exchangeRateDataSourcesSet);
1.130 + return c;
1.131 }
1.132
1.133 static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> mergeConvertors(
1.134 @@ -144,9 +194,18 @@
1.135 // ---
1.136 static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
1.137 Computer<AmountType> computer, Collection<ExchangeRateValue<AmountType, IdentifierType>> exchangeRates) {
1.138 - return new Convertor<AmountType, IdentifierType>(computer, exchangeRates);
1.139 + Convertor<AmountType, IdentifierType> c = new Convertor<AmountType, IdentifierType>(computer);
1.140 + c.addExchangeRates(exchangeRates);
1.141 + return c;
1.142 }
1.143
1.144 + static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertorDataSource(
1.145 + Computer<AmountType> computer, Collection<ExchangeRateDataSource<AmountType, IdentifierType>> exchangeRateDataSources) {
1.146 + Convertor<AmountType, IdentifierType> c = new Convertor<AmountType, IdentifierType>(computer);
1.147 + c.addExchangeRateDataSources(exchangeRateDataSources);
1.148 + return c;
1.149 + }
1.150 +
1.151 static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
1.152 Computer<AmountType> computer, ExchangeRateValue<AmountType, IdentifierType> exchangeRate) {
1.153 Collection<ExchangeRateValue<AmountType, IdentifierType>> exchangeRates =
1.154 @@ -155,6 +214,14 @@
1.155 return getConvertor(computer, exchangeRates);
1.156 }
1.157
1.158 + static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertorDataSource(
1.159 + Computer<AmountType> computer, ExchangeRateDataSource<AmountType, IdentifierType> exchangeRateDataSource) {
1.160 + Collection<ExchangeRateDataSource<AmountType, IdentifierType>> exchangeRateDataSources =
1.161 + new ArrayList<ExchangeRateDataSource<AmountType, IdentifierType>>();
1.162 + exchangeRateDataSources.add(exchangeRateDataSource);
1.163 + return getConvertorDataSource(computer, exchangeRateDataSources);
1.164 + }
1.165 +
1.166 public static Convertor<Double, String> getConvertorDoubleString(
1.167 Collection<ExchangeRateValue<Double, String>> exchangeRates) {
1.168 return getConvertor(DoubleComputer, exchangeRates);
1.169 @@ -164,7 +231,17 @@
1.170 ExchangeRateValue<Double, String> exchangeRate) {
1.171 return getConvertor(DoubleComputer, exchangeRate);
1.172 }
1.173 +
1.174 + public static Convertor<Double, String> getConvertorDataSourceDoubleString(
1.175 + Collection<ExchangeRateDataSource<Double, String>> exchangeRateDataSources) {
1.176 + return getConvertorDataSource(DoubleComputer, exchangeRateDataSources);
1.177 + }
1.178
1.179 + public static Convertor<Double, String> getConvertorDataSourceDoubleString(
1.180 + ExchangeRateDataSource<Double, String> exchangeRateDataSource) {
1.181 + return getConvertorDataSource(DoubleComputer, exchangeRateDataSource);
1.182 + }
1.183 +
1.184 public static Convertor<Integer, String> getConvertorIntegerString(
1.185 Collection<ExchangeRateValue<Integer, String>> exchangeRates) {
1.186 return getConvertor(IntegerComputer, exchangeRates);
1.187 @@ -174,6 +251,16 @@
1.188 ExchangeRateValue<Integer, String> exchangeRate) {
1.189 return getConvertor(IntegerComputer, exchangeRate);
1.190 }
1.191 +
1.192 + public static Convertor<Integer, String> getConvertorDataSourceIntegerString(
1.193 + Collection<ExchangeRateDataSource<Integer, String>> exchangeRateDataSources) {
1.194 + return getConvertorDataSource(IntegerComputer, exchangeRateDataSources);
1.195 + }
1.196 +
1.197 + public static Convertor<Integer, String> getConvertorDataSourceIntegerString(
1.198 + ExchangeRateDataSource<Integer, String> exchangeRateDataSource) {
1.199 + return getConvertorDataSource(IntegerComputer, exchangeRateDataSource);
1.200 + }
1.201
1.202 // ---
1.203 // BACKWARD COMPATIBILITY - CREATION
1.204 @@ -211,18 +298,14 @@
1.205 // ---
1.206 static final Computer<Double> DoubleComputer = new Computer<Double>() {
1.207
1.208 - public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
1.209 - ComputerResponse<Double> response = new ComputerResponse<Double>();
1.210 + public void compute(ComputerRequest<Double> request, ComputerResponse<Double> response) {
1.211 response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
1.212 - return response;
1.213 }
1.214 };
1.215 static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
1.216
1.217 - public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
1.218 - ComputerResponse<Integer> response = new ComputerResponse<Integer>();
1.219 + public void compute(ComputerRequest<Integer> request, ComputerResponse<Integer> response) {
1.220 response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
1.221 - return response;
1.222 }
1.223 };
1.224 }