japod@6
|
1 |
package org.apidesign.apifest08.currency;
|
japod@6
|
2 |
|
japod@6
|
3 |
import java.lang.ref.WeakReference;
|
japod@17
|
4 |
import java.math.BigDecimal;
|
japod@6
|
5 |
import java.util.Currency;
|
japod@6
|
6 |
import java.util.Map;
|
japod@6
|
7 |
import java.util.WeakHashMap;
|
japod@6
|
8 |
|
japod@6
|
9 |
|
japod@6
|
10 |
/**
|
japod@6
|
11 |
* Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
|
japod@6
|
12 |
*
|
japod@6
|
13 |
* @author D'Arcy Smith
|
japod@6
|
14 |
* @version 1.0
|
japod@6
|
15 |
*/
|
japod@6
|
16 |
public final class ConvertorFactory
|
japod@6
|
17 |
{
|
japod@6
|
18 |
/**
|
japod@6
|
19 |
* flyweight so that only one vestion of each converter is created at a time.
|
japod@6
|
20 |
*/
|
japod@6
|
21 |
private final static Map<String, WeakReference<Convertor>> convertors;
|
japod@6
|
22 |
|
japod@6
|
23 |
static
|
japod@6
|
24 |
{
|
japod@6
|
25 |
convertors = new WeakHashMap<String, WeakReference<Convertor>>();
|
japod@6
|
26 |
}
|
japod@6
|
27 |
|
japod@6
|
28 |
/**
|
japod@6
|
29 |
* Prevent accidental construction.
|
japod@6
|
30 |
*/
|
japod@6
|
31 |
private ConvertorFactory()
|
japod@6
|
32 |
{
|
japod@6
|
33 |
}
|
japod@6
|
34 |
|
japod@6
|
35 |
/**
|
japod@6
|
36 |
* Get the convertor for the specified currencies. The currency name format
|
japod@6
|
37 |
* must be acceptable to java.util.Currency.getInstance(String)
|
japod@6
|
38 |
*
|
japod@6
|
39 |
* @param a the currency to convert from.
|
japod@17
|
40 |
* @param aRate the exchange rate for a to b.
|
japod@6
|
41 |
* @param b the currency to convert to.
|
japod@17
|
42 |
* @param bRate the echante rate for b to a.
|
japod@6
|
43 |
* @return the convertor for the specified currencies.
|
japod@17
|
44 |
* @throws IllegalArgumentException if any of the arguments are null.
|
japod@6
|
45 |
*/
|
japod@17
|
46 |
public static Convertor getConvertor(final String a,
|
japod@17
|
47 |
final BigDecimal aRate,
|
japod@17
|
48 |
final String b,
|
japod@17
|
49 |
final BigDecimal bRate)
|
japod@6
|
50 |
{
|
japod@6
|
51 |
final Currency currencyA;
|
japod@6
|
52 |
final Currency currencyB;
|
japod@6
|
53 |
final Convertor convertor;
|
japod@6
|
54 |
|
japod@6
|
55 |
currencyA = Currency.getInstance(a);
|
japod@6
|
56 |
currencyB = Currency.getInstance(b);
|
japod@17
|
57 |
convertor = getConvertor(currencyA, aRate, currencyB, bRate);
|
japod@6
|
58 |
|
japod@6
|
59 |
return (convertor);
|
japod@6
|
60 |
}
|
japod@6
|
61 |
|
japod@6
|
62 |
/**
|
japod@6
|
63 |
* Get the convertor for the specified currencies.
|
japod@6
|
64 |
*
|
japod@6
|
65 |
* @param a the currency to convert from.
|
japod@17
|
66 |
* @param aRate the exchange rate for a to b.
|
japod@6
|
67 |
* @param b the currency to convert to.
|
japod@17
|
68 |
* @param bRate the echante rate for b to a.
|
japod@6
|
69 |
* @return the convertor for the specified currencies.
|
japod@17
|
70 |
* @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
|
japod@6
|
71 |
*/
|
japod@17
|
72 |
public static Convertor getConvertor(final Currency a,
|
japod@17
|
73 |
final BigDecimal aRate,
|
japod@17
|
74 |
final Currency b,
|
japod@17
|
75 |
final BigDecimal bRate)
|
japod@6
|
76 |
{
|
japod@6
|
77 |
final String key;
|
japod@6
|
78 |
Convertor convertor;
|
japod@6
|
79 |
|
japod@6
|
80 |
if(a == null)
|
japod@6
|
81 |
{
|
japod@6
|
82 |
throw new IllegalArgumentException("a cannot be null");
|
japod@6
|
83 |
}
|
japod@6
|
84 |
|
japod@6
|
85 |
if(b == null)
|
japod@6
|
86 |
{
|
japod@6
|
87 |
throw new IllegalArgumentException("b cannot be null");
|
japod@6
|
88 |
}
|
japod@17
|
89 |
|
japod@17
|
90 |
if(aRate == null)
|
japod@17
|
91 |
{
|
japod@17
|
92 |
throw new IllegalArgumentException("aRate cannot be null");
|
japod@17
|
93 |
}
|
japod@17
|
94 |
|
japod@17
|
95 |
if(bRate == null)
|
japod@17
|
96 |
{
|
japod@17
|
97 |
throw new IllegalArgumentException("bRate cannot be null");
|
japod@17
|
98 |
}
|
japod@6
|
99 |
|
japod@17
|
100 |
key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
|
japod@6
|
101 |
|
japod@6
|
102 |
// make sure that we don't try to overwrite one
|
japod@6
|
103 |
synchronized(convertors)
|
japod@6
|
104 |
{
|
japod@6
|
105 |
if(!(convertors.containsKey(key)))
|
japod@6
|
106 |
{
|
japod@17
|
107 |
convertor = new ConvertorImpl(a, aRate, b, bRate);
|
japod@6
|
108 |
convertors.put(key, new WeakReference(convertor));
|
japod@6
|
109 |
}
|
japod@6
|
110 |
}
|
japod@6
|
111 |
|
japod@6
|
112 |
convertor = convertors.get(key).get();
|
japod@6
|
113 |
|
japod@6
|
114 |
return (convertor);
|
japod@6
|
115 |
}
|
japod@6
|
116 |
}
|