Example usage for org.apache.commons.beanutils.converters BigDecimalConverter BigDecimalConverter

List of usage examples for org.apache.commons.beanutils.converters BigDecimalConverter BigDecimalConverter

Introduction

In this page you can find the example usage for org.apache.commons.beanutils.converters BigDecimalConverter BigDecimalConverter.

Prototype

public BigDecimalConverter(Object defaultValue) 

Source Link

Document

Create a Converter that will return the specified default value if a conversion error occurs.

Usage

From source file:com.autobizlogic.abl.logic.LogicSvcs.java

/**
 * /*from w  ww . j a  v a  2  s.c  o  m*/
 * @param aSourceHibernateBean
 * @param aLogicRunner - just for context
 * @return 2nd instance of aSourceHibernateBean, with non-collection properties
 */
public static Object beanCopyOf(Object aSourceHibernateBean, LogicRunner aLogicRunner) {
    Object rtnTargetHibernateBean = null;
    LogicTransactionContext context = aLogicRunner.getContext();

    BigDecimal defaultValue = null; //new BigDecimal("0.0");  // an experiment
    Converter bdc = new BigDecimalConverter(defaultValue);
    ConvertUtils.register(bdc, BigDecimal.class);

    Class<?> hibernateDomainBeanClass = HibernateUtil.getEntityClassForBean(aSourceHibernateBean);
    ClassMetadata entityMeta = context.getSession().getSessionFactory()
            .getClassMetadata(hibernateDomainBeanClass);
    Type propertyTypes[] = entityMeta.getPropertyTypes();
    String propertyNames[] = entityMeta.getPropertyNames(); // hope names/types share index...

    String className = hibernateDomainBeanClass.getName();
    try {
        rtnTargetHibernateBean = ClassLoaderManager.getInstance().getClassFromName(className).newInstance();
    } catch (Exception e) {
        throw new LogicException("Unable to instantatiate new Bean like: " + aSourceHibernateBean, e);
    }
    BeanMap rtnBeanMap = new BeanMap(rtnTargetHibernateBean);
    BeanMap srcBeanMap = new BeanMap(aSourceHibernateBean);
    Object eachValue = null;
    for (int i = 0; i < propertyNames.length; i++) {
        String propertyName = propertyNames[i];
        try {
            Type type = propertyTypes[i];
            if (!type.isCollectionType() && !type.isEntityType()) { // NB - not moving collections!
                eachValue = srcBeanMap.get(propertyName);
                rtnBeanMap.put(propertyName, eachValue);
            } else {
                BeanUtils.setProperty(rtnTargetHibernateBean, propertyNames[i], null); // prevent ClassCastException: HashSet cannot be cast to PersistentCollection
            }
        } catch (Exception e) {
            throw new LogicException("Cannot set Property: " + propertyNames[i] + " = " + eachValue + ", on "
                    + rtnTargetHibernateBean);
        }
    }
    // TODO - IMPORTANT - set the key field(s), presumably using Hibernate meta data      
    return rtnTargetHibernateBean;
}

From source file:io.milton.http.annotated.DataBinder.java

public DataBinder() {
    ConvertUtilsBean2 convertUtilsBean = new ConvertUtilsBean2();
    NullSafeDateTimeConverter dtConverter = new NullSafeDateTimeConverter();
    dtConverter.setPatterns(dateFormats);
    convertUtilsBean.register(dtConverter, Date.class);

    BigDecimalConverter bdConverter = new BigDecimalConverter(null);
    convertUtilsBean.register(bdConverter, BigDecimal.class);

    bub = new BeanUtilsBean(convertUtilsBean);
}

From source file:com.sunchenbin.store.feilong.core.bean.ConvertUtil.java

/**
 * object?? {@link java.math.BigDecimal}.
 * //from w w  w.j a v a 2s . co  m
 * <p>
 * converted is missing or an error occurs converting the value,<span style="color:red">return null</span>
 * </p>
 * 
 * <h3>{@link java.lang.Double} ? {@link java.math.BigDecimal}?:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * <span style="color:red">?? {@link BigDecimal#valueOf(double)}</span>,? {@code new BigDecimal(double)},?? JDK API<br>
 * </p>
 * 
 * <ul>
 * <li>{@code new BigDecimal(0.1) ====> 0.1000000000000000055511151231257827021181583404541015625}</li>
 * <li>{@code BigDecimal.valueOf(0.1) ====> 0.1}</li>
 * </ul>
 * 
 *  {@link NumberConverter#toNumber(Class, Class, Number)
 * NumberConverter#toNumber(Class, Class, Number)},? {@link java.lang.Double} ? {@link java.math.BigDecimal} </blockquote>
 * 
 * @param toBeConvertedValue
 *            
 * @return BigDecimal
 * @see #convert(Object, Class)
 * @see org.apache.commons.beanutils.converters.NumberConverter#toNumber(Class, Class, Number)
 * @see org.apache.commons.beanutils.converters.BigDecimalConverter
 */
