Java Type Coerce coerce(Object val, Class c)

Here you can find the source of coerce(Object val, Class c)

Description

Tries to coerce a value to a specific (simple) data type.

License

Open Source License

Parameter

Parameter Description
val value to be coerced (converted)
c destination type class

Return

coerced value

Declaration

public static Object coerce(Object val, Class<?> c) 

Method Source Code

//package com.java2s;
/**// w  w w .j ava2s .  c  om
 * Copyright 2012-2014 Rafal Lewczuk <rafal.lewczuk@jitlogic.com>
 *
 * ZORKA 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.
 *
 * ZORKA 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
 * ZORKA. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Tries to coerce a value to a specific (simple) data type.
     *
     * @param val value to be coerced (converted)
     * @param c   destination type class
     * @return coerced value
     */
    public static Object coerce(Object val, Class<?> c) {

        if (val == null || c == null) {
            return null;
        } else if (val.getClass() == c) {
            return val;
        } else if (c == String.class) {
            return castString(val);
        } else if (c == Boolean.class) {
            return coerceBool(val);
        } else if (c == Long.class) {
            return castLong(val);
        } else if (c == Integer.class) {
            return castInteger(val);
        } else if (c == Double.class) {
            return castDouble(val);
        } else if (c == Short.class) {
            return castShort(val);
        } else if (c == Float.class) {
            return castFloat(val);
        }

        return null;
    }

    public static String castString(Object val) {
        try {
            return val != null ? val.toString() : "null";
        } catch (Exception e) {
            return "<ERR: " + e.getMessage() + ">";
        }
    }

    /**
     * Coerces any value to boolean.
     *
     * @param val value to be coerced
     * @return false if value is null or boolean false, true otherwise
     */
    public static boolean coerceBool(Object val) {
        return !(val == null || val.equals(false));
    }

    private static long castLong(Object val) {
        return (val instanceof String) ? Long.parseLong(val.toString()
                .trim()) : ((Number) val).longValue();
    }

    private static int castInteger(Object val) {
        return (val instanceof String) ? Integer.parseInt(val.toString()
                .trim()) : ((Number) val).intValue();
    }

    private static double castDouble(Object val) {
        return (val instanceof String) ? Double.parseDouble(val.toString()
                .trim()) : ((Number) val).doubleValue();
    }

    private static short castShort(Object val) {
        return (val instanceof String) ? Short.parseShort(val.toString()
                .trim()) : ((Number) val).shortValue();
    }

    private static float castFloat(Object val) {
        return (val instanceof String) ? Float.parseFloat(val.toString()
                .trim()) : ((Number) val).floatValue();
    }
}

Related

  1. coerce(final Class klass)
  2. coerce(Object obj, Class c)
  3. coerce(Object val, Class clz)
  4. coerce(Object value, Class clazz)
  5. coerce(Object value, Class clazz)
  6. coerceBool(Object obj)
  7. coerceBool(Object val)