Example usage for java.lang Boolean TYPE

List of usage examples for java.lang Boolean TYPE

Introduction

In this page you can find the example usage for java.lang Boolean TYPE.

Prototype

Class TYPE

To view the source code for java.lang Boolean TYPE.

Click Source Link

Document

The Class object representing the primitive type boolean.

Usage

From source file:Util.java

/**
 * Is the given {@code object} a primitive type or wrapper for a primitive
 * type?/*from   w  w  w . j a  v a  2s  .co m*/
 * 
 * @param object
 *            The object to check for primitive-ness.
 * @return {@code true} if {@code object} is a primitive type or wrapper for
 *         a primitive type, {@code false} otherwise.
 */
public static boolean isPrimitive(Object object) {
    if (object == null)
        return false;

    Class<?> type = object.getClass();

    return object instanceof String || (object instanceof Integer || Integer.TYPE.equals(type))
            || (object instanceof Boolean || Boolean.TYPE.equals(type))
            || (object instanceof Long || Long.TYPE.equals(type))
            || (object instanceof Double || Double.TYPE.equals(type))
            || (object instanceof Float || Float.TYPE.equals(type))
            || (object instanceof Byte || Byte.TYPE.equals(type))
            || (object instanceof Short || Short.TYPE.equals(type))
            || (object instanceof Character || Character.TYPE.equals(type));
}

From source file:Main.java

public static String getPrimitiveLetter(Class<?> paramClass) {
    if (Integer.TYPE.equals(paramClass)) {
        return "I";
    }//from w  ww .j  av  a  2s  .  co  m
    if (Void.TYPE.equals(paramClass)) {
        return "V";
    }
    if (Boolean.TYPE.equals(paramClass)) {
        return "Z";
    }
    if (Character.TYPE.equals(paramClass)) {
        return "C";
    }
    if (Byte.TYPE.equals(paramClass)) {
        return "B";
    }
    if (Short.TYPE.equals(paramClass)) {
        return "S";
    }
    if (Float.TYPE.equals(paramClass)) {
        return "F";
    }
    if (Long.TYPE.equals(paramClass)) {
        return "J";
    }
    if (Double.TYPE.equals(paramClass)) {
        return "D";
    }
    throw new IllegalStateException("Type: " + paramClass.getCanonicalName() + " is not a primitive type");
}

From source file:io.amira.zen.layout.ZenViewProxy.java

private void _fill_classes() {
    class_convert = new HashMap<Class, Class>();
    class_convert.put(Integer.class, Integer.TYPE);
    class_convert.put(Float.class, Float.TYPE);
    class_convert.put(Double.class, Double.TYPE);
    class_convert.put(Boolean.class, Boolean.TYPE);
}

From source file:org.jtwig.util.ArrayUtil.java

public static Object[] toArray(Object obj) {
    if (!obj.getClass().isArray()) {
        throw new IllegalArgumentException("Argument is not an array");
    }/*from w ww  .ja va2s  . co  m*/

    Class<?> type = obj.getClass().getComponentType();
    if (!type.isPrimitive()) {
        return (Object[]) obj;
    }

    if (Byte.TYPE.isAssignableFrom(type)) {
        return toArray((byte[]) obj);
    }
    if (Short.TYPE.isAssignableFrom(type)) {
        return toArray((short[]) obj);
    }
    if (Integer.TYPE.isAssignableFrom(obj.getClass().getComponentType())) {
        return toArray((int[]) obj);
    }
    if (Long.TYPE.isAssignableFrom(obj.getClass().getComponentType())) {
        return toArray((long[]) obj);
    }
    if (Boolean.TYPE.isAssignableFrom(obj.getClass().getComponentType())) {
        return toArray((boolean[]) obj);
    }
    if (Character.TYPE.isAssignableFrom(obj.getClass().getComponentType())) {
        return toArray((char[]) obj);
    }
    if (Float.TYPE.isAssignableFrom(obj.getClass().getComponentType())) {
        return toArray((float[]) obj);
    }
    if (Double.TYPE.isAssignableFrom(obj.getClass().getComponentType())) {
        return toArray((double[]) obj);
    }

    throw new IllegalArgumentException("Unsupported argument type: " + type.getName());
}

