avatar

598

Fix the retrieval of the USD equivalent amounts for auctions in non-USD currencies. The USD equivalent is used for sorting different currencies together. [#279 state:resolved tagged:committed]

by mrs, 13 Aug, 2008 09:50 AM
560 598  
113113       if (val.getCurrencyType() == Currency.US_DOLLAR) {
114114         newCur = val;
115115       } else {
116         newCur = walkForUSCurrency(_htmlDoc, newCur);
116         newCur = walkForUSCurrency(_htmlDoc);
117117       }
118118     }
119119 
------
125125    * Skim forward until we either find something, or give up.  (6 steps for now.)
126126    *
127127    * @param html   - The document to search.
128    * @param newCur - The current high bid amount.
129128    * @return - Either zeroDollars or the approximate USD equivalent of the value of the item.
130129    */
131   private Currency walkForUSCurrency(JHTML html, Currency newCur) {
130   private Currency walkForUSCurrency(JHTML html) {
131     Currency newCur = zeroDollars;
132132     int count = 0;
133133 
134134     String usdPattern = Externalized.getString("ebayServer.USD");
------
142142         newCur = Currency.getCurrency(approxAmount);
143143         if (newCur.getCurrencyType() != Currency.US_DOLLAR) newCur = zeroDollars;
144144       }
145     } while (count++ < 6 && newCur != zeroDollars);
145     } while (count++ < 6 && newCur == zeroDollars);
146146 
147147     return newCur;
148148   }
508 598  
88 import com.jbidwatcher.util.config.ErrorManagement;
99 import java.text.NumberFormat;
1010 import java.util.Locale;
11 import java.util.Map;
12 import java.util.HashMap;
1113 
1214 public class Currency implements Comparable {
1315   private static NumberFormat df = NumberFormat.getNumberInstance(Locale.US); // We create a lot of these, so minimizing memory usage is good.
------
3436   private static final char pound = '\u00A3';
3537   private static final Character objPound = '\u00A3';
3638 
39   private static Map<Integer,Double> sCurrencyMap = new HashMap<Integer,Double>();
40 
3741   public static Currency convertToUSD(Currency usd, Currency nonusd, Currency cvt) {
3842     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       }
4052       return getCurrency(US_DOLLAR, multiple*cvt.getValue());
4153     }
4254 
------
4961    *
5062    * This is used when comparing two currencies of disparate monies.
5163    */
52   public class CurrencyTypeException extends Exception {
64   public static class CurrencyTypeException extends Exception {
5365     String _associatedString;
5466 
5567     public CurrencyTypeException(String inString) {
------
148160   }
149161 
150162   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();
152164 
153165     return new Currency(wholeValue);
154166   }
------
216228     if(wholeValue == null || wholeValue.equals("null")) {
217229       setValues(Currency.NONE, 0.0);
218230     } 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);
223232 
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");
225241 
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;
235244       if(wholeValue.startsWith("US $")) {
236245         parseCurrency = "US $";
237246         valuePortion = wholeValue.substring(4);
------
245254       } else if(usdLen != 0) {
246255         parseCurrency = "USD";
247256         valuePortion = wholeValue.substring(usdLen);
248       } else if(euLen != 0) {
257       } else if(eurLen != 0) {
249258         parseCurrency = "EUR";
250         valuePortion = wholeValue.substring(euLen);
259         valuePortion = wholeValue.substring(eurLen);
251260       } else if(gbpLen != 0) {
252261         parseCurrency = "GBP";
253262         valuePortion = wholeValue.substring(gbpLen);
------
317326       }
318327 
319328       //  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);
321330 
322331       //  If anything's left, try and parse it.
323       if(!valuePortion.equals("")) {
332       if(valuePortion.length() != 0) {
333         double actualValue;
324334         try {
325335           actualValue = df.parse(valuePortion).doubleValue();
326336         } catch(java.text.ParseException e) {
------
533543    * unequal.
534544    */
535545   public boolean equals(Object inValue) {
536     boolean sameCurrency, sameValue;
537     Currency otherValue;
538 
539546     //  Be careful not to compare with null.
540547     if(inValue == null) return false;
541548     //  Shortcut for this.equals(this)
------
543550     //  Is it this class even?
544551     if(!(inValue instanceof Currency)) return false;
545552     //  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));
547556 
548     sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
549     sameValue = ((int)(otherValue.getValue()*1000)) == ((int)(_value*1000));
550 
551557     return(sameCurrency && sameValue);
552558   }
553559 
------
566572    * @throws CurrencyTypeException if you try to compare different currencies.
567573    */
568574   public boolean less(Currency otherValue) throws CurrencyTypeException {
569     boolean sameCurrency, lowerValue;
570 
571575     //  Be careful
572576     if(otherValue == null) return false;
573577     //  Shortcut
574578     if(otherValue == this) return false;
575579 
576     sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
580     boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
577581     if(!sameCurrency) {
578582       throw new CurrencyTypeException("Cannot compare different currencies.");
579583     }
580584 
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;
582586 
583587     return(lowerValue);
584588   }
------
613617    * @throws ClassCastException if you try to compareTo non-Currency classes.
614618    */
615619   public int compareTo(Object o) {
616     Currency otherValue;
617 
618620     //  We are always greater than null
619621     if(o == null) return 1;
620622     //  We are always equal to ourselves
------
623625     if(!(o instanceof Currency)) throw new ClassCastException("Currency cannot compareTo different classes!");
624626 
625627     //  Okay, now cast it because it's safe.
626     otherValue = (Currency)o;
628     Currency otherValue = (Currency) o;
627629 
628630     if(otherValue.isNull()) return 1;
629631     if(isNull()) return -1;