japod@6
|
1 |
package org.apidesign.apifest08.currency;
|
japod@6
|
2 |
|
japod@17
|
3 |
import java.math.BigDecimal;
|
japod@6
|
4 |
import java.util.Currency;
|
jaroslav@69
|
5 |
import java.util.Date;
|
japod@6
|
6 |
|
japod@6
|
7 |
|
japod@6
|
8 |
/**
|
japod@6
|
9 |
* Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
|
japod@6
|
10 |
*
|
japod@6
|
11 |
* @author D'Arcy Smith
|
jaroslav@69
|
12 |
* @version 1.2
|
japod@6
|
13 |
*/
|
japod@6
|
14 |
public final class ConvertorFactory
|
japod@6
|
15 |
{
|
japod@6
|
16 |
/**
|
japod@6
|
17 |
* Prevent accidental construction.
|
japod@6
|
18 |
*/
|
japod@6
|
19 |
private ConvertorFactory()
|
japod@6
|
20 |
{
|
japod@6
|
21 |
}
|
japod@6
|
22 |
|
japod@6
|
23 |
/**
|
japod@6
|
24 |
* Get the convertor for the specified currencies. The currency name format
|
japod@6
|
25 |
* must be acceptable to java.util.Currency.getInstance(String)
|
japod@6
|
26 |
*
|
japod@6
|
27 |
* @param a the currency to convert from.
|
japod@17
|
28 |
* @param aRate the exchange rate for a to b.
|
japod@6
|
29 |
* @param b the currency to convert to.
|
japod@17
|
30 |
* @param bRate the echante rate for b to a.
|
japod@6
|
31 |
* @return the convertor for the specified currencies.
|
japod@17
|
32 |
* @throws IllegalArgumentException if any of the arguments are null.
|
japod@6
|
33 |
*/
|
japod@17
|
34 |
public static Convertor getConvertor(final String a,
|
japod@17
|
35 |
final BigDecimal aRate,
|
japod@17
|
36 |
final String b,
|
japod@17
|
37 |
final BigDecimal bRate)
|
japod@6
|
38 |
{
|
japod@6
|
39 |
final Currency currencyA;
|
japod@6
|
40 |
final Currency currencyB;
|
japod@6
|
41 |
final Convertor convertor;
|
japod@6
|
42 |
|
japod@6
|
43 |
currencyA = Currency.getInstance(a);
|
japod@6
|
44 |
currencyB = Currency.getInstance(b);
|
japod@17
|
45 |
convertor = getConvertor(currencyA, aRate, currencyB, bRate);
|
japod@6
|
46 |
|
japod@6
|
47 |
return (convertor);
|
japod@6
|
48 |
}
|
japod@6
|
49 |
|
japod@6
|
50 |
/**
|
japod@6
|
51 |
* Get the convertor for the specified currencies.
|
japod@6
|
52 |
*
|
japod@6
|
53 |
* @param a the currency to convert from.
|
japod@17
|
54 |
* @param aRate the exchange rate for a to b.
|
japod@6
|
55 |
* @param b the currency to convert to.
|
japod@17
|
56 |
* @param bRate the echante rate for b to a.
|
japod@6
|
57 |
* @return the convertor for the specified currencies.
|
japod@17
|
58 |
* @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
|
japod@6
|
59 |
*/
|
japod@17
|
60 |
public static Convertor getConvertor(final Currency a,
|
japod@17
|
61 |
final BigDecimal aRate,
|
japod@17
|
62 |
final Currency b,
|
japod@17
|
63 |
final BigDecimal bRate)
|
japod@6
|
64 |
{
|
jaroslav@69
|
65 |
Convertor convertor;
|
jaroslav@69
|
66 |
final ExchangeRate rate;
|
japod@6
|
67 |
|
japod@6
|
68 |
if(a == null)
|
japod@6
|
69 |
{
|
japod@6
|
70 |
throw new IllegalArgumentException("a cannot be null");
|
japod@6
|
71 |
}
|
japod@6
|
72 |
|
japod@6
|
73 |
if(b == null)
|
japod@6
|
74 |
{
|
japod@6
|
75 |
throw new IllegalArgumentException("b cannot be null");
|
japod@6
|
76 |
}
|
japod@17
|
77 |
|
japod@17
|
78 |
if(aRate == null)
|
japod@17
|
79 |
{
|
japod@17
|
80 |
throw new IllegalArgumentException("aRate cannot be null");
|
japod@17
|
81 |
}
|
japod@17
|
82 |
|
japod@17
|
83 |
if(bRate == null)
|
japod@17
|
84 |
{
|
japod@17
|
85 |
throw new IllegalArgumentException("bRate cannot be null");
|
japod@17
|
86 |
}
|
japod@6
|
87 |
|
jaroslav@69
|
88 |
rate = ExchangeRate.getExchangeRate(a, b, aRate, bRate);
|
jaroslav@69
|
89 |
convertor = getConvertor(rate);
|
japod@35
|
90 |
|
japod@35
|
91 |
return (convertor);
|
japod@35
|
92 |
}
|
japod@35
|
93 |
|
jaroslav@69
|
94 |
public static Convertor getConvertor(final ExchangeRate rate)
|
jaroslav@69
|
95 |
{
|
jaroslav@69
|
96 |
final ConvertorImpl convertor;
|
jaroslav@69
|
97 |
|
jaroslav@69
|
98 |
if(rate == null)
|
jaroslav@69
|
99 |
{
|
jaroslav@69
|
100 |
throw new IllegalArgumentException("rate cannot be null");
|
jaroslav@69
|
101 |
}
|
jaroslav@69
|
102 |
|
jaroslav@69
|
103 |
convertor = new ConvertorImpl(rate);
|
jaroslav@69
|
104 |
|
jaroslav@69
|
105 |
return (convertor);
|
jaroslav@69
|
106 |
}
|
jaroslav@69
|
107 |
|
jaroslav@69
|
108 |
public static DatedConvertor getConvertor(final Date from,
|
jaroslav@69
|
109 |
final Date till,
|
jaroslav@69
|
110 |
final Convertor convertor)
|
jaroslav@69
|
111 |
{
|
jaroslav@69
|
112 |
final DateRange range;
|
jaroslav@69
|
113 |
final ExchangeRate rate;
|
jaroslav@69
|
114 |
final DatedConvertor datedConvertor;
|
jaroslav@69
|
115 |
|
jaroslav@69
|
116 |
if(from == null)
|
jaroslav@69
|
117 |
{
|
jaroslav@69
|
118 |
throw new IllegalArgumentException("from cannot be null");
|
jaroslav@69
|
119 |
}
|
jaroslav@69
|
120 |
|
jaroslav@69
|
121 |
if(till == null)
|
jaroslav@69
|
122 |
{
|
jaroslav@69
|
123 |
throw new IllegalArgumentException("till cannot be null");
|
jaroslav@69
|
124 |
}
|
jaroslav@69
|
125 |
|
jaroslav@69
|
126 |
if(convertor == null)
|
jaroslav@69
|
127 |
{
|
jaroslav@69
|
128 |
throw new IllegalArgumentException("convertor cannot be null");
|
jaroslav@69
|
129 |
}
|
jaroslav@69
|
130 |
|
jaroslav@69
|
131 |
if(from.after(till))
|
jaroslav@69
|
132 |
{
|
jaroslav@69
|
133 |
throw new IllegalArgumentException(from + " cannot be after " + till);
|
jaroslav@69
|
134 |
}
|
jaroslav@69
|
135 |
|
jaroslav@69
|
136 |
if(convertor instanceof ExchangeRateConvertor)
|
jaroslav@69
|
137 |
{
|
jaroslav@69
|
138 |
rate = ((ExchangeRateConvertor)convertor).getExchangeRate();
|
jaroslav@69
|
139 |
}
|
jaroslav@69
|
140 |
else
|
jaroslav@69
|
141 |
{
|
jaroslav@69
|
142 |
throw new Error();
|
jaroslav@69
|
143 |
}
|
jaroslav@69
|
144 |
|
jaroslav@69
|
145 |
range = new DateRange(from, till);
|
jaroslav@69
|
146 |
datedConvertor = new DatedConvertorImpl(range, rate);
|
jaroslav@69
|
147 |
|
jaroslav@69
|
148 |
return (datedConvertor);
|
jaroslav@69
|
149 |
}
|
jaroslav@69
|
150 |
|
japod@55
|
151 |
/**
|
japod@55
|
152 |
*
|
japod@55
|
153 |
* @param cs
|
japod@55
|
154 |
* @return
|
japod@55
|
155 |
*/
|
japod@35
|
156 |
public static Convertor mergeConvertors(final Convertor ... cs)
|
japod@35
|
157 |
{
|
japod@35
|
158 |
Convertor convertor;
|
jaroslav@69
|
159 |
int dated;
|
jaroslav@69
|
160 |
int nonDated;
|
japod@6
|
161 |
|
jaroslav@69
|
162 |
dated = 0;
|
jaroslav@69
|
163 |
nonDated = 0;
|
japod@35
|
164 |
|
jaroslav@69
|
165 |
for(final Convertor c : cs)
|
japod@35
|
166 |
{
|
jaroslav@69
|
167 |
if(c instanceof DatedConvertor)
|
jaroslav@69
|
168 |
{
|
jaroslav@69
|
169 |
dated++;
|
japod@35
|
170 |
}
|
jaroslav@69
|
171 |
else
|
jaroslav@69
|
172 |
{
|
jaroslav@69
|
173 |
nonDated++;
|
jaroslav@69
|
174 |
}
|
jaroslav@69
|
175 |
}
|
japod@35
|
176 |
|
jaroslav@69
|
177 |
if(dated != 0 && nonDated != 0)
|
jaroslav@69
|
178 |
{
|
jaroslav@69
|
179 |
throw new IllegalArgumentException("cannot mix DatedConvertors and non-DatedConvertors");
|
japod@35
|
180 |
}
|
jaroslav@69
|
181 |
|
jaroslav@69
|
182 |
if(dated != 0)
|
jaroslav@69
|
183 |
{
|
jaroslav@69
|
184 |
convertor = new DatedCompositeConvertorImpl(cs);
|
jaroslav@69
|
185 |
}
|
jaroslav@69
|
186 |
else
|
jaroslav@69
|
187 |
{
|
jaroslav@69
|
188 |
convertor = new CompositeConvertorImpl(cs);
|
jaroslav@69
|
189 |
}
|
jaroslav@69
|
190 |
|
japod@6
|
191 |
return (convertor);
|
japod@6
|
192 |
}
|
japod@6
|
193 |
}
|