From source file:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object array, Class type, int index, JSONArray jsonArray)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        if (type == Integer.TYPE) {
            Array.setInt(array, index, jsonArray.getInt(index));
        } else if (type == Long.TYPE) {
            Array.setLong(array, index, jsonArray.getInt(index));
        } else if (type == Short.TYPE) {
            Array.setShort(array, index, (short) jsonArray.getInt(index));
        } else if (type == Boolean.TYPE) {
            Array.setBoolean(array, index, jsonArray.getBoolean(index));
        } else if (type == Double.TYPE) {
            Array.setDouble(array, index, jsonArray.getDouble(index));
        } else if (type == Float.TYPE) {
            Array.setFloat(array, index, (float) jsonArray.getDouble(index));
        } else if (type == Character.TYPE) {
            char ch = jsonArray.getString(index).charAt(0);
            Array.setChar(array, index, ch);
        } else if (type == Byte.TYPE) {
            Array.setByte(array, index, (byte) jsonArray.getInt(index));
        } else {/*from w  w  w  .  j av a  2s .  c o  m*/
            throw new JSONSerializationException("Unknown primitive: " + type);
        }
    } else if (type == String.class) {
        Array.set(array, index, jsonArray.getString(index));
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONObject jObj = jsonArray.getJSONObject(index);
        JSONSerializable serObj = deSerialize(type, jObj);
        Array.set(array, index, serObj);
    } else if (type.isArray()) {
        Class componentClass = type.getComponentType();
        JSONArray subArray = jsonArray.getJSONArray(index);
        int len = subArray.length();

        Object newArray = Array.newInstance(componentClass, len);
        for (int k = 0; k < len; ++k) {
            loadObject(newArray, componentClass, k, subArray);
        }
    }
}

From source file:cc.aileron.accessor.TypeConvertorImpl.java

@Override
public Object convert(final Object value, final Class<?> from, final Class<?> to) {
    /*//from  w w  w  .j  a  va 2s. c o  m
     * to boolean
     */
    if (Boolean.class.isAssignableFrom(to) || to == Boolean.TYPE) {
        if (value == null) {
            return false;
        }
        if (Boolean.class.isAssignableFrom(from) || Boolean.TYPE == from) {
            return value;
        }
        if (Number.class.isAssignableFrom(from) || from.isPrimitive()) {
            return ((Number) value).intValue() != 0;
        }
        if (BooleanGetAccessor.class.isAssignableFrom(from)) {
            final BooleanGetAccessor accessor = (BooleanGetAccessor) value;
            return accessor.toBoolean();
        }
        if (String.class == from) {
            return Boolean.parseBoolean(String.class.cast(value));
        }
        return true;
    }

    /*
     * to primitive is null
     */
    if (to.isPrimitive() && value == null) {
        return 0;
    }

    /*
     * value is null
     */
    if (value == null) {
        return null;
    }

    /*
     * to primitive from string
     */
    if (to.isPrimitive() && String.class == from) {
        if (StringUtils.isEmpty((String) value)) {
            return 0;
        }
        final Integer number = Integer.valueOf((String) value);
        return numberToPrimitive(number, to);
    }

    /*
     * to primitive from number
     */
    if (to.isPrimitive() && Number.class.isAssignableFrom(from)) {
        final Number number = (Number) value;
        return numberToPrimitive(number, to);
    }

    /*
     * to number from string
     */
    if (Number.class.isAssignableFrom(to) && String.class == from) {
        final String string = (String) value;
        if (Boolean.TYPE == to) {
            return Boolean.parseBoolean(string);
        }
        if (Character.TYPE == to) {
            throw new UnsupportedOperationException();
        }
        final Number number = Integer.valueOf(string);
        return numberToPrimitive(number, to);
    }

    /*
     * to number from number-get-accessor
     */
    if (Number.class.isAssignableFrom(to) && value instanceof NumberGetAccessor) {
        final NumberGetAccessor accessor = (NumberGetAccessor) value;
        return accessor.toNumber();
    }

    /*
     * to string from not string
     */
    if (to == String.class && !(value instanceof String)) {
        return value.toString();
    }

    return value;
}

From source file:org.primeframework.mvc.parameter.convert.converters.BooleanConverterTest.java

@Test
public void fromStrings() {
    GlobalConverter converter = new BooleanConverter(new MockConfiguration());
    Boolean b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr",
            ArrayUtils.toArray((String) null));
    assertNull(b);//from   w w  w  .  ja  v  a 2s .c  o  m

    b = (Boolean) converter.convertFromStrings(Boolean.TYPE, null, "testExpr",
            ArrayUtils.toArray((String) null));
    assertFalse(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("true"));
    assertTrue(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("yes"));
    assertTrue(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("on"));
    assertTrue(b);

    b = (Boolean) converter.convertFromStrings(Boolean.TYPE, null, "testExpr", ArrayUtils.toArray("true"));
    assertTrue(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("false"));
    assertFalse(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("no"));
    assertFalse(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("off"));
    assertFalse(b);

    b = (Boolean) converter.convertFromStrings(Boolean.TYPE, null, "testExpr", ArrayUtils.toArray("false"));
    assertFalse(b);

    b = (Boolean) converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("   "));
    assertNull(b);

    b = (Boolean) converter.convertFromStrings(Boolean.TYPE, null, "testExpr", ArrayUtils.toArray("   "));
    assertFalse(b);

    Boolean[] ba = (Boolean[]) converter.convertFromStrings(Boolean[].class, null, "testExpr",
            ArrayUtils.toArray("true", "false"));
    assertTrue(ba[0]);
    assertFalse(ba[1]);

    boolean[] bpa = (boolean[]) converter.convertFromStrings(boolean[].class, null, "testExpr",
            ArrayUtils.toArray("true", "false"));
    assertTrue(bpa[0]);
    assertFalse(bpa[1]);

    try {
        converter.convertFromStrings(Boolean.class, null, "testExpr", ArrayUtils.toArray("fals3"));
        fail("Should have failed");
    } catch (ConversionException ce) {
        // Expected
    }
}

