1 9 package org.jscience.economics.money; 10 11 import javolution.context.LocalContext; 12 import javolution.util.LocalMap; 13 14 import javax.measure.converters.ConversionException; 15 import javax.measure.converters.UnitConverter; 16 import javax.measure.units.DerivedUnit; 17 import javax.measure.units.Unit; 18 import javax.measure.units.UnitFormat; 19 20 45 public class Currency extends DerivedUnit<Money> { 46 47 50 public static final Currency AUD = new Currency("AUD"); 51 52 55 public static final Currency CAD = new Currency("CAD"); 56 57 60 public static final Currency CNY = new Currency("CNY"); 61 62 65 public static final Currency EUR = new Currency("EUR"); 66 67 70 public static final Currency GBP = new Currency("GBP"); 71 72 75 public static final Currency JPY = new Currency("JPY"); 76 77 80 public static final Currency KRW = new Currency("KRW"); 81 82 85 public static final Currency TWD = new Currency("TWD"); 86 87 90 public static final Currency USD = new Currency("USD"); 91 92 95 private static final LocalContext.Reference<Currency> 96 REFERENCE = new LocalContext.Reference<Currency>(); 97 98 101 private static final LocalMap<String , Double > TO_REFERENCE = 102 new LocalMap<String , Double >(); 103 104 107 private final Converter _toBaseUnit; 108 109 120 public Currency(String code) { 121 _toBaseUnit = new Converter(code, false); 122 UnitFormat.getStandardInstance().label(this, code); 123 } 124 125 131 public String getCode() { 132 return _toBaseUnit._code; 133 } 134 135 144 public int getDefaultFractionDigits() { 145 return (this.equals(JPY) || (this.equals(KRW))) ? 146 0 : 2; 147 } 148 149 156 public static void setReferenceCurrency(Currency currency) { 157 REFERENCE.set(currency); 158 TO_REFERENCE.clear(); 159 TO_REFERENCE.put(currency.getCode(), 1.0); 160 } 161 162 170 public static Currency getReferenceCurrency() { 171 return REFERENCE.get(); 172 } 173 174 187 public void setExchangeRate(double refAmount) { 188 TO_REFERENCE.put(this.getCode(), refAmount); 189 } 190 191 199 public double getExchangeRate() { 200 Double refAmount = TO_REFERENCE.get(this.getCode()); 201 if (refAmount == null) 202 throw new ConversionException("Exchange rate not set for " + this.getCode()); 203 return refAmount.doubleValue(); 204 } 205 206 207 @Override 208 public boolean equals(Object obj) { 209 if (this == obj) return true; 210 if (!(obj instanceof Currency)) 211 return false; 212 Currency that = (Currency) obj; 213 return this._toBaseUnit.equals(that._toBaseUnit); 214 } 215 216 @Override 217 public int hashCode() { 218 return _toBaseUnit.hashCode(); 219 } 220 221 @Override 222 public Unit<? super Money> getSystemUnit() { 223 return Money.BASE_UNIT; 224 } 225 226 @Override 227 public UnitConverter toSystemUnit() { 228 return _toBaseUnit; 229 } 230 231 234 private static class Converter extends UnitConverter { 235 236 String _code; 237 238 boolean _invert; 239 240 private Converter(String code, boolean invert) { 241 _code = code; 242 _invert = invert; 243 } 244 245 @Override 246 public UnitConverter inverse() { 247 return new Converter(_code, !_invert); 248 } 249 250 @Override 251 public double convert(double x) throws ConversionException { 252 Double refAmount = TO_REFERENCE.get(_code); 253 if (refAmount == null) 254 throw new ConversionException("Exchange rate not set for " + _code); 255 return _invert ? x / refAmount.doubleValue() : x * refAmount.doubleValue(); 256 } 257 258 @Override 259 public boolean isLinear() { 260 return true; 261 } 262 263 @Override 264 public boolean equals(Object obj) { 265 if (this == obj) return true; 266 if (!(obj instanceof Converter)) 267 return false; 268 Converter that = (Converter) obj; 269 return this._code.equals(that._code) && (this._invert == that._invert); 270 } 271 272 @Override 273 public int hashCode() { 274 return _invert ? _code.hashCode() : - _code.hashCode(); 275 } 276 277 private static final long serialVersionUID = 1L; 278 } 279 280 private static final long serialVersionUID = 1L; 281 } | Popular Tags |