001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: NumberFieldValueConvertor.java,v $
031: * Revision 1.7 2005/10/12 18:36:36 colinmacleod
032: * Standardized format of Logger declaration - to make it easier to find instances
033: * which are not both static and final.
034: *
035: * Revision 1.6 2005/10/02 14:06:32 colinmacleod
036: * Added/improved log4j logging.
037: *
038: * Revision 1.5 2005/09/14 12:51:52 colinmacleod
039: * Added serialVersionUID.
040: *
041: * Revision 1.4 2005/04/11 12:27:01 colinmacleod
042: * Added preliminary support for filters.
043: * Added FieldValueConvertor factor interface
044: * to split off value convertors for reuse.
045: *
046: * Revision 1.3 2005/04/09 18:04:15 colinmacleod
047: * Changed copyright text to GPL v2 explicitly.
048: *
049: * Revision 1.2 2005/01/06 22:13:22 colinmacleod
050: * Moved up a version number.
051: * Changed copyright notices to 2005.
052: * Updated the documentation:
053: * - started working on multiproject:site docu.
054: * - changed the logo.
055: * Added checkstyle and fixed LOADS of style issues.
056: * Added separate thirdparty subproject.
057: * Added struts (in web), util and webgui (in webtheme) from ivata op.
058: *
059: * Revision 1.1 2004/12/29 20:07:07 colinmacleod
060: * Renamed subproject masks to mask.
061: *
062: * Revision 1.2 2004/11/11 13:35:15 colinmacleod
063: * Added log4j logging.
064: *
065: * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
066: * Ready for 0.1 release
067: * -----------------------------------------------------------------------------
068: */
069: package com.ivata.mask.field.number;
070:
071: import java.lang.reflect.InvocationTargetException;
072: import java.lang.reflect.Method;
073: import java.text.DecimalFormat;
074: import java.text.NumberFormat;
075: import org.apache.log4j.Logger;
076: import com.ivata.mask.field.FieldValueConvertor;
077:
078: /**
079: * This convertor is used when the field value is a string representing a number
080: * or floating point (such as an amount or rate).
081: *
082: * @since ivata masks 0.1 (2004-05-14)
083: * @author Colin MacLeod
084: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
085: * @version $Revision: 1.7 $
086: */
087: public class NumberFieldValueConvertor extends FieldValueConvertor {
088: /**
089: * Serialization version (for <code>Serializable</code> interface).
090: */
091: private static final long serialVersionUID = 1L;
092: /**
093: * <p>
094: * This format does all the hard work!
095: * </p>
096: */
097: private NumberFormat format;
098: /**
099: * Logger for this class.
100: */
101: private static final Logger logger = Logger
102: .getLogger(NumberFieldValueConvertor.class);
103:
104: /**
105: * <p>
106: * Construct a new value convertor from the format pattern supplied.
107: * </p>
108: *
109: * @param pattern
110: * number format pattern to use to convert the number. See
111: * {@link java.text.DecimalFormat}.
112: */
113: public NumberFieldValueConvertor(final String pattern) {
114: format = new DecimalFormat(pattern);
115: }
116:
117: /**
118: * <p>
119: * Convert an object representing a number to a string.
120: * </p>
121: *
122: * @param objectValue
123: * object to be converted.
124: * @return string equivalent of the object provided.
125: * @see com.ivata.mask.web.field.FieldValueConvertor#toString
126: */
127: protected String toString(final Object objectValue) {
128: if (logger.isDebugEnabled()) {
129: logger.debug("toString(Object objectValue = " + objectValue
130: + ") - start");
131: }
132:
133: if (objectValue == null) {
134: String returnString = format.format(0);
135: if (logger.isDebugEnabled()) {
136: logger.debug("toString(Object) - end - return value = "
137: + returnString);
138: }
139: return returnString;
140: }
141: Double doubleValue;
142: try {
143: Class objectClass = objectValue.getClass();
144: Method method = objectClass.getMethod("doubleValue",
145: new Class[] {});
146: doubleValue = (Double) method.invoke(objectValue,
147: new Object[] {});
148: } catch (IllegalAccessException e) {
149: logger.error("toString(Object)", e);
150:
151: throw new FieldValueException(e);
152: } catch (InvocationTargetException e) {
153: logger.error("toString(Object)", e);
154:
155: throw new FieldValueException(e);
156: } catch (NoSuchMethodException e) {
157: logger.error("ERROR: no method called '" + "doubleValue"
158: + "' in instance of '" + objectValue.getClass()
159: + "', value '" + objectValue + "'");
160: throw new FieldValueException(e);
161: } catch (Exception e) {
162: logger.error("toString(Object)", e);
163:
164: throw new FieldValueException(e);
165: }
166: String returnString = format.format(doubleValue.doubleValue());
167: if (logger.isDebugEnabled()) {
168: logger.debug("toString(Object) - end - return value = "
169: + returnString);
170: }
171: return returnString;
172: }
173: }
|