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