1 /*
2 * Copyright (c) 2001 - 2005 ivata limited.
3 * All rights reserved.
4 * -----------------------------------------------------------------------------
5 * ivata masks may be redistributed under the GNU General Public
6 * License as published by the Free Software Foundation;
7 * version 2 of the License.
8 *
9 * These programs are free software; you can redistribute them and/or
10 * modify them under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2 of the License.
12 *
13 * These programs are distributed in the hope that they will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * See the GNU General Public License in the file LICENSE.txt for more
18 * details.
19 *
20 * If you would like a copy of the GNU General Public License write to
21 *
22 * Free Software Foundation, Inc.
23 * 59 Temple Place - Suite 330
24 * Boston, MA 02111-1307, USA.
25 *
26 *
27 * To arrange commercial support and licensing, contact ivata at
28 * http://www.ivata.com/contact.jsp
29 * -----------------------------------------------------------------------------
30 * $Log: NumberFieldValueConvertor.java,v $
31 * Revision 1.4 2005/04/11 12:27:01 colinmacleod
32 * Added preliminary support for filters.
33 * Added FieldValueConvertor factor interface
34 * to split off value convertors for reuse.
35 *
36 * Revision 1.3 2005/04/09 18:04:15 colinmacleod
37 * Changed copyright text to GPL v2 explicitly.
38 *
39 * Revision 1.2 2005/01/06 22:13:22 colinmacleod
40 * Moved up a version number.
41 * Changed copyright notices to 2005.
42 * Updated the documentation:
43 * - started working on multiproject:site docu.
44 * - changed the logo.
45 * Added checkstyle and fixed LOADS of style issues.
46 * Added separate thirdparty subproject.
47 * Added struts (in web), util and webgui (in webtheme) from ivata op.
48 *
49 * Revision 1.1 2004/12/29 20:07:07 colinmacleod
50 * Renamed subproject masks to mask.
51 *
52 * Revision 1.2 2004/11/11 13:35:15 colinmacleod
53 * Added log4j logging.
54 *
55 * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
56 * Ready for 0.1 release
57 * -----------------------------------------------------------------------------
58 */
59 package com.ivata.mask.field.number;
60 import java.lang.reflect.InvocationTargetException;
61 import java.lang.reflect.Method;
62 import java.text.DecimalFormat;
63 import java.text.NumberFormat;
64 import org.apache.log4j.Logger;
65 import com.ivata.mask.field.FieldValueConvertor;
66 /***
67 * This convertor is used when the field value is a string representing a number
68 * or floating point (such as an amount or rate).
69 *
70 * @since ivata masks 0.1 (2004-05-14)
71 * @author Colin MacLeod
72 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
73 * @version $Revision: 1.4 $
74 */
75 public class NumberFieldValueConvertor extends FieldValueConvertor {
76 /***
77 * <p>
78 * This format does all the hard work!
79 * </p>
80 */
81 private NumberFormat format;
82 /***
83 * <p>
84 * This log provides tracing and debugging information.
85 * </p>
86 */
87 private Logger log = Logger.getLogger(NumberFieldValueConvertor.class);
88 /***
89 * <p>
90 * Construct a new value convertor from the format pattern supplied.
91 * </p>
92 *
93 * @param pattern
94 * number format pattern to use to convert the number. See
95 * {@link java.text.DecimalFormat}.
96 */
97 public NumberFieldValueConvertor(final String pattern) {
98 format = new DecimalFormat(pattern);
99 }
100 /***
101 * <p>
102 * Convert an object representing a number to a string.
103 * </p>
104 *
105 * @param objectValue
106 * object to be converted.
107 * @return string equivalent of the object provided.
108 * @see com.ivata.mask.web.field.FieldValueConvertor#toString
109 */
110 protected String toString(final Object objectValue) {
111 if (objectValue == null) {
112 return format.format(0);
113 }
114 Double doubleValue;
115 try {
116 Class objectClass = objectValue.getClass();
117 Method method = objectClass
118 .getMethod("doubleValue", new Class[] {});
119 doubleValue = (Double) method.invoke(objectValue, new Object[] {});
120 } catch (IllegalAccessException e) {
121 throw new FieldValueException(e);
122 } catch (InvocationTargetException e) {
123 throw new FieldValueException(e);
124 } catch (NoSuchMethodException e) {
125 log.error("ERROR: no method called '" + "doubleValue"
126 + "' in instance of '" + objectValue.getClass()
127 + "', value '" + objectValue + "'");
128 throw new FieldValueException(e);
129 } catch (Exception e) {
130 throw new FieldValueException(e);
131 }
132 return format.format(doubleValue.doubleValue());
133 }
134 }