1 30 31 34 package com.nightlabs.l10n; 35 36 import java.text.DecimalFormat ; 37 import java.text.DecimalFormatSymbols ; 38 import java.text.NumberFormat ; 39 import java.util.HashMap ; 40 import java.util.Locale ; 41 import java.util.Map ; 42 43 import com.nightlabs.config.Config; 44 45 48 public class DefaultNumberFormatProvider implements NumberFormatProvider 49 { 50 51 protected Config config; 52 protected String isoLanguage; 53 protected String isoCountry; 54 protected DefaultNumberFormatCfMod defaultNumberFormatCfMod; 55 56 public DefaultNumberFormatProvider() 57 { 58 } 59 60 63 public void init(Config config, String isoLanguage, String isoCountry) 64 { 65 try { 66 this.config = config; 67 this.isoLanguage = isoLanguage; 68 this.isoCountry = isoCountry; 69 this.defaultNumberFormatCfMod = (DefaultNumberFormatCfMod) ConfigUtil.createConfigModule(config, DefaultNumberFormatCfMod.class, isoLanguage, isoCountry); 70 } catch (RuntimeException x) { 71 throw x; 72 } catch (Exception x) { 73 throw new RuntimeException (x); 74 } 75 } 76 77 80 protected Map numberFormats = new HashMap (); 81 82 protected DecimalFormatSymbols createDecimalFormatSymbols() 83 { 84 DecimalFormatSymbols dfs = new DecimalFormatSymbols (new Locale (isoLanguage, isoCountry)); 85 dfs.setGroupingSeparator(defaultNumberFormatCfMod.getGroupingSeparator()); 86 dfs.setDecimalSeparator(defaultNumberFormatCfMod.getDecimalSeparator()); 87 return dfs; 88 } 89 90 protected String createIntegerDigitCountPattern(int integerDigitCount) 91 { 92 StringBuffer sb = new StringBuffer (); 93 int groupingSize = defaultNumberFormatCfMod.getGroupingSize(); 94 for (int i = 1; i <= integerDigitCount || i <= groupingSize; ++i) { 95 sb.insert(0, i <= integerDigitCount ? '0' : '#'); 96 if (i % groupingSize == 0) 97 sb.insert(0, ','); 98 } 99 return sb.toString(); 100 } 101 102 protected String createDecimalDigitCountPattern(int minDecimalDigitCount, int maxDecimalDigitCount) 103 { 104 StringBuffer sb = new StringBuffer (); 105 for (int i = 1; i <= minDecimalDigitCount || i <= maxDecimalDigitCount; ++i) { 106 sb.append(i <= minDecimalDigitCount ? '0' : '#'); 107 } 108 return sb.toString(); 109 } 110 111 114 public NumberFormat getIntegerFormat(int minIntegerDigitCount) 115 { 116 String key = "integer_"+Integer.toHexString(minIntegerDigitCount); 117 DecimalFormat df; 118 synchronized (numberFormats) { 119 df = (DecimalFormat ) numberFormats.get(key); 120 } 121 if (df == null) { 122 DecimalFormatSymbols dfs = createDecimalFormatSymbols(); 123 String integerDigitCountPattern = createIntegerDigitCountPattern(minIntegerDigitCount); 124 125 df = new DecimalFormat ( 126 defaultNumberFormatCfMod.getPositivePrefix() 127 +'#'+integerDigitCountPattern 128 +defaultNumberFormatCfMod.getPositiveSuffix() 129 +';' 130 +defaultNumberFormatCfMod.getNegativePrefix() 131 +'#'+integerDigitCountPattern 132 +defaultNumberFormatCfMod.getNegativeSuffix(), 133 dfs); 134 135 synchronized (numberFormats) { 136 numberFormats.put(key, df); 137 } 138 } 139 140 return df; 141 } 142 143 146 public NumberFormat getFloatFormat(int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount) 147 { 148 String key = "float_"+Integer.toHexString(minIntegerDigitCount) 149 +'_'+Integer.toHexString(minDecimalDigitCount) 150 +'_'+Integer.toHexString(maxDecimalDigitCount); 151 DecimalFormat df; 152 synchronized (numberFormats) { 153 df = (DecimalFormat ) numberFormats.get(key); 154 } 155 if (df == null) { 156 DecimalFormatSymbols dfs = createDecimalFormatSymbols(); 157 String integerDigitCountPattern = createIntegerDigitCountPattern(minIntegerDigitCount); 158 String decimalDigitCountPattern = createDecimalDigitCountPattern(minDecimalDigitCount, maxDecimalDigitCount); 159 160 df = new DecimalFormat ( 161 defaultNumberFormatCfMod.getPositivePrefix() 162 +'#'+integerDigitCountPattern 163 +'.'+decimalDigitCountPattern 164 +defaultNumberFormatCfMod.getPositiveSuffix() 165 +';' 166 +defaultNumberFormatCfMod.getNegativePrefix() 167 +'#'+integerDigitCountPattern 168 +'.'+decimalDigitCountPattern 169 +defaultNumberFormatCfMod.getNegativeSuffix(), 170 dfs); 171 172 synchronized (numberFormats) { 173 numberFormats.put(key, df); 174 } 175 } 176 177 return df; 178 } 179 180 183 public NumberFormat getCurrencyFormat(int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount, String currencySymbol, boolean includeCurrencySymbol) 184 { 185 String key = "currency_"+Integer.toHexString(minIntegerDigitCount) 186 +'_'+Integer.toHexString(minDecimalDigitCount) 187 +'_'+Integer.toHexString(maxDecimalDigitCount); 188 DecimalFormat df; 189 synchronized (numberFormats) { 190 df = (DecimalFormat ) numberFormats.get(key); 191 } 192 if (df == null) { 193 DecimalFormatSymbols dfs = createDecimalFormatSymbols(); 194 dfs.setCurrencySymbol(currencySymbol); 196 String integerDigitCountPattern = createIntegerDigitCountPattern(minIntegerDigitCount); 197 String decimalDigitCountPattern = createDecimalDigitCountPattern(minDecimalDigitCount, maxDecimalDigitCount); 198 199 String beginCurrSymbol; 200 String endCurrSymbol; 201 if (!includeCurrencySymbol) { 202 beginCurrSymbol = ""; 203 endCurrSymbol = ""; 204 } 205 else if (DefaultNumberFormatCfMod.CURRENCYSYMBOLPOSITION_BEGIN.equals(defaultNumberFormatCfMod.getCurrencySymbolPosition())) { 206 beginCurrSymbol = "\u00A4 "; 207 endCurrSymbol = ""; 208 } 209 else { 210 beginCurrSymbol = ""; 211 endCurrSymbol = " \u00A4"; 212 } 213 214 df = new DecimalFormat ( 215 beginCurrSymbol 216 +defaultNumberFormatCfMod.getPositivePrefix() 217 +'#'+integerDigitCountPattern 218 +'.'+decimalDigitCountPattern 219 +defaultNumberFormatCfMod.getPositiveSuffix() 220 +endCurrSymbol 221 +';' 222 +beginCurrSymbol 223 +defaultNumberFormatCfMod.getNegativePrefix() 224 +'#'+integerDigitCountPattern 225 +'.'+decimalDigitCountPattern 226 +defaultNumberFormatCfMod.getNegativeSuffix() 227 +endCurrSymbol, 228 dfs); 229 230 synchronized (numberFormats) { 231 numberFormats.put(key, df); 232 } 233 } 234 235 return df; 236 } 237 238 241 public NumberFormat getScientificFormat(int preferredIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount, int exponentDigitCount) 242 { 243 String key = "scientific_"+Integer.toHexString(preferredIntegerDigitCount) 244 +'_'+Integer.toHexString(minDecimalDigitCount) 245 +'_'+Integer.toHexString(maxDecimalDigitCount) 246 +'_'+Integer.toHexString(exponentDigitCount); 247 DecimalFormat df; 248 synchronized (numberFormats) { 249 df = (DecimalFormat ) numberFormats.get(key); 250 } 251 if (df == null) { 252 DecimalFormatSymbols dfs = createDecimalFormatSymbols(); 253 254 StringBuffer sb = new StringBuffer (); 255 for (int i = 1; i <= preferredIntegerDigitCount; ++i) { 256 sb.append('0'); 257 } 258 String integerDigitCountPattern = sb.toString(); 259 String decimalDigitCountPattern = createDecimalDigitCountPattern(minDecimalDigitCount, maxDecimalDigitCount); 260 sb.setLength(0); 261 for (int i = 1; i <= exponentDigitCount; ++i) { 262 sb.append('0'); 263 } 264 String exponentDigitCountPattern = sb.toString(); 265 266 df = new DecimalFormat ( 267 defaultNumberFormatCfMod.getPositivePrefix() 268 +'#'+integerDigitCountPattern 269 +'.'+decimalDigitCountPattern 270 +'E'+exponentDigitCountPattern 271 +defaultNumberFormatCfMod.getPositiveSuffix() 272 +';' 273 +defaultNumberFormatCfMod.getNegativePrefix() 274 +'#'+integerDigitCountPattern 275 +'.'+decimalDigitCountPattern 276 +'E'+exponentDigitCountPattern 277 +defaultNumberFormatCfMod.getNegativeSuffix(), 278 dfs); 279 280 synchronized (numberFormats) { 281 numberFormats.put(key, df); 282 } 283 } 284 285 return null; 286 } 287 288 } 289 | Popular Tags |