japod@25
|
1 |
|
japod@25
|
2 |
package org.apidesign.apifest08.currency;
|
japod@25
|
3 |
|
japod@49
|
4 |
import java.util.ArrayList;
|
jaroslav@67
|
5 |
import java.util.Collection;
|
jaroslav@67
|
6 |
import java.util.Date;
|
japod@49
|
7 |
import java.util.List;
|
japod@49
|
8 |
|
japod@25
|
9 |
public final class ConvertorFactory {
|
japod@25
|
10 |
|
japod@25
|
11 |
//Singleton
|
japod@25
|
12 |
private static ConvertorFactory thisFactory = new ConvertorFactory();
|
japod@25
|
13 |
private ConvertorFactory() {};
|
japod@49
|
14 |
public static ConvertorFactory newInstance() { //ehm, mistake - it should be named getInstance
|
japod@25
|
15 |
return thisFactory;
|
japod@25
|
16 |
}
|
japod@25
|
17 |
|
japod@25
|
18 |
public Convertor createConvertor(String currency1, String currency2, Rate rate) {
|
japod@25
|
19 |
return new Convertor(currency1, currency2, rate);
|
japod@25
|
20 |
}
|
japod@25
|
21 |
|
japod@25
|
22 |
public Convertor createConvertor(String currency1, String currency2, int amount1, int amount2) {
|
japod@25
|
23 |
return new Convertor(currency1, currency2, new Rate(amount1, amount2));
|
japod@25
|
24 |
}
|
japod@25
|
25 |
|
japod@25
|
26 |
public Convertor createConvertor(String currency1, String currency2, double amount1, double amount2) {
|
japod@25
|
27 |
return new Convertor(currency1, currency2, new Rate(amount1, amount2));
|
japod@25
|
28 |
}
|
japod@25
|
29 |
|
japod@25
|
30 |
public Convertor createConvertor(String currency1, String currency2, double rate) {
|
japod@25
|
31 |
return new Convertor(currency1, currency2, new Rate(rate));
|
japod@25
|
32 |
}
|
japod@49
|
33 |
|
japod@49
|
34 |
public Convertor createConvertor(CurrencyRate currencyRate) {
|
japod@49
|
35 |
return new Convertor(currencyRate);
|
japod@49
|
36 |
}
|
japod@49
|
37 |
|
japod@49
|
38 |
public Convertor createConvertor(CurrencyRate ... currencyRates) {
|
japod@49
|
39 |
return new Convertor(currencyRates);
|
japod@49
|
40 |
}
|
japod@49
|
41 |
|
japod@49
|
42 |
public Convertor mergeConvertors(Convertor ... convertors) {
|
japod@49
|
43 |
if (convertors == null) {
|
japod@49
|
44 |
throw new IllegalArgumentException("Parameter cannot be null.");
|
japod@49
|
45 |
}
|
japod@49
|
46 |
if (convertors.length == 0) {
|
japod@49
|
47 |
throw new IllegalArgumentException("Convertors cannot be empty.");
|
japod@49
|
48 |
}
|
japod@49
|
49 |
List<CurrencyRate> currRates = new ArrayList<CurrencyRate>();
|
japod@49
|
50 |
List<Pair<String,String>> currPairs = new ArrayList<Pair<String,String>>();
|
japod@49
|
51 |
for (Convertor convertor : convertors) {
|
japod@49
|
52 |
if (convertor == null) {
|
japod@49
|
53 |
throw new IllegalArgumentException("Parameter cannot be null.");
|
japod@49
|
54 |
}
|
japod@49
|
55 |
for (CurrencyRate currRate : convertor.getCurrencyRates()) {
|
japod@49
|
56 |
Pair<String,String> currPair = new Pair<String,String>(currRate.getCurrency1(), currRate.getCurrency2());
|
japod@49
|
57 |
if (currPairs.contains(currPair)) {
|
japod@49
|
58 |
throw new IllegalArgumentException("Cannot merge - convertors contain same currency rates.");
|
japod@49
|
59 |
}
|
japod@49
|
60 |
currPairs.add(currPair);
|
japod@49
|
61 |
currRates.add(currRate);
|
japod@49
|
62 |
}
|
japod@49
|
63 |
}
|
japod@49
|
64 |
|
japod@49
|
65 |
return new Convertor(currRates.toArray(new CurrencyRate[0]));
|
japod@49
|
66 |
}
|
japod@49
|
67 |
|
jaroslav@67
|
68 |
public Convertor mergeConvertorsIgnoreEqualCurrencies(Convertor ... convertors) {
|
jaroslav@67
|
69 |
if (convertors == null) {
|
jaroslav@67
|
70 |
throw new IllegalArgumentException("Parameter cannot be null.");
|
jaroslav@67
|
71 |
}
|
jaroslav@67
|
72 |
if (convertors.length == 0) {
|
jaroslav@67
|
73 |
throw new IllegalArgumentException("Convertors cannot be empty.");
|
jaroslav@67
|
74 |
}
|
jaroslav@67
|
75 |
List<TimeLimitedCurrencyRate> currRates = new ArrayList<TimeLimitedCurrencyRate>();
|
jaroslav@67
|
76 |
for (Convertor convertor : convertors) {
|
jaroslav@67
|
77 |
if (convertor == null) {
|
jaroslav@67
|
78 |
throw new IllegalArgumentException("Parameter cannot be null.");
|
jaroslav@67
|
79 |
}
|
jaroslav@67
|
80 |
currRates.addAll(convertor.getTimeLimitedCurrencyRates());
|
jaroslav@67
|
81 |
}
|
jaroslav@67
|
82 |
|
jaroslav@67
|
83 |
return new Convertor(4, currRates.toArray(new TimeLimitedCurrencyRate[0]));
|
jaroslav@67
|
84 |
}
|
jaroslav@67
|
85 |
|
jaroslav@67
|
86 |
public Convertor limitConvertor(Convertor convertor, Date fromDate, Date toDate) {
|
jaroslav@67
|
87 |
if ((convertor == null)||(fromDate == null)||(toDate == null)) {
|
jaroslav@67
|
88 |
throw new IllegalArgumentException("Parameter cannot be null");
|
jaroslav@67
|
89 |
}
|
jaroslav@67
|
90 |
Collection<TimeLimitedCurrencyRate> timeLimitedCurrencyRates = convertor.getTimeLimitedCurrencyRates();
|
jaroslav@67
|
91 |
List<TimeLimitedCurrencyRate> filteredRates = new ArrayList<TimeLimitedCurrencyRate>();
|
jaroslav@67
|
92 |
for (final TimeLimitedCurrencyRate timeLimitedCurrencyRate : timeLimitedCurrencyRates) {
|
jaroslav@67
|
93 |
final long newFrom = java.lang.Math.max(fromDate.getTime(), timeLimitedCurrencyRate.getFromTime());
|
jaroslav@67
|
94 |
final long newTo = java.lang.Math.min(toDate.getTime(), timeLimitedCurrencyRate.getToTime());
|
jaroslav@67
|
95 |
if (newTo >= newFrom) {
|
jaroslav@67
|
96 |
filteredRates.add(new TimeLimitedCurrencyRate() { //create delegate
|
jaroslav@67
|
97 |
public long getFromTime() {
|
jaroslav@67
|
98 |
return newFrom;
|
jaroslav@67
|
99 |
}
|
jaroslav@67
|
100 |
public long getToTime() {
|
jaroslav@67
|
101 |
return newTo;
|
jaroslav@67
|
102 |
}
|
jaroslav@67
|
103 |
public String getCurrency1() {
|
jaroslav@67
|
104 |
return timeLimitedCurrencyRate.getCurrency1();
|
jaroslav@67
|
105 |
}
|
jaroslav@67
|
106 |
public String getCurrency2() {
|
jaroslav@67
|
107 |
return timeLimitedCurrencyRate.getCurrency2();
|
jaroslav@67
|
108 |
}
|
jaroslav@67
|
109 |
public Rate getRate() {
|
jaroslav@67
|
110 |
return timeLimitedCurrencyRate.getRate();
|
jaroslav@67
|
111 |
}
|
jaroslav@67
|
112 |
});
|
jaroslav@67
|
113 |
}
|
jaroslav@67
|
114 |
}
|
jaroslav@67
|
115 |
return new Convertor(4, filteredRates.toArray(new TimeLimitedCurrencyRate[0]));
|
jaroslav@67
|
116 |
}
|
jaroslav@67
|
117 |
|
japod@25
|
118 |
}
|