View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.util;
24  
25  import java.text.DecimalFormat;
26  import java.text.ParseException;
27  
28  import org.apache.commons.beanutils.ConversionException;
29  import org.apache.commons.beanutils.Converter;
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * This class is converts a Double to a double-digit String (and vise-versa) by
36   * BeanUtils when copying properties.
37   * 
38   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
39   */
40  public class CurrencyConverter implements Converter {
41      private final Log log = LogFactory.getLog(CurrencyConverter.class);
42  
43      private DecimalFormat formatter = new DecimalFormat("###,###.00");
44  
45      public void setDecimalFormatter(DecimalFormat df) {
46  	this.formatter = df;
47      }
48  
49      /**
50       * Convert a String to a Double and a Double to a String
51       * 
52       * @param type
53       *                the class type to output
54       * @param value
55       *                the object to convert
56       * @return object the converted object (Double or String)
57       */
58      @SuppressWarnings("unchecked")
59      public final Object convert(final Class type, final Object value) {
60  	// for a null value, return null
61  	if (value == null) {
62  	    return null;
63  	} else {
64  	    if (value instanceof String) {
65  		if (log.isDebugEnabled()) {
66  		    log.debug("value (" + value + ") instance of String");
67  		}
68  
69  		try {
70  		    if (StringUtils.isBlank(String.valueOf(value))) {
71  			return null;
72  		    }
73  
74  		    if (log.isDebugEnabled()) {
75  			log.debug("converting '" + value + "' to a decimal");
76  		    }
77  
78  		    // formatter.setDecimalSeparatorAlwaysShown(true);
79  		    Number num = formatter.parse(String.valueOf(value));
80  
81  		    return num.doubleValue();
82  		} catch (ParseException pe) {
83  		}
84  	    } else if (value instanceof Double) {
85  		if (log.isDebugEnabled()) {
86  		    log.debug("value (" + value + ") instance of Double");
87  		    log.debug("returning double: " + formatter.format(value));
88  		}
89  
90  		return formatter.format(value);
91  	    }
92  	}
93  
94  	throw new ConversionException("Could not convert " + value + " to "
95  		+ type.getName() + "!");
96      }
97  }