1.1 --- a/task4/solution14/src/org/apidesign/apifest08/currency/Convertor.java Sat Oct 11 23:38:46 2008 +0200
1.2 +++ b/task4/solution14/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 17 17:35:52 2008 +0200
1.3 @@ -3,6 +3,7 @@
1.4 import java.util.ArrayList;
1.5 import java.util.Collection;
1.6 import java.util.Collections;
1.7 +import java.util.Date;
1.8 import java.util.HashSet;
1.9 import java.util.List;
1.10 import java.util.Set;
1.11 @@ -22,7 +23,7 @@
1.12 private Rate rate = null;
1.13
1.14 //version 2 field
1.15 - private List<CurrencyRate> currencyRates = null;
1.16 + private List<TimeLimitedCurrencyRate> currencyRates = null;
1.17
1.18 //version - for compatible mode
1.19 private int instanceVersion = 0; //compatible mode because of problem with empty currency and CZE -> CZE (1:2) rate
1.20 @@ -47,9 +48,9 @@
1.21 throw new IllegalArgumentException("CurrencyRates cannot be empty.");
1.22 }
1.23 Set<Pair<String,String>> currencies = new HashSet<Pair<String,String>>();
1.24 - List<CurrencyRate> curRates = new ArrayList<CurrencyRate>();
1.25 + List<TimeLimitedCurrencyRate> curRates = new ArrayList<TimeLimitedCurrencyRate>();
1.26 for (int i = 0; i < currencyRate.length; i++) {
1.27 - CurrencyRate curRat = currencyRate[i];
1.28 + final CurrencyRate curRat = currencyRate[i];
1.29 if (curRat == null) {
1.30 throw new IllegalArgumentException("Parameter cannot be null.");
1.31 }
1.32 @@ -59,13 +60,66 @@
1.33 throw new IllegalArgumentException("Pair of currencies in a currency rate cannot be defined twice");
1.34 }
1.35 currencies.add(curPair);
1.36 -
1.37 +
1.38 + if (curRat instanceof TimeLimitedCurrencyRate) {
1.39 + curRates.add((TimeLimitedCurrencyRate)curRat);
1.40 + } else {
1.41 + curRates.add(new TimeLimitedCurrencyRate() { //create delegate which implements TimeLimitedCurrencyRate
1.42 + public long getFromTime() {
1.43 + return Long.MIN_VALUE;
1.44 + }
1.45 +
1.46 + public long getToTime() {
1.47 + return Long.MAX_VALUE;
1.48 + }
1.49 +
1.50 + public String getCurrency1() {
1.51 + return curRat.getCurrency1();
1.52 + }
1.53 +
1.54 + public String getCurrency2() {
1.55 + return curRat.getCurrency2();
1.56 + }
1.57 +
1.58 + public Rate getRate() {
1.59 + return curRat.getRate();
1.60 + }
1.61 + });
1.62 + }
1.63 + }
1.64 + this.currencyRates = Collections.unmodifiableList(curRates);
1.65 + }
1.66 +
1.67 + Convertor(final int fakeParameter, final TimeLimitedCurrencyRate ... currencyRate) { //use fake parameter just to specify which constructor should be invoked
1.68 + instanceVersion = 3;
1.69 +
1.70 + if (currencyRate == null) {
1.71 + throw new IllegalArgumentException("Parameter cannot be null.");
1.72 + }
1.73 + List<TimeLimitedCurrencyRate> curRates = new ArrayList<TimeLimitedCurrencyRate>();
1.74 + for (int i = 0; i < currencyRate.length; i++) {
1.75 + final TimeLimitedCurrencyRate curRat = currencyRate[i];
1.76 + if (curRat == null) {
1.77 + throw new IllegalArgumentException("Parameter cannot be null.");
1.78 + }
1.79 +
1.80 curRates.add(curRat);
1.81 }
1.82 this.currencyRates = Collections.unmodifiableList(curRates);
1.83 }
1.84
1.85 public double convert(String fromCurrency, String toCurrency, int amount) {
1.86 + return convert(fromCurrency, toCurrency, amount, System.currentTimeMillis());
1.87 + }
1.88 +
1.89 + public double convert(String fromCurrency, String toCurrency, int amount, Date date) {
1.90 + if (date == null) {
1.91 + throw new IllegalArgumentException("Date cannot be null");
1.92 + }
1.93 + return convert(fromCurrency, toCurrency, amount, date.getTime());
1.94 + }
1.95 +
1.96 + public double convert(String fromCurrency, String toCurrency, int amount, long time) {
1.97 if (instanceVersion == 1) {
1.98 if ((fromCurrency == null) || (toCurrency == null)) {
1.99 throw new IllegalArgumentException("All arguments have to be non-null.");
1.100 @@ -81,27 +135,42 @@
1.101 }
1.102 } else { //instanceVersion >= 2
1.103 //find suitable convertor
1.104 - for (CurrencyRate curRate : currencyRates) {
1.105 - if ((curRate.getCurrency1().equals(fromCurrency))&&
1.106 - (curRate.getCurrency2().equals(toCurrency)))
1.107 + for (TimeLimitedCurrencyRate curRate : currencyRates) {
1.108 + if ((curRate.getCurrency1().equals(fromCurrency))&&
1.109 + (curRate.getCurrency2().equals(toCurrency))&&
1.110 + (curRate.getFromTime() <= time) &&
1.111 + (curRate.getToTime() >= time))
1.112 {
1.113 return curRate.getRate().convertAtoB(amount);
1.114 }
1.115 }
1.116 //suitable convertor not found, try to find inverse convertor
1.117 - for (CurrencyRate curRate : currencyRates) {
1.118 + for (TimeLimitedCurrencyRate curRate : currencyRates) {
1.119 if ((curRate.getCurrency2().equals(fromCurrency))&&
1.120 - (curRate.getCurrency1().equals(toCurrency)))
1.121 + (curRate.getCurrency1().equals(toCurrency))&&
1.122 + (curRate.getFromTime() <= time) &&
1.123 + (curRate.getToTime() >= time))
1.124 {
1.125 return curRate.getRate().convertBtoA(amount);
1.126 }
1.127 - }
1.128 + }
1.129 //even inverse convertor not found
1.130 throw new IllegalArgumentException("Cannot work with selected currencies.");
1.131 }
1.132 }
1.133
1.134 public double convert(String fromCurrency, String toCurrency, double amount) {
1.135 + return convert(fromCurrency, toCurrency, amount, System.currentTimeMillis());
1.136 + }
1.137 +
1.138 + public double convert(String fromCurrency, String toCurrency, double amount, Date date) {
1.139 + if (date == null) {
1.140 + throw new IllegalArgumentException("Date cannot be null");
1.141 + }
1.142 + return convert(fromCurrency, toCurrency, amount, date.getTime());
1.143 + }
1.144 +
1.145 + public double convert(String fromCurrency, String toCurrency, double amount, long time) {
1.146 if (instanceVersion == 1) {
1.147 if ((fromCurrency == null) || (toCurrency == null)) {
1.148 throw new IllegalArgumentException("All arguments have to be non-null.");
1.149 @@ -117,21 +186,25 @@
1.150 }
1.151 } else { //instanceVersion >= 2
1.152 //find suitable convertor
1.153 - for (CurrencyRate curRate : currencyRates) {
1.154 - if ((curRate.getCurrency1().equals(fromCurrency))&&
1.155 - (curRate.getCurrency2().equals(toCurrency)))
1.156 + for (TimeLimitedCurrencyRate curRate : currencyRates) {
1.157 + if ((curRate.getCurrency1().equals(fromCurrency))&&
1.158 + (curRate.getCurrency2().equals(toCurrency))&&
1.159 + (curRate.getFromTime() <= time) &&
1.160 + (curRate.getToTime() >= time))
1.161 {
1.162 return curRate.getRate().convertAtoB(amount);
1.163 }
1.164 }
1.165 //suitable convertor not found, try to find inverse convertor
1.166 - for (CurrencyRate curRate : currencyRates) {
1.167 + for (TimeLimitedCurrencyRate curRate : currencyRates) {
1.168 if ((curRate.getCurrency2().equals(fromCurrency))&&
1.169 - (curRate.getCurrency1().equals(toCurrency)))
1.170 + (curRate.getCurrency1().equals(toCurrency))&&
1.171 + (curRate.getFromTime() <= time) &&
1.172 + (curRate.getToTime() >= time))
1.173 {
1.174 return curRate.getRate().convertBtoA(amount);
1.175 }
1.176 - }
1.177 + }
1.178 //even inverse convertor not found
1.179 throw new IllegalArgumentException("Cannot work with selected currencies.");
1.180 }
1.181 @@ -148,7 +221,19 @@
1.182 ret.add(new CurrencyRateImpl(currency1, currency2, rate)); //here it checks that currency rate is not nonsense
1.183 return Collections.unmodifiableCollection(ret);
1.184 } else { //instanceVersion >= 2
1.185 - return currencyRates;
1.186 + List<CurrencyRate> ret = new ArrayList<CurrencyRate>(currencyRates);
1.187 + return Collections.unmodifiableCollection(ret);
1.188 + }
1.189 + }
1.190 +
1.191 + public Collection<TimeLimitedCurrencyRate> getTimeLimitedCurrencyRates() {
1.192 + if (instanceVersion == 1) {
1.193 + List<TimeLimitedCurrencyRate> ret = new ArrayList<TimeLimitedCurrencyRate>();
1.194 + ret.add(new CurrencyRateImpl(currency1, currency2, rate)); //here it checks that currency rate is not nonsense
1.195 + return Collections.unmodifiableCollection(ret);
1.196 + } else { //instanceVersion >= 2
1.197 + List<TimeLimitedCurrencyRate> ret = new ArrayList<TimeLimitedCurrencyRate>(currencyRates);
1.198 + return Collections.unmodifiableCollection(ret);
1.199 }
1.200 }
1.201