Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Checks whether objects of the given class can be converted to the specified
     * type.
     * 
     * @see #convert(Object, Class)
     */
    public static boolean canConvert(final Class<?> c, final Class<?> type) {
        // ensure type is well-behaved, rather than a primitive type
        final Class<?> saneType = getNonprimitiveType(type);

        // OK if the existing object can be casted
        if (canCast(c, saneType))
            return true;

        // OK if string
        if (saneType == String.class)
            return true;

        // OK if source type is string and destination type is character
        // (in this case, the first character of the string would be used)
        if (String.class.isAssignableFrom(c) && saneType == Character.class) {
            return true;
        }

        // OK if appropriate wrapper constructor exists
        try {
            saneType.getConstructor(c);
            return true;
        } catch (final Exception exc) {
            // no known way to convert
            return false;
        }
    }

    /**
     * Checks whether the given object can be converted to the specified type.
     * 
     * @see #convert(Object, Class)
     */
    public static boolean canConvert(final Object value, final Class<?> type) {
        if (value == null)
            return true;
        return canConvert(value.getClass(), type);
    }

    /**
     * Returns the non-primitive {@link Class} closest to the given type.
     * <p>
     * Specifically, the following type conversions are done:
     * <ul>
     * <li>boolean.class becomes Boolean.class</li>
     * <li>byte.class becomes Byte.class</li>
     * <li>char.class becomes Character.class</li>
     * <li>double.class becomes Double.class</li>
     * <li>float.class becomes Float.class</li>
     * <li>int.class becomes Integer.class</li>
     * <li>long.class becomes Long.class</li>
     * <li>short.class becomes Short.class</li>
     * <li>void.class becomes Void.class</li>
     * </ul>
     * All other types are unchanged.
     * </p>
     */
    public static <T> Class<T> getNonprimitiveType(final Class<T> type) {
        final Class<?> destType;
        if (type == boolean.class)
            destType = Boolean.class;
        else if (type == byte.class)
            destType = Byte.class;
        else if (type == char.class)
            destType = Character.class;
        else if (type == double.class)
            destType = Double.class;
        else if (type == float.class)
            destType = Float.class;
        else if (type == int.class)
            destType = Integer.class;
        else if (type == long.class)
            destType = Long.class;
        else if (type == short.class)
            destType = Short.class;
        else if (type == void.class)
            destType = Void.class;
        else
            destType = type;
        @SuppressWarnings("unchecked")
        final Class<T> result = (Class<T>) destType;
        return result;
    }

    /**
     * Checks whether objects of the given class can be cast to the specified
     * type.
     * 
     * @see #cast(Object, Class)
     */
    public static boolean canCast(final Class<?> c, final Class<?> type) {
        return type.isAssignableFrom(c);
    }

    /**
     * Checks whether the given object can be cast to the specified type.
     * 
     * @see #cast(Object, Class)
     */
    public static boolean canCast(final Object obj, final Class<?> type) {
        return canCast(obj.getClass(), type);
    }
}