From source file:Main.java

private static boolean isAssignableFrom(Class<?> parameterType, Object value) {
    if (parameterType.isPrimitive()) {
        if (value == null) {
            return false;
        }/*w w  w . j a v a2 s . c o m*/
        Class<?> valueClass = value.getClass();

        if (parameterType == Boolean.TYPE) {
            return valueClass == Boolean.class;
        } else if (parameterType == Byte.TYPE) {
            return valueClass == Byte.class;
        } else if (parameterType == Character.TYPE) {
            return valueClass == Character.class;
        } else if (parameterType == Short.TYPE) {
            return valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Integer.TYPE) {
            return valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                    || valueClass == Byte.class;
        } else if (parameterType == Long.TYPE) {
            return valueClass == Long.class || valueClass == Integer.class || valueClass == Character.class
                    || valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Float.TYPE) {
            return valueClass == Float.class || valueClass == Long.class || valueClass == Integer.class
                    || valueClass == Character.class || valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Double.TYPE) {
            return valueClass == Double.class || valueClass == Float.class || valueClass == Long.class
                    || valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                    || valueClass == Byte.class;
        } else {
            return false;
        }
    } else {
        return value == null || parameterType.isAssignableFrom(value.getClass());
    }
}

From source file:org.openamf.util.OpenAMFUtils.java

public static boolean typesMatch(Class parameterType, Object parameter) {
    log.debug("expected class: " + parameterType.getName());
    if (parameter == null) {
        log.debug("parameter is null");
    } else {// www . ja v  a 2s . c  o m
        log.debug("parameter class: " + parameter.getClass().getName());
    }
    boolean typesMatch = parameterType.isInstance(parameter);
    if (!typesMatch) {
        if (parameterType.equals(Boolean.TYPE) && parameter instanceof Boolean) {
            typesMatch = true;
        } else if (parameterType.equals(Character.TYPE) && parameter instanceof Character) {
            typesMatch = true;
        } else if (parameterType.equals(Byte.TYPE) && parameter instanceof Byte) {
            typesMatch = true;
        } else if (parameterType.equals(Short.TYPE) && parameter instanceof Short) {
            typesMatch = true;
        } else if (parameterType.equals(Integer.TYPE) && parameter instanceof Integer) {
            typesMatch = true;
        } else if (parameterType.equals(Long.TYPE) && parameter instanceof Long) {
            typesMatch = true;
        } else if (parameterType.equals(Float.TYPE) && parameter instanceof Float) {
            typesMatch = true;
        } else if (parameterType.equals(Double.TYPE) && parameter instanceof Double) {
            typesMatch = true;
        }
    }
    return typesMatch;
}

From source file:org.pentaho.platform.util.versionchecker.PentahoVersionCheckReflectHelper.java

public static List performVersionCheck(final boolean ignoreExistingUpdates, final int versionRequestFlags) {
    // check to see if jar is loaded before continuing
    if (PentahoVersionCheckReflectHelper.isVersionCheckerAvailable()) {
        try {//from  ww w  .j  a  va  2 s .c  o  m

            // use reflection so anyone can delete the version checker jar without pain

            // PentahoVersionCheckHelper helper = new PentahoVersionCheckHelper();
            Class helperClass = Class
                    .forName("org.pentaho.platform.util.versionchecker.PentahoVersionCheckHelper"); //$NON-NLS-1$
            Object helper = helperClass.getConstructors()[0].newInstance(new Object[] {});

            // helper.setIgnoreExistingUpdates(ignoreExistingUpdates);
            Method setIgnoreExistingUpdatesMethod = helperClass.getDeclaredMethod("setIgnoreExistingUpdates", //$NON-NLS-1$
                    new Class[] { Boolean.TYPE });
            setIgnoreExistingUpdatesMethod.invoke(helper, new Object[] { new Boolean(ignoreExistingUpdates) });

            // helper.setVersionRequestFlags(versionRequestFlags);
            Method setVersionRequestFlagsMethod = helperClass.getDeclaredMethod("setVersionRequestFlags", //$NON-NLS-1$
                    new Class[] { Integer.TYPE });
            setVersionRequestFlagsMethod.invoke(helper, new Object[] { new Integer(versionRequestFlags) });

            // helper.performUpdate();
            Method performUpdateMethod = helperClass.getDeclaredMethod("performUpdate", new Class[] {}); //$NON-NLS-1$
            performUpdateMethod.invoke(helper, new Object[] {});

            // List results = helper.getResults();
            Method getResultsMethod = helperClass.getDeclaredMethod("getResults", new Class[] {}); //$NON-NLS-1$
            List results = (List) getResultsMethod.invoke(helper, new Object[] {});
            return results;

        } catch (Exception e) {
            // ignore errors
        }
    }

    return null;
}