Example usage for java.lang Character TYPE

List of usage examples for java.lang Character TYPE

Introduction

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

Prototype

Class TYPE

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

Click Source Link

Document

The Class instance representing the primitive type char .

Usage

From source file:com.alibaba.doris.admin.service.impl.ValueParseUtil.java

/**
 * ?,?,//from   w  ww . ja  v  a 2s . co m
 * 
 * @param clazz
 * @return
 */
@SuppressWarnings("unchecked")
private static <T> T getInternalDefaultValue(Class<T> clazz) {
    if (!clazz.isPrimitive()) {
        return null;
    }
    if (Short.TYPE.equals(clazz)) {
        return (T) Short.valueOf((short) 0);
    }
    if (Integer.TYPE.equals(clazz)) {
        return (T) Integer.valueOf(0);
    }
    if (Long.TYPE.equals(clazz)) {
        return (T) Long.valueOf(0);
    }
    if (Boolean.TYPE.equals(clazz)) {
        return (T) Boolean.valueOf(false);
    }
    if (Float.TYPE.equals(clazz)) {
        return (T) Float.valueOf(0);
    }
    if (Double.TYPE.equals(clazz)) {
        return (T) Double.valueOf(0);
    }
    if (Byte.TYPE.equals(clazz)) {
        return (T) Byte.valueOf((byte) 0);
    }
    if (Character.TYPE.equals(clazz)) {
        return (T) Character.valueOf('\0');
    }
    return null;
}

From source file:Util.java

/**
 * Is the given {@code object} a primitive type or wrapper for a primitive
 * type?/*from  w  ww . jav  a2s. 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:org.jtwig.util.ArrayUtil.java

public static Object[] toArray(Object obj) {
    if (!obj.getClass().isArray()) {
        throw new IllegalArgumentException("Argument is not an array");
    }//  w w w  . jav a  2s.  c o 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:io.github.benas.jpopulator.impl.DefaultRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @return a random value for the given type or null if the type is not supported
 *//*from   ww w  .j a v  a  2  s.  c om*/
public static Object getRandomValue(final Class type) {

    /*
     * String and Character types
     */
    if (type.equals(String.class)) {
        return RandomStringUtils.randomAlphabetic(ConstantsUtil.DEFAULT_STRING_LENGTH);
    }
    if (type.equals(Character.TYPE) || type.equals(Character.class)) {
        return RandomStringUtils.randomAlphabetic(1).charAt(0);
    }

    /*
     * Boolean type
     */
    if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
        return ConstantsUtil.RANDOM.nextBoolean();
    }

    /*
     * Numeric types
     */
    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) (ConstantsUtil.RANDOM.nextInt());
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) (ConstantsUtil.RANDOM.nextInt());
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return ConstantsUtil.RANDOM.nextInt();
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return ConstantsUtil.RANDOM.nextLong();
    }
    if (type.equals(Double.TYPE) || type.equals(Double.class)) {
        return ConstantsUtil.RANDOM.nextDouble();
    }
    if (type.equals(Float.TYPE) || type.equals(Float.class)) {
        return ConstantsUtil.RANDOM.nextFloat();
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(
                Math.abs(ConstantsUtil.RANDOM.nextInt(ConstantsUtil.DEFAULT_BIG_INTEGER_NUM_BITS_LENGTH)),
                ConstantsUtil.RANDOM);
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(ConstantsUtil.RANDOM.nextDouble());
    }
    if (type.equals(AtomicLong.class)) {
        return new AtomicLong(ConstantsUtil.RANDOM.nextLong());
    }
    if (type.equals(AtomicInteger.class)) {
        return new AtomicInteger(ConstantsUtil.RANDOM.nextInt());
    }

    /*
     * Date and time types
     */
    if (type.equals(java.util.Date.class)) {
        return ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue();
    }
    if (type.equals(java.sql.Date.class)) {
        return new java.sql.Date(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(java.sql.Time.class)) {
        return new java.sql.Time(ConstantsUtil.RANDOM.nextLong());
    }
    if (type.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(Calendar.class)) {
        return Calendar.getInstance();
    }
    if (type.equals(org.joda.time.DateTime.class)) {
        return new org.joda.time.DateTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalDate.class)) {
        return new org.joda.time.LocalDate(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalTime.class)) {
        return new org.joda.time.LocalTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalDateTime.class)) {
        return new org.joda.time.LocalDateTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.Duration.class)) {
        return new org.joda.time.Duration(Math.abs(ConstantsUtil.RANDOM.nextLong()));
    }
    if (type.equals(org.joda.time.Period.class)) {
        return new org.joda.time.Period(Math.abs(ConstantsUtil.RANDOM.nextInt()));
    }
    if (type.equals(org.joda.time.Interval.class)) {
        long startDate = Math.abs(ConstantsUtil.RANDOM.nextInt());
        long endDate = startDate + Math.abs(ConstantsUtil.RANDOM.nextInt());
        return new org.joda.time.Interval(startDate, endDate);
    }

    /*
     * Enum type
     */
    if (type.isEnum() && type.getEnumConstants().length > 0) {
        Object[] enumConstants = type.getEnumConstants();
        return enumConstants[ConstantsUtil.RANDOM.nextInt(enumConstants.length)];
    }

    /*
     * Return null for any unsupported type
     */
    return null;

}

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