public static BigDecimal toBigDecimal(Object toBeConvertedValue) {
    BigDecimalConverter bigDecimalConverter = new BigDecimalConverter(null);
    return bigDecimalConverter.convert(BigDecimal.class, toBeConvertedValue);
}

From source file:com.feilong.core.bean.ConvertUtil.java

/**
 *  <code>toBeConvertedValue</code> ?? {@link java.math.BigDecimal}.
 * /*  w w  w  .  ja va2s  .c  o  m*/
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * ConvertUtil.toBigDecimal(1111) = BigDecimal.valueOf(1111)
 * </pre>
 * 
 * </blockquote>
 * 
 * <h3>{@link java.lang.Double} ? {@link java.math.BigDecimal}?:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * <span style="color:red">?? {@link BigDecimal#valueOf(double)}</span>,? <code>new BigDecimal(double)</code>,?? JDK API<br>
 * </p>
 * 
 * <ul>
 * <li>{@code new BigDecimal(0.1) ====> 0.1000000000000000055511151231257827021181583404541015625}</li>
 * <li>{@code BigDecimal.valueOf(0.1) ====> 0.1}</li>
 * </ul>
 * 
 *  {@link NumberConverter#toNumber(Class, Class, Number)
 * NumberConverter#toNumber(Class, Class, Number)},? {@link java.lang.Double} ? {@link java.math.BigDecimal} </blockquote>
 * 
 * @param toBeConvertedValue
 *            
 * @return  <code>toBeConvertedValue</code> null, null<br>
 *         ???, null
 * @see #convert(Object, Class)
 * @see org.apache.commons.beanutils.converters.NumberConverter#toNumber(Class, Class, Number)
 * @see org.apache.commons.beanutils.converters.BigDecimalConverter
 */
public static BigDecimal toBigDecimal(Object toBeConvertedValue) {
    return new BigDecimalConverter(null).convert(BigDecimal.class, toBeConvertedValue);
}

From source file:org.apache.struts.action.ActionServlet.java

/**
 * <p>Initialize other global characteristics of the controller
 * servlet.</p>/*from  ww  w  .  j a v a  2 s  .c  o  m*/
 *
 * @throws ServletException if we cannot initialize these resources
 */
protected void initOther() throws ServletException {
    String value;

    value = getServletConfig().getInitParameter("config");

    if (value != null) {
        config = value;
    }

    // Backwards compatibility for form beans of Java wrapper classes
    // Set to true for strict Struts 1.0 compatibility
    value = getServletConfig().getInitParameter("convertNull");

    if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)
            || "y".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) {
        convertNull = true;
    }

    if (convertNull) {
        ConvertUtils.deregister();
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
        ConvertUtils.register(new BooleanConverter(null), Boolean.class);
        ConvertUtils.register(new ByteConverter(null), Byte.class);
        ConvertUtils.register(new CharacterConverter(null), Character.class);
        ConvertUtils.register(new DoubleConverter(null), Double.class);
        ConvertUtils.register(new FloatConverter(null), Float.class);
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);
        ConvertUtils.register(new ShortConverter(null), Short.class);
    }
}

From source file:org.kuali.rice.kns.web.struts.action.KualiActionServlet.java

/**
 * <p>Initialize other global characteristics of the controller servlet.</p>
 * Overridden to remove the ConvertUtils.deregister() command that caused problems
 * with the concurrent data dictionary load.  (KULRNE-4405)
 *
 * @exception ServletException if we cannot initialize these resources
 *//*from   w  w  w  .java2s .co m*/
@Override
protected void initOther() throws ServletException {

    String value = null;
    value = getServletConfig().getInitParameter("config");
    if (value != null) {
        config = value;
    }

    // Backwards compatibility for form beans of Java wrapper classes
    // Set to true for strict Struts 1.0 compatibility
    value = getServletConfig().getInitParameter("convertNull");
    if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)
            || "y".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) {

        convertNull = true;
    }

    if (convertNull) {
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
        ConvertUtils.register(new BooleanConverter(null), Boolean.class);
        ConvertUtils.register(new ByteConverter(null), Byte.class);
        ConvertUtils.register(new CharacterConverter(null), Character.class);
        ConvertUtils.register(new DoubleConverter(null), Double.class);
        ConvertUtils.register(new FloatConverter(null), Float.class);
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);
        ConvertUtils.register(new ShortConverter(null), Short.class);
    }

    // KULRICE-8176: KFS Notes/Attachments Tab Functionality for Note Text Error - Visible/Special characters, spaces, or tabs
    parameterEncoding = getServletConfig().getInitParameter("PARAMETER_ENCODING");
}

From source file:org.quickbundle.third.struts.action.RmActionServlet.java

/**
  * <p>Initialize other global characteristics of the controller
  * servlet.</p>/* w  ww . java  2 s .com*/
  *
  * @throws ServletException if we cannot initialize these resources
  */
