Java String to convertStringToObject(String value, Class type)

Here you can find the source of convertStringToObject(String value, Class type)

Description

Not support negative numeric value: - http://en.wikipedia.org/wiki/Double-precision_floating-point_format

License

Apache License

Parameter

Parameter Description
value a parameter
type a parameter

Declaration

public static Object convertStringToObject(String value, Class<?> type) 

Method Source Code

//package com.java2s;
/*/*from   ww w.j a v  a  2s .c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Not support negative numeric value:
     * - http://en.wikipedia.org/wiki/Double-precision_floating-point_format
     *
     * @param value
     * @param type
     * @return
     */
    public static Object convertStringToObject(String value, Class<?> type) {
        Object obj = null;
        try {
            if (String.class.equals(type)) {
                obj = value;
            }
            if (Long.class.equals(type) || long.class.equals(type)) {
                obj = Long.parseLong(value);
                // if((Long) obj < 0) throw new IllegalArgumentException("Don't support negative Long yet: "+obj);
            } else if (Integer.class.equals(type) || int.class.equals(type)) {
                obj = Integer.parseInt(value);
                // if((Integer) obj < 0) throw new IllegalArgumentException("Don't support negative Integer yet: "+obj);
            } else if (Double.class.equals(type) || double.class.equals(type)) {
                obj = Double.parseDouble(value);
                // if((Double) obj < 0) throw new IllegalArgumentException("Don't support negative Double yet: "+obj);
            } else if (Float.class.equals(type) || float.class.equals(type)) {
                obj = Float.parseFloat(value);
                // if((Double) obj < 0) throw new IllegalArgumentException("Don't support negative Float yet: "+obj);
            }
            if (obj != null)
                return obj;
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException("Fail to convert string: " + value + " into type of " + type, ex);
        }

        throw new IllegalArgumentException(
                "Fail to convert string: " + value + " into type of " + type + ", illegal type: " + type);
    }
}

Related

  1. ConvertStringToEnumValue(String enumAsString, Class type)
  2. convertStringToFloat(String str)
  3. convertStringToId(String s)
  4. convertStringToIntDef(String string, int defaultValue)
  5. convertStringToLiteral(String className, String value)
  6. convertStringToPrimaryKey(String keyString)
  7. convertStringToPrimitive(Object object, Class toType)
  8. convertStringToPrimitive(String value, Class destinationClass)
  9. convertStringToSafeFilename(String s)