KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > util > CurrencyConverter


1 package com.blandware.atleap.common.util;
2
3 import org.apache.commons.beanutils.ConversionException;
4 import org.apache.commons.beanutils.Converter;
5 import org.apache.commons.lang.StringUtils;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import java.text.DecimalFormat JavaDoc;
10 import java.text.ParseException JavaDoc;
11
12
13 /**
14  * <p>This class converts a Double to a double-digit String
15  * (and vise versa) by BeanUtils when copying properties.
16  * Registered for use in BaseAction.
17  * </p>
18  * <p><a HREF="CurrencyConverter.java.htm"><i>View Source</i></a>
19  * </p>
20  *
21  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
22  * @version $Revision: 1.5 $ $Date: 2005/08/02 14:53:32 $
23  */

24 public class CurrencyConverter implements Converter {
25     //~ Instance fields ========================================================
26

27     protected transient final Log log = LogFactory.getLog(CurrencyConverter.class);
28     protected final DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("###,###.00");
29
30     //~ Methods ================================================================
31

32     /**
33      * Converts a String to a Double and a Double to a String
34      *
35      * @param type the class type to output
36      * @param value the object to convert
37      * @return object the converted object (Double or String)
38      */

39     public final Object JavaDoc convert(final Class JavaDoc type, final Object JavaDoc value) {
40         // for a null value, return null
41
if ( value == null ) {
42             return null;
43         } else {
44             if ( value instanceof String JavaDoc ) {
45                 if ( log.isDebugEnabled() ) {
46                     log.debug("value (" + value + ") instance of String");
47                 }
48
49                 try {
50                     if ( StringUtils.isBlank(String.valueOf(value)) ) {
51                         return null;
52                     }
53
54                     if ( log.isDebugEnabled() ) {
55                         log.debug("converting '" + value + "' to a decimal");
56                     }
57
58                     //formatter.setDecimalSeparatorAlwaysShown(true);
59
Number JavaDoc num = formatter.parse(String.valueOf(value));
60
61                     return new Double JavaDoc(num.doubleValue());
62                 } catch ( ParseException JavaDoc pe ) {
63                     pe.printStackTrace();
64                 }
65             } else if ( value instanceof Double JavaDoc ) {
66                 if ( log.isDebugEnabled() ) {
67                     log.debug("value (" + value + ") instance of Double");
68                     log.debug("returning double: " + formatter.format(value));
69                 }
70
71                 return formatter.format(value);
72             }
73         }
74
75         throw new ConversionException("Could not convert " + value + " to " +
76                 type.getName() + "!");
77     }
78 }
79
Popular Tags