protected void initOther() throws ServletException {
    String value;

    value = getServletConfig().getInitParameter("config");

    if (value != null) {
        //QB-RM add *.xml
        if (value.trim().length() > 0 && value.indexOf("*.xml") > -1) { //
            String finalValue = "";
            String[] aValue = value.trim().split(",");
            for (int j = 0; j < aValue.length; j++) {
                String path = aValue[j];
                if (path.trim().length() == 0) {
                    continue;
                }
                //??*.xml
                if (path.trim().endsWith("*.xml")) {
                    File fWarDirStr = RmPathHelper.getWarDir();
                    File fPath = new File(fWarDirStr + File.separator
                            + (path.substring(0, path.length() - "*.xml".length())));
                    for (int i = 0; i < fPath.listFiles().length; i++) {
                        File fPathXml = fPath.listFiles()[i];
                        if (fPathXml.isFile() && fPathXml.toString().toLowerCase().endsWith(".xml")) {
                            String newPath = fPathXml.getAbsolutePath()
                                    .substring((int) fWarDirStr.getAbsoluteFile().toString().length())
                                    .replaceAll("\\\\", "/");
                            finalValue += newPath + ",";
                        }
                    }
                } else {
                    finalValue += path + ",";
                }
            }
            if (finalValue.endsWith(",")) {
                finalValue = finalValue.substring(0, finalValue.length() - ",".length());
            }
            config = finalValue;
        } else {
            config = value;
        }
    }

    // Backwards compatibility for form beans of Java wrapper classes
    // Set to true for strict Struts 1.0 compatibility
    value = getServletConfig().getInitParameter("convertNull");

    if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)
            || "y".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) {
        convertNull = true;
    }

    if (convertNull) {
        ConvertUtils.deregister();
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
        ConvertUtils.register(new BooleanConverter(null), Boolean.class);
        ConvertUtils.register(new ByteConverter(null), Byte.class);
        ConvertUtils.register(new CharacterConverter(null), Character.class);
        ConvertUtils.register(new DoubleConverter(null), Double.class);
        ConvertUtils.register(new FloatConverter(null), Float.class);
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);
        ConvertUtils.register(new ShortConverter(null), Short.class);
    }
}

From source file:org.sputnikdev.bluetooth.gattparser.FieldHolder.java

/**
 * Returns a BigInteger representation of the field or a default value in case if the field cannot
 * be converted to a BigInteger./*from   ww w  . j  a  v  a  2 s.  c om*/
 * @param def the default value to be returned if an error occurs converting the field
 * @return a BigInteger representation of the field
 */
public BigInteger getBigInteger(BigInteger def) {
    BigDecimal result = new BigDecimalConverter(null).convert(BigDecimal.class, prepareValue());
    return result != null
            ? result.multiply(BigDecimal.valueOf(getMultiplier())).add(BigDecimal.valueOf(getOffset()))
                    .setScale(0, RoundingMode.HALF_UP).toBigInteger()
            : def;
}

From source file:org.sputnikdev.bluetooth.gattparser.FieldHolder.java

/**
 * Returns a BigDecimal representation of the field or a default value in case if the field cannot
 * be converted to a BigDecimal.//from w  w w  .  j  a va 2  s . c o m
 * @param def the default value to be returned if an error occurs converting the field
 * @return a BigDecimal representation of the field
 */
public BigDecimal getBigDecimal(BigDecimal def) {
    BigDecimal result = new BigDecimalConverter(null).convert(BigDecimal.class, prepareValue());
    return result != null ? result.multiply(BigDecimal.valueOf(getMultiplier())) : def;
}

From source file:utils.beanUtils.JIMBeanUtilsBean.java

/** 
 * Gets the instance which provides the functionality for {@link BeanUtils}.
 * This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
 * This mechanism provides isolation for web apps deployed in the same container. 
 *//* w  w w  .  ja v a 2 s.  c o m*/
protected synchronized static JIMBeanUtilsBean getInstance(Locale locale) {
    Map<Locale, JIMBeanUtilsBean> temp = (Map<Locale, JIMBeanUtilsBean>) beansByClassLoader.get();

    if (temp.containsKey(locale)) {
        return temp.get(locale);
    } else {
        JIMBeanUtilsBean utilsBean = new JIMBeanUtilsBean();
        utilsBean.getConvertUtils().register(new DateConverter(locale), Date.class);
        utilsBean.getConvertUtils().register(new StringConverter(locale), String.class);
        utilsBean.getConvertUtils().register(new NumberConverter(locale), BigDecimal.class);
        utilsBean.getConvertUtils().register(new NumberConverter(locale), Integer.class);
        utilsBean.getConvertUtils().register(new NumberConverter(locale), int.class);
        utilsBean.getConvertUtils().register(new BooleanConverter(locale), Boolean.class);
        utilsBean.getConvertUtils().register(new BooleanConverter(locale), boolean.class);

        byte byteArray[] = new byte[0];
        utilsBean.getConvertUtils().register(new ByteArrayConverter(), byteArray.getClass());

        //this is set in order to enable null results
        BeanUtilsBean.getInstance().getConvertUtils().register(new BigDecimalConverter(null), BigDecimal.class);

        temp.put(locale, utilsBean);

        return utilsBean;
    }
}