View Javadoc
1 /*** 2 * CurrencyFormat.java 3 * 4 * $Author: mballesteros $ 5 * $Date: 2003/11/28 19:18:04 $ 6 * $Revision: 1.1 $ 7 */ 8 package net.sf.jec.functions; 9 10 import java.text.DecimalFormat; 11 import java.text.DecimalFormatSymbols; 12 import java.util.HashMap; 13 import java.util.Map; 14 15 /*** This is a utility class for currency formatting and conversion. Use it to 16 * convert currency amounts between countries (example: EUR->ESP). You can just 17 * convert a 'double' amount, so it acts as a converter, or a 'double' to a 18 * formatted String.<p> 19 * To create a new CurrencyFormat object you need a factor map, which gives the 20 * relationship between amounts, and a format map, that specifies how a 21 * currency type must be transformed into a String.<p> 22 * <b>factorMap</b> is a Map with pairs String/Double with the relationships 23 * between currency types amounts. The default instance has 2 entries: 24 * "EUR"->"1.0d" and "ESP"->"166.386d". Notice that there's a reference 25 * currency with value "1.0d". Other currency values are relative to this 26 * reference currency type.<p> 27 * <b>formatMap</b> is a Map with pairs String/DecimalFormat with the relationships 28 * between currency types and their formatter (as they are 'doubles' their 29 * formatter are DecimalFormat objects).<p> 30 * The default CurrencyFormat has only 2 currency types: ESP and EUR.<p> 31 * <ul> 32 * <li>The factor map is: EUR = 1.0d, ESP = 166.386d</li> 33 * <li>The format map is: EUR = #,##0.00 \u00A4, ESP = #,##0 \u00A4, where 34 * the decimal simbols are: ',' decimal separator, '.' group separator, 35 * 'EUR' & 'ESP' currency symbol.</li> 36 * </ul> 37 * @author mballesteros 38 * @version 39 */ 40 public class CurrencyFormat { 41 42 private static CurrencyFormat dafaultInstance; 43 44 /*** Returns the default instance with ESP and EUR as the only currency types. 45 */ 46 public static CurrencyFormat getDefaultInstance() { 47 if (dafaultInstance == null) { 48 DecimalFormatSymbols dfsEUR = new DecimalFormatSymbols(); 49 dfsEUR.setDecimalSeparator(','); 50 dfsEUR.setMonetaryDecimalSeparator(','); 51 dfsEUR.setGroupingSeparator('.'); 52 dfsEUR.setCurrencySymbol("EUR"); 53 54 DecimalFormatSymbols dfsESP = new DecimalFormatSymbols(); 55 dfsESP.setDecimalSeparator(','); 56 dfsESP.setMonetaryDecimalSeparator(','); 57 dfsESP.setGroupingSeparator('.'); 58 dfsESP.setCurrencySymbol("ESP"); 59 60 Map formatMap = new HashMap(); 61 formatMap.put("EUR", new DecimalFormat("#,##0.00 \u00A4", dfsEUR)); 62 formatMap.put("ESP", new DecimalFormat("#,##0 \u00A4", dfsESP)); 63 64 Map factorMap = new HashMap(); 65 factorMap.put("EUR", new Double(1.0d)); 66 factorMap.put("ESP", new Double(166.386d)); 67 68 dafaultInstance = new CurrencyFormat(factorMap, formatMap, "EUR"); 69 } 70 return dafaultInstance; 71 } 72 73 private String defaultCurrType; 74 private Map factorMap; 75 private Map formatMap; 76 77 /*** Creates a new CurrencyFormat object given a factor map, which gives the 78 * relationship between amounts, and a format map, that specifies how a 79 * currency type must be transformed into a String. 80 * @param factorMap A Map with pairs String/Double with the relationships 81 * between currency types amounts. The default instance has 2 entries: 82 * "EUR"->"1.0d" and "ESP"->"166.386d". Notice that there's a reference 83 * currency with value "1.0d". Other currency values are relative to this 84 * reference currency type. 85 * @param formatMap A Map with pairs String/DecimalFormat with the relationships 86 * between currency types and their formatter (as they are 'doubles' their 87 * formatter are DecimalFormat objects). 88 */ 89 public CurrencyFormat(Map factorMap, Map formatMap, String defaultCurrType) { 90 this.factorMap = factorMap; 91 this.formatMap = formatMap; 92 this.defaultCurrType = defaultCurrType; 93 } 94 95 /*** Formats an amount in the default currency type 96 * @param amount The amount to convert and format 97 */ 98 public String toString(double amount) { 99 return toString(amount, defaultCurrType, defaultCurrType); 100 } 101 102 103 /*** Converts and formats an amount, given a source currency type, into a 104 * target currency format. 105 * @param amount The amount to convert and format 106 * @param idSource The amount's currency type 107 * @param idTarget The desired target currency type 108 */ 109 public String toString(double amount, String idSource, String idTarget) { 110 double sourceFactor; 111 double targetFactor; 112 double finalAmount; 113 Double dSourceFactor = (Double)factorMap.get(idSource); 114 Double dTargetFactor = (Double)factorMap.get(idTarget); 115 DecimalFormat df = (DecimalFormat)formatMap.get(idTarget); 116 if (dSourceFactor != null && dTargetFactor != null) { 117 sourceFactor = dSourceFactor.doubleValue(); 118 targetFactor = dTargetFactor.doubleValue(); 119 finalAmount = amount * (targetFactor / sourceFactor); 120 } else if (dSourceFactor != null) { 121 finalAmount = amount; 122 df = (DecimalFormat)formatMap.get(idSource); 123 } else { 124 return Double.toString(amount); 125 } 126 return df.format(finalAmount); 127 } 128 129 /*** Parses an String expressed in a source currency type and converts it 130 * to a target currency type amount 131 */ 132 public double parseString(String amount) { 133 return parseString(amount, defaultCurrType, defaultCurrType); 134 } 135 136 /*** Parses an String expressed in a source currency type and converts it 137 * to a target currency type amount 138 */ 139 public double parseString(String amount, String idSource, String idTarget) { 140 try { 141 DecimalFormat df = (DecimalFormat)formatMap.get(idSource); 142 double sourceOut = df.parse(amount).doubleValue(); 143 return convert(sourceOut, idSource, idTarget); 144 } catch (Exception ex) { 145 ex.printStackTrace(); 146 return 0d; 147 } 148 } 149 150 /*** Converts an amount, given a source currency type, into a target currency 151 * amount. 152 * @param amount The amount to convert 153 * @param idSource The amount's currency type 154 * @param idTarget The desired target currency type 155 */ 156 public double convert(double amount, String idSource, String idTarget) { 157 double sourceFactor; 158 double targetFactor; 159 Double dSourceFactor = (Double)factorMap.get(idSource); 160 Double dTargetFactor = (Double)factorMap.get(idTarget); 161 if (dSourceFactor != null && dTargetFactor != null) { 162 sourceFactor = dSourceFactor.doubleValue(); 163 targetFactor = dTargetFactor.doubleValue(); 164 return amount * (targetFactor / sourceFactor); 165 } else { 166 return amount; 167 } 168 } 169 }

This page was automatically generated by Maven