avatar

945

Provide a default persistence delegate for currency objects, so they can be serialized and sent through the queues.

by mrs, 06 Jun, 2009 05:53 AM
706 945  
1111 import java.util.Locale;
1212 import java.util.Map;
1313 import java.util.HashMap;
14 import java.beans.PersistenceDelegate;
15 import java.beans.DefaultPersistenceDelegate;
1416 
1517 public class Currency implements Comparable {
1618   private static NumberFormat df = NumberFormat.getNumberInstance(Locale.US); // We create a lot of these, so minimizing memory usage is good.
------
3234     return _noValue;
3335   }
3436 
35   private int _whatCurrency;
36   private double _value;
37   protected int mCurrencyType;
38   protected double mValue;
3739   private static final char pound = '\u00A3';
3840   private static final Character objPound = '\u00A3';
3941 
------
393395    * @param startValue - The amount represented.
394396    */
395397   private void setValues(int whatType, double startValue) {
396     _whatCurrency = whatType;
397     _value = startValue;
398     mCurrencyType = whatType;
399     mValue = startValue;
398400     df.setMinimumFractionDigits(2);
399401     df.setMaximumFractionDigits(2);
400402   }
------
406408    * @return A string containing a full ISO currency name.
407409    */
408410   public String fullCurrencyName() {
409     switch(_whatCurrency) {
411     switch(mCurrencyType) {
410412       case US_DOLLAR: return("USD");
411413       case AU_DOLLAR: return("AUD");
412414       case NT_DOLLAR: return("NTD");
------
425427     }
426428   }
427429 
428   public double getValue() { return _value; }
430   public double getValue() { return mValue; }
429431 
430432   public String fullCurrency() {
431433     return fullCurrencyName() + " " + getValueString();
------
446448   public Currency add(Currency addValue) throws CurrencyTypeException {
447449     if(addValue == null) throw new CurrencyTypeException("Cannot add null Currency.");
448450 
449     if(addValue.getCurrencyType() == _whatCurrency) {
450       return new Currency(_whatCurrency, _value + addValue.getValue());
451     if(addValue.getCurrencyType() == mCurrencyType) {
452       return new Currency(mCurrencyType, mValue + addValue.getValue());
451453     }
452454 
453455     //  If only one currency is known, return the result as the known currency.
454     if (_whatCurrency == NONE) return new Currency(addValue.getCurrencyType(), _value + addValue.getValue());
455     if (addValue.getCurrencyType() == NONE) return new Currency(_whatCurrency, _value + addValue.getValue());
456     if (mCurrencyType == NONE) return new Currency(addValue.getCurrencyType(), mValue + addValue.getValue());
457     if (addValue.getCurrencyType() == NONE) return new Currency(mCurrencyType, mValue + addValue.getValue());
456458 
457459     throw new CurrencyTypeException("Cannot add " + fullCurrencyName() + " to " + addValue.fullCurrencyName() + ".");
458460   }
------
472474   public Currency subtract(Currency subValue) throws CurrencyTypeException {
473475     if(subValue == null) throw new CurrencyTypeException("Cannot subtract null Currency.");
474476 
475     if(subValue.getCurrencyType() == _whatCurrency) {
476       return new Currency(_whatCurrency, _value - subValue.getValue());
477     if(subValue.getCurrencyType() == mCurrencyType) {
478       return new Currency(mCurrencyType, mValue - subValue.getValue());
477479     }
478480 
479481     //  If only one currency is known, return the result as the known currency.
480     if(_whatCurrency == NONE) return new Currency(subValue.getCurrencyType(), _value - subValue.getValue());
481     if(subValue.getCurrencyType() == NONE) return new Currency(_whatCurrency, _value - subValue.getValue());
482     if(mCurrencyType == NONE) return new Currency(subValue.getCurrencyType(), mValue - subValue.getValue());
483     if(subValue.getCurrencyType() == NONE) return new Currency(mCurrencyType, mValue - subValue.getValue());
482484 
483485     throw new CurrencyTypeException("Cannot subtract " + fullCurrencyName() + " from " + subValue.fullCurrencyName() + ".");
484486   }
485487 
486   public int getCurrencyType() { return _whatCurrency; }
488   public int getCurrencyType() { return mCurrencyType; }
487489 
488490   public String getCurrencySymbol() {
489     switch(_whatCurrency) {
491     switch(mCurrencyType) {
490492       case US_DOLLAR: return("$");
491493       case NT_DOLLAR: return("nt$");
492494       case HK_DOLLAR: return("hk$");
------
522524     } else {
523525       String cvtToString = getCurrencySymbol();
524526 
525       cvtToString += df.format(_value);
527       cvtToString += df.format(mValue);
526528 
527529       return(cvtToString);
528530     }
------
542544     if(isNull()) {
543545       return("null");
544546     } else {
545       return df.format(_value);
547       return df.format(mValue);
546548     }
547549   }
548550 
------
579581     if(!(inValue instanceof Currency)) return false;
580582     //  Okay, now cast it because it's safe.
581583     Currency otherValue = (Currency) inValue;
582     boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
583     boolean sameValue = ((int) (otherValue.getValue() * 1000)) == ((int) (_value * 1000));
584     boolean sameCurrency = (otherValue.getCurrencyType() == mCurrencyType);
585     boolean sameValue = ((int) (otherValue.getValue() * 1000)) == ((int) (mValue * 1000));
584586 
585587     return(sameCurrency && sameValue);
586588   }
------
605607     //  Shortcut
606608     if(otherValue == this) return false;
607609 
608     boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
610     boolean sameCurrency = (otherValue.getCurrencyType() == mCurrencyType);
609611     if(!sameCurrency) {
610612       throw new CurrencyTypeException("Cannot compare different currencies.");
611613     }
612614 
613     boolean lowerValue = Double.compare((double) ((int) (otherValue.getValue() * 1000)), (double) (int) (_value * 1000)) == 1;
615     boolean lowerValue = Double.compare((double) ((int) (otherValue.getValue() * 1000)), (double) (int) (mValue * 1000)) == 1;
614616 
615617     return(lowerValue);
616618   }
------
623625    * @return True if this is a 'null currency' object.
624626    */
625627   public boolean isNull() {
626     return(_value == 0.0 && _whatCurrency == NONE);
628     return(mValue == 0.0 && mCurrencyType == NONE);
627629   }
628630 
629631   /** 
------
669671     if(equals(otherValue)) return 0;
670672     return 1;
671673   }
674 
675   public static PersistenceDelegate getDelegate() {
676     return new DefaultPersistenceDelegate(new String[]{"mCurrencyType", "mValue"});
677   }
672678 }
901 945  
99 import java.beans.*;
1010 import java.io.ByteArrayOutputStream;
1111 import java.io.ByteArrayInputStream;
12 import com.jbidwatcher.util.Currency;
1213 
1314 public abstract class MessageQueue implements Runnable {
1415   protected final LinkedList<Object> _queue = new LinkedList<Object>();
------
3435     // Create output stream.
3536     ByteArrayOutputStream fos = new ByteArrayOutputStream();
3637     XMLEncoder xe2 = new XMLEncoder(fos);
38     xe2.setPersistenceDelegate(Currency.class, Currency.getDelegate());
3739     xe2.writeObject(xe);
3840     xe2.close();
3941     enqueue(fos.toString());