Java Number Convert convertStringToObjectWrapper(String className, String value)

Here you can find the source of convertStringToObjectWrapper(String className, String value)

Description

Converts the string value to the specified wrapper class type

License

Open Source License

Parameter

Parameter Description
className The wrapper type to convert the string to
value The string value to convert

Declaration

private static Object convertStringToObjectWrapper(String className, String value) 

Method Source Code

//package com.java2s;
/*//from  ww  w.  j  av  a 2  s .co  m
Copyright (c) 2009 Mindaugas Greibus (spantus@gmail.com)
Part of program for analyze speech signal 
http://spantus.sourceforge.net
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it 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 for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

import java.math.BigDecimal;

public class Main {
    /**
     * Converts the string value to the specified wrapper class type
     * 
     * @param className
     *            The wrapper type to convert the string to
     * @param value
     *            The string value to convert
     * @return
     */
    private static Object convertStringToObjectWrapper(String className, String value) {
        // Build a parameter class to pass as a parameter to
        Object o = null;
        if (className.equals("java.lang.Integer") || className.equals("int")) {
            o = new Integer(value);
        } else if (className.equals("java.lang.Long") || className.equals("long")) {
            o = Long.valueOf(value);
        } else if (className.equals("java.lang.Short") || className.equals("short")) {
            o = Short.valueOf(value);
        } else if (className.equals("java.lang.Byte") || className.equals("byte")) {
            o = Byte.valueOf(value);
        } else if (className.equals("java.lang.Float") || className.equals("float")) {
            o = Float.valueOf(value);
        } else if (className.equals("java.lang.Double") || className.equals("double")) {
            o = Double.valueOf(value);
        } else if (className.equals(BigDecimal.class.getName())) {
            o = BigDecimal.valueOf(Double.valueOf(value));
        } else if (className.equals("java.lang.String")) {
            o = value;
        } else if (className.equals("java.lang.Boolean") || className.equals("boolean")) {
            o = Boolean.valueOf(value);
        }

        // Return the object we built
        return o;
    }
}

Related

  1. convertScientificToStandard(double value)
  2. convertSize(Long size)
  3. convertSizeG1DetailsToSizeG1(final String size, final char units)
  4. convertSizeToMB(long sizeInBytes)
  5. convertsToLong(double v)
  6. convertToDecimal(Number n)
  7. convertToHours(long workingDuration)
  8. convertToInchesOfMercury(double altimeterSetting, String unit)
  9. convertToJBPM(Object value)