|
|
| 508 |
598 |
|
| 8 | 8 | import com.jbidwatcher.util.config.ErrorManagement;
|
| 9 | 9 | import java.text.NumberFormat;
|
| 10 | 10 | import java.util.Locale;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.HashMap;
|
| 11 | 13 |
|
| 12 | 14 | public class Currency implements Comparable {
|
| 13 | 15 | private static NumberFormat df = NumberFormat.getNumberInstance(Locale.US); // We create a lot of these, so minimizing memory usage is good.
|
| --- | --- | |
| 34 | 36 | private static final char pound = '\u00A3';
|
| 35 | 37 | private static final Character objPound = '\u00A3';
|
| 36 | 38 |
|
| 39 | private static Map<Integer,Double> sCurrencyMap = new HashMap<Integer,Double>();
|
| 40 |
|
| 37 | 41 | public static Currency convertToUSD(Currency usd, Currency nonusd, Currency cvt) {
|
| 38 | 42 | if(cvt != null && !cvt.isNull() && cvt.getCurrencyType() != US_DOLLAR) {
|
| 39 | | double multiple = usd.getValue() / nonusd.getValue();
|
| 43 | double multiple;
|
| 44 | if((usd == null || usd.isNull() || usd.getValue() == 0.0 ||
|
| 45 | nonusd == null || nonusd.isNull() || nonusd.getValue() == 0.0) &&
|
| 46 | sCurrencyMap.containsKey(cvt.getCurrencyType())) {
|
| 47 | multiple = sCurrencyMap.get(cvt.getCurrencyType());
|
| 48 | } else {
|
| 49 | multiple = usd.getValue() / nonusd.getValue();
|
| 50 | if(multiple != 0.0) sCurrencyMap.put(nonusd.getCurrencyType(), multiple);
|
| 51 | }
|
| 40 | 52 | return getCurrency(US_DOLLAR, multiple*cvt.getValue());
|
| 41 | 53 | }
|
| 42 | 54 |
|
| --- | --- | |
| 49 | 61 | *
|
| 50 | 62 | * This is used when comparing two currencies of disparate monies.
|
| 51 | 63 | */
|
| 52 | | public class CurrencyTypeException extends Exception {
|
| 64 | public static class CurrencyTypeException extends Exception {
|
| 53 | 65 | String _associatedString;
|
| 54 | 66 |
|
| 55 | 67 | public CurrencyTypeException(String inString) {
|
| --- | --- | |
| 148 | 160 | }
|
| 149 | 161 |
|
| 150 | 162 | public static Currency getCurrency(String wholeValue) {
|
| 151 | | if(wholeValue == null || wholeValue.equals("") || wholeValue.startsWith("UNK")) return NoValue();
|
| 163 | if(wholeValue == null || wholeValue.length() == 0 || wholeValue.startsWith("UNK")) return NoValue();
|
| 152 | 164 |
|
| 153 | 165 | return new Currency(wholeValue);
|
| 154 | 166 | }
|
| --- | --- | |
| 216 | 228 | if(wholeValue == null || wholeValue.equals("null")) {
|
| 217 | 229 | setValues(Currency.NONE, 0.0);
|
| 218 | 230 | } else {
|
| 219 | | String parseCurrency, valuePortion;
|
| 220 | | double actualValue;
|
| 221 | | int euLen, gbpLen, frfLen, cdnLen, chfLen, ntdLen, audLen, usdLen;
|
| 222 | | char firstChar;
|
| 231 | char firstChar = wholeValue.charAt(0);
|
| 223 | 232 |
|
| 224 | | firstChar = wholeValue.charAt(0);
|
| 233 | int eurLen = checkLengthMatchStart(wholeValue, "EUR");
|
| 234 | int gbpLen = checkLengthMatchStart(wholeValue, "GBP");
|
| 235 | int frfLen = checkLengthMatchStart(wholeValue, "FRF");
|
| 236 | int chfLen = checkLengthMatchStart(wholeValue, "CHF");
|
| 237 | int cdnLen = checkLengthMatchStart(wholeValue, "CAD");
|
| 238 | int ntdLen = checkLengthMatchStart(wholeValue, "NTD");
|
| 239 | int audLen = checkLengthMatchStart(wholeValue, "AUD");
|
| 240 | int usdLen = checkLengthMatchStart(wholeValue, "USD");
|
| 225 | 241 |
|
| 226 | | euLen = checkLengthMatchStart(wholeValue, "EUR");
|
| 227 | | gbpLen = checkLengthMatchStart(wholeValue, "GBP");
|
| 228 | | frfLen = checkLengthMatchStart(wholeValue, "FRF");
|
| 229 | | chfLen = checkLengthMatchStart(wholeValue, "CHF");
|
| 230 | | cdnLen = checkLengthMatchStart(wholeValue, "CAD");
|
| 231 | | ntdLen = checkLengthMatchStart(wholeValue, "NTD");
|
| 232 | | audLen = checkLengthMatchStart(wholeValue, "AUD");
|
| 233 | | usdLen = checkLengthMatchStart(wholeValue, "USD");
|
| 234 | |
|
| 242 | String parseCurrency;
|
| 243 | String valuePortion;
|
| 235 | 244 | if(wholeValue.startsWith("US $")) {
|
| 236 | 245 | parseCurrency = "US $";
|
| 237 | 246 | valuePortion = wholeValue.substring(4);
|
| --- | --- | |
| 245 | 254 | } else if(usdLen != 0) {
|
| 246 | 255 | parseCurrency = "USD";
|
| 247 | 256 | valuePortion = wholeValue.substring(usdLen);
|
| 248 | | } else if(euLen != 0) {
|
| 257 | } else if(eurLen != 0) {
|
| 249 | 258 | parseCurrency = "EUR";
|
| 250 | | valuePortion = wholeValue.substring(euLen);
|
| 259 | valuePortion = wholeValue.substring(eurLen);
|
| 251 | 260 | } else if(gbpLen != 0) {
|
| 252 | 261 | parseCurrency = "GBP";
|
| 253 | 262 | valuePortion = wholeValue.substring(gbpLen);
|
| --- | --- | |
| 317 | 326 | }
|
| 318 | 327 |
|
| 319 | 328 | // Kill off non-digit characters.
|
| 320 | | while(!valuePortion.equals("") && !Character.isDigit(valuePortion.charAt(0))) valuePortion = valuePortion.substring(1);
|
| 329 | while(valuePortion.length() != 0 && !Character.isDigit(valuePortion.charAt(0))) valuePortion = valuePortion.substring(1);
|
| 321 | 330 |
|
| 322 | 331 | // If anything's left, try and parse it.
|
| 323 | | if(!valuePortion.equals("")) {
|
| 332 | if(valuePortion.length() != 0) {
|
| 333 | double actualValue;
|
| 324 | 334 | try {
|
| 325 | 335 | actualValue = df.parse(valuePortion).doubleValue();
|
| 326 | 336 | } catch(java.text.ParseException e) {
|
| --- | --- | |
| 533 | 543 | * unequal.
|
| 534 | 544 | */
|
| 535 | 545 | public boolean equals(Object inValue) {
|
| 536 | | boolean sameCurrency, sameValue;
|
| 537 | | Currency otherValue;
|
| 538 | |
|
| 539 | 546 | // Be careful not to compare with null.
|
| 540 | 547 | if(inValue == null) return false;
|
| 541 | 548 | // Shortcut for this.equals(this)
|
| --- | --- | |
| 543 | 550 | // Is it this class even?
|
| 544 | 551 | if(!(inValue instanceof Currency)) return false;
|
| 545 | 552 | // Okay, now cast it because it's safe.
|
| 546 | | otherValue = (Currency)inValue;
|
| 553 | Currency otherValue = (Currency) inValue;
|
| 554 | boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
|
| 555 | boolean sameValue = ((int) (otherValue.getValue() * 1000)) == ((int) (_value * 1000));
|
| 547 | 556 |
|
| 548 | | sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
|
| 549 | | sameValue = ((int)(otherValue.getValue()*1000)) == ((int)(_value*1000));
|
| 550 | |
|
| 551 | 557 | return(sameCurrency && sameValue);
|
| 552 | 558 | }
|
| 553 | 559 |
|
| --- | --- | |
| 566 | 572 | * @throws CurrencyTypeException if you try to compare different currencies.
|
| 567 | 573 | */
|
| 568 | 574 | public boolean less(Currency otherValue) throws CurrencyTypeException {
|
| 569 | | boolean sameCurrency, lowerValue;
|
| 570 | |
|
| 571 | 575 | // Be careful
|
| 572 | 576 | if(otherValue == null) return false;
|
| 573 | 577 | // Shortcut
|
| 574 | 578 | if(otherValue == this) return false;
|
| 575 | 579 |
|
| 576 | | sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
|
| 580 | boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
|
| 577 | 581 | if(!sameCurrency) {
|
| 578 | 582 | throw new CurrencyTypeException("Cannot compare different currencies.");
|
| 579 | 583 | }
|
| 580 | 584 |
|
| 581 | | lowerValue = Double.compare( (double)((int)(otherValue.getValue()*1000)), (double)(int)(_value*1000)) == 1;
|
| 585 | boolean lowerValue = Double.compare((double) ((int) (otherValue.getValue() * 1000)), (double) (int) (_value * 1000)) == 1;
|
| 582 | 586 |
|
| 583 | 587 | return(lowerValue);
|
| 584 | 588 | }
|
| --- | --- | |
| 613 | 617 | * @throws ClassCastException if you try to compareTo non-Currency classes.
|
| 614 | 618 | */
|
| 615 | 619 | public int compareTo(Object o) {
|
| 616 | | Currency otherValue;
|
| 617 | |
|
| 618 | 620 | // We are always greater than null
|
| 619 | 621 | if(o == null) return 1;
|
| 620 | 622 | // We are always equal to ourselves
|
| --- | --- | |
| 623 | 625 | if(!(o instanceof Currency)) throw new ClassCastException("Currency cannot compareTo different classes!");
|
| 624 | 626 |
|
| 625 | 627 | // Okay, now cast it because it's safe.
|
| 626 | | otherValue = (Currency)o;
|
| 628 | Currency otherValue = (Currency) o;
|
| 627 | 629 |
|
| 628 | 630 | if(otherValue.isNull()) return 1;
|
| 629 | 631 | if(isNull()) return -1;
|