japod@58
|
1 |
package org.apidesign.apifest08.test;
|
japod@58
|
2 |
|
japod@58
|
3 |
import java.math.BigDecimal;
|
japod@58
|
4 |
import org.apidesign.apifest08.currency.ConvertorCurrency;
|
japod@58
|
5 |
import org.apidesign.apifest08.currency.ExchangeRate;
|
japod@58
|
6 |
import org.apidesign.apifest08.currency.IExchangeRateEngine;
|
japod@58
|
7 |
|
japod@58
|
8 |
/** Exchange rate engine which is periodicaly changing echange rate.
|
japod@58
|
9 |
* See <a href="http://wiki.apidesign.org/wiki/APIFest08:Task3">http://wiki.apidesign.org/wiki/APIFest08:Task3</a> for further description.
|
japod@58
|
10 |
*
|
japod@58
|
11 |
* @author arnostvalicek
|
japod@58
|
12 |
*/
|
japod@58
|
13 |
public class BouncingExchangeRateEngine implements IExchangeRateEngine {
|
japod@58
|
14 |
|
japod@58
|
15 |
ConvertorCurrency fromCurrency;
|
japod@58
|
16 |
BigDecimal fromValue;
|
japod@58
|
17 |
ConvertorCurrency toCurrency;
|
japod@58
|
18 |
BigDecimal toValueStart;
|
japod@58
|
19 |
BigDecimal toValueStop;
|
japod@58
|
20 |
BigDecimal step;
|
japod@58
|
21 |
|
japod@58
|
22 |
BigDecimal toValue;
|
japod@58
|
23 |
|
japod@58
|
24 |
|
japod@58
|
25 |
|
japod@58
|
26 |
|
japod@58
|
27 |
private BouncingExchangeRateEngine(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) {
|
japod@58
|
28 |
this.fromCurrency = fromCurrency;
|
japod@58
|
29 |
this.fromValue = fromValue;
|
japod@58
|
30 |
this.toCurrency = toCurrency;
|
japod@58
|
31 |
this.toValueStart = toValueStart;
|
japod@58
|
32 |
this.toValueStop = toValueStop;
|
japod@58
|
33 |
this.step = step;
|
japod@58
|
34 |
this.toValue = toValueStart;
|
japod@58
|
35 |
}
|
japod@58
|
36 |
|
japod@58
|
37 |
|
japod@58
|
38 |
public static IExchangeRateEngine create(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) {
|
japod@58
|
39 |
return new BouncingExchangeRateEngine(fromCurrency, fromValue, toCurrency, toValueStart, toValueStop, step);
|
japod@58
|
40 |
}
|
japod@58
|
41 |
|
japod@58
|
42 |
public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
|
japod@58
|
43 |
if (!fromCurrency.equals(this.fromCurrency)) {
|
japod@58
|
44 |
return null;
|
japod@58
|
45 |
}
|
japod@58
|
46 |
if (!toCurrency.equals(this.toCurrency)) {
|
japod@58
|
47 |
return null;
|
japod@58
|
48 |
}
|
japod@58
|
49 |
|
japod@58
|
50 |
ExchangeRate result = new ExchangeRate(fromValue, toValue);
|
japod@58
|
51 |
|
japod@58
|
52 |
toValue = toValue.add(step);
|
japod@58
|
53 |
|
japod@58
|
54 |
// if (step.signum()==1 && toValueStop.compareTo(toValue)==0) {
|
japod@58
|
55 |
// System.out.println("A");
|
japod@58
|
56 |
// toValue=toValueStop;
|
japod@58
|
57 |
// step = step.negate();
|
japod@58
|
58 |
// BigDecimal x = toValueStart;
|
japod@58
|
59 |
// toValueStart=toValueStop;
|
japod@58
|
60 |
// toValueStop=x;
|
japod@58
|
61 |
// } else if (step.signum()==-1 && toValueStop.compareTo(toValue)==0) {
|
japod@58
|
62 |
// System.out.println("B");
|
japod@58
|
63 |
// toValue=toValueStop;
|
japod@58
|
64 |
// step = step.negate();
|
japod@58
|
65 |
// BigDecimal x = toValueStart;
|
japod@58
|
66 |
// toValueStart=toValueStop;
|
japod@58
|
67 |
// toValueStop=x;
|
japod@58
|
68 |
// }
|
japod@58
|
69 |
if (toValueStop.compareTo(toValue)==0) {
|
japod@58
|
70 |
toValue=toValueStop;
|
japod@58
|
71 |
step = step.negate();
|
japod@58
|
72 |
BigDecimal x = toValueStart;
|
japod@58
|
73 |
toValueStart=toValueStop;
|
japod@58
|
74 |
toValueStop=x;
|
japod@58
|
75 |
}
|
japod@58
|
76 |
return result;
|
japod@58
|
77 |
}
|
japod@58
|
78 |
|
japod@58
|
79 |
}
|