@Test
public void fromStrings() {
    GlobalConverter converter = new CharacterConverter(new MockConfiguration());
    Character cw = (Character) converter.convertFromStrings(Character.class, null, "testExpr",
            ArrayUtils.toArray((String) null));
    assertNull(cw);//from   ww w  .j  a  va2 s  .com

    char c = (Character) converter.convertFromStrings(Character.TYPE, null, "testExpr",
            ArrayUtils.toArray((String) null));
    assertEquals(c, '\u0000');

    cw = (Character) converter.convertFromStrings(Character.class, null, "testExpr", ArrayUtils.toArray("c"));
    assertEquals((char) cw, 'c');

    c = (Character) converter.convertFromStrings(Character.TYPE, null, "testExpr", ArrayUtils.toArray("c"));
    assertEquals(c, 'c');

    cw = (Character) converter.convertFromStrings(Character.class, null, "testExpr", ArrayUtils.toArray(" "));
    assertNull(cw);

    c = (Character) converter.convertFromStrings(Character.TYPE, null, "testExpr", ArrayUtils.toArray(" "));
    assertEquals(c, 0);

    Character[] ca = (Character[]) converter.convertFromStrings(Character[].class, null, "testExpr",
            ArrayUtils.toArray("c", "d"));
    assertEquals(ca[0], (Character) 'c');
    assertEquals(ca[1], (Character) 'd');

    char[] cpa = (char[]) converter.convertFromStrings(char[].class, null, "testExpr",
            ArrayUtils.toArray("c", "d"));
    assertEquals(cpa[0], 'c');
    assertEquals(cpa[1], 'd');

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

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

From source file:Main.java

/**
 * <p>Copies the given array and adds the given element at the end of the new array.</p>
 *
 * <p>The new array contains the same elements of the input
 * array plus the given element in the last position. The component type of 
 * the new array is the same as that of the input array.</p>
 *
 * <p>If the input array is <code>null</code>, a new one element array is returned
 *  whose component type is the same as the element.</p>
 * //from   w  w w. j a v  a  2  s .c o m
 * <pre>
 * ArrayUtils.add(null, '0')       = ['0']
 * ArrayUtils.add(['1'], '0')      = ['1', '0']
 * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
 * </pre>
 * 
 * @param array  the array to copy and add the element to, may be <code>null</code>
 * @param element  the object to add at the last index of the new array
 * @return A new array containing the existing elements plus the new element
 * @since 2.1
 */
public static char[] add(char[] array, char element) {
    char[] newArray = (char[]) copyArrayGrow1(array, Character.TYPE);
    newArray[newArray.length - 1] = element;
    return newArray;
}

From source file:org.fhcrc.cpl.toolbox.AbstractConvertHelper.java

protected void register() {
    _register(new NullSafeConverter(new BigDecimalConverter()), BigDecimal.class);
    _register(new NullSafeConverter(new BigIntegerConverter()), BigInteger.class);
    _register(new NullSafeConverter(new BooleanArrayConverter()), boolean[].class);
    _register(new ByteConverter(), Byte.TYPE);
    _register(new NullSafeConverter(new ByteConverter()), Byte.class);
    _register(new CharacterConverter(), Character.TYPE);
    _register(new NullSafeConverter(new CharacterConverter()), Character.class);
    _register(new NullSafeConverter(new CharacterArrayConverter()), char[].class);
    _register(new NullSafeConverter(new ClassConverter()), Class.class);
    _register(new NullSafeConverter(new DoubleArrayConverter()), double[].class);
    _register(new FloatArrayConverter(), float[].class);
    _register(new NullSafeConverter(new IntegerArrayConverter()), int[].class);
    _register(new LongConverter(), Long.TYPE);
    _register(new NullSafeConverter(new LongConverter()), Long.class);
    _register(new NullSafeConverter(new LongArrayConverter()), long[].class);
    _register(new ShortConverter(), Short.TYPE);
    _register(new NullSafeConverter(new ShortConverter()), Short.class);
    _register(new NullSafeConverter(new ShortArrayConverter()), short[].class);
    _register(new NullSafeConverter(new StringArrayConverter()), String[].class);
    _register(new NullSafeConverter(new SqlDateConverter()), java.sql.Date.class);
    _register(new NullSafeConverter(new SqlTimeConverter()), java.sql.Time.class);
    _register(new NullSafeConverter(new SqlTimestampConverter()), java.sql.Time.class);
}

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 va 2 s. co 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:com.autonomy.aci.client.annotations.ConversionServiceImpl.java

public ConversionServiceImpl() {
    registerConverter(new StringToDoubleConverter(), String.class, Double.class);
    registerConverter(new StringToDoubleConverter(), String.class, Double.TYPE);

    registerConverter(new StringToByteConverter(), String.class, Byte.class);
    registerConverter(new StringToByteConverter(), String.class, Byte.TYPE);

    registerConverter(new StringToIntegerConverter(), String.class, Integer.class);
    registerConverter(new StringToIntegerConverter(), String.class, Integer.TYPE);

    registerConverter(new StringToFloatConverter(), String.class, Float.class);
    registerConverter(new StringToFloatConverter(), String.class, Float.TYPE);

    registerConverter(new StringToShortConverter(), String.class, Short.class);
    registerConverter(new StringToShortConverter(), String.class, Short.TYPE);

    registerConverter(new StringToCharacterConverter(), String.class, Character.class);
    registerConverter(new StringToCharacterConverter(), String.class, Character.TYPE);

    registerConverter(new StringToBooleanConverter(), String.class, Boolean.class);
    registerConverter(new StringToBooleanConverter(), String.class, Boolean.TYPE);

    registerConverter(new StringToLongConverter(), String.class, Long.class);
    registerConverter(new StringToLongConverter(), String.class, Long.TYPE);
}

From source file:Main.java

private static boolean isAssignableFrom(Class<?> parameterType, Object value) {
    if (parameterType.isPrimitive()) {
        if (value == null) {
            return false;
        }/*  www.  j a  v a 2 s.  co  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());
    }
}