NumberFieldValueConvertor.java :  » Development » ivatamasks » com » ivata » mask » field » number » Java Open Source

Java Open Source » Development » ivatamasks 
ivatamasks » com » ivata » mask » field » number » NumberFieldValueConvertor.java
/*
 * Copyright (c) 2001 - 2005 ivata limited.
 * All rights reserved.
 * -----------------------------------------------------------------------------
 * ivata masks may be redistributed under the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License.
 *
 * These programs are free software; you can redistribute them and/or
 * modify them under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2 of the License.
 *
 * These programs are distributed in the hope that they will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License in the file LICENSE.txt for more
 * details.
 *
 * If you would like a copy of the GNU General Public License write to
 *
 * Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA.
 *
 *
 * To arrange commercial support and licensing, contact ivata at
 *                  http://www.ivata.com/contact.jsp
 * -----------------------------------------------------------------------------
 * $Log: NumberFieldValueConvertor.java,v $
 * Revision 1.7  2005/10/12 18:36:36  colinmacleod
 * Standardized format of Logger declaration - to make it easier to find instances
 * which are not both static and final.
 *
 * Revision 1.6  2005/10/02 14:06:32  colinmacleod
 * Added/improved log4j logging.
 *
 * Revision 1.5  2005/09/14 12:51:52  colinmacleod
 * Added serialVersionUID.
 *
 * Revision 1.4  2005/04/11 12:27:01  colinmacleod
 * Added preliminary support for filters.
 * Added FieldValueConvertor factor interface
 * to split off value convertors for reuse.
 *
 * Revision 1.3  2005/04/09 18:04:15  colinmacleod
 * Changed copyright text to GPL v2 explicitly.
 *
 * Revision 1.2  2005/01/06 22:13:22  colinmacleod
 * Moved up a version number.
 * Changed copyright notices to 2005.
 * Updated the documentation:
 *   - started working on multiproject:site docu.
 *   - changed the logo.
 * Added checkstyle and fixed LOADS of style issues.
 * Added separate thirdparty subproject.
 * Added struts (in web), util and webgui (in webtheme) from ivata op.
 *
 * Revision 1.1  2004/12/29 20:07:07  colinmacleod
 * Renamed subproject masks to mask.
 *
 * Revision 1.2  2004/11/11 13:35:15  colinmacleod
 * Added log4j logging.
 *
 * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
 * Ready for 0.1 release
 * -----------------------------------------------------------------------------
 */
package com.ivata.mask.field.number;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.apache.log4j.Logger;
import com.ivata.mask.field.FieldValueConvertor;
/**
 * This convertor is used when the field value is a string representing a number
 * or floating point (such as an amount or rate).
 *
 * @since ivata masks 0.1 (2004-05-14)
 * @author Colin MacLeod
 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
 * @version $Revision: 1.7 $
 */
public class NumberFieldValueConvertor extends FieldValueConvertor {
    /**
     * Serialization version (for <code>Serializable</code> interface).
     */
    private static final long serialVersionUID = 1L;
    /**
     * <p>
     * This format does all the hard work!
     * </p>
     */
    private NumberFormat format;
    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(
            NumberFieldValueConvertor.class);
    /**
     * <p>
     * Construct a new value convertor from the format pattern supplied.
     * </p>
     *
     * @param pattern
     *            number format pattern to use to convert the number. See
     *            {@link java.text.DecimalFormat}.
     */
    public NumberFieldValueConvertor(final String pattern) {
        format = new DecimalFormat(pattern);
    }
    /**
     * <p>
     * Convert an object representing a number to a string.
     * </p>
     *
     * @param objectValue
     *            object to be converted.
     * @return string equivalent of the object provided.
     * @see com.ivata.mask.web.field.FieldValueConvertor#toString
     */
    protected String toString(final Object objectValue) {
        if (logger.isDebugEnabled()) {
            logger.debug("toString(Object objectValue = " + objectValue
                    + ") - start");
        }

        if (objectValue == null) {
            String returnString = format.format(0);
            if (logger.isDebugEnabled()) {
                logger.debug("toString(Object) - end - return value = "
                        + returnString);
            }
            return returnString;
        }
        Double doubleValue;
        try {
            Class objectClass = objectValue.getClass();
            Method method = objectClass
                    .getMethod("doubleValue", new Class[] {});
            doubleValue = (Double) method.invoke(objectValue, new Object[] {});
        } catch (IllegalAccessException e) {
            logger.error("toString(Object)", e);

            throw new FieldValueException(e);
        } catch (InvocationTargetException e) {
            logger.error("toString(Object)", e);

            throw new FieldValueException(e);
        } catch (NoSuchMethodException e) {
            logger.error("ERROR: no method called '" + "doubleValue"
                    + "' in instance of '" + objectValue.getClass()
                    + "', value '" + objectValue + "'");
            throw new FieldValueException(e);
        } catch (Exception e) {
            logger.error("toString(Object)", e);

            throw new FieldValueException(e);
        }
        String returnString = format.format(doubleValue.doubleValue());
        if (logger.isDebugEnabled()) {
            logger.debug("toString(Object) - end - return value = "
                    + returnString);
        }
        return returnString;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.