Example usage for java.lang Integer TYPE

List of usage examples for java.lang Integer TYPE

Introduction

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

Prototype

Class TYPE

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

Click Source Link

Document

The Class instance representing the primitive type int .

Usage

From source file:org.hyperic.hq.product.jmx.MBeanUtil.java

private static void initConverters() {
    addConverter(Object.class, new Converter() {
        public Object convert(String param) {
            return param;
        }/*from www.  j  a v  a 2  s .co  m*/
    });

    addConverter(Short.class, new Converter() {
        public Object convert(String param) {
            return Short.valueOf(param);
        }
    });

    addConverter(Integer.class, new Converter() {
        public Object convert(String param) {
            return Integer.valueOf(param);
        }
    });

    addConverter(Long.class, new Converter() {
        public Object convert(String param) {
            return Long.valueOf(param);
        }
    });

    addConverter(Double.class, new Converter() {
        public Object convert(String param) {
            return Double.valueOf(param);
        }
    });

    addConverter(Boolean.class, new Converter() {
        public Object convert(String param) {
            return Boolean.valueOf(param);
        }
    });

    addConverter(File.class, new Converter() {
        public Object convert(String param) {
            return new File(param);
        }
    });

    addConverter(URL.class, new Converter() {
        public Object convert(String param) {
            try {
                return new URL(param);
            } catch (MalformedURLException e) {
                throw invalid(param, e);
            }
        }
    });

    addConverter(ObjectName.class, new Converter() {
        public Object convert(String param) {
            try {
                return new ObjectName(param);
            } catch (MalformedObjectNameException e) {
                throw invalid(param, e);
            }
        }
    });

    addConverter(List.class, new ListConverter() {
        public Object convert(String[] params) {
            return Arrays.asList(params);
        }
    });

    addConverter(String[].class, new ListConverter() {
        public Object convert(String[] params) {
            return params;
        }
    });

    addConverter(Long[].class, new ListConverter() {
        public Object convert(String[] params) {
            Long[] args = new Long[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Long.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Integer[].class, new ListConverter() {
        public Object convert(String[] params) {
            Integer[] args = new Integer[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Integer.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Double[].class, new ListConverter() {
        public Object convert(String[] params) {
            Double[] args = new Double[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Double.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Short[].class, new ListConverter() {
        public Object convert(String[] params) {
            Short[] args = new Short[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Short.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(Boolean[].class, new ListConverter() {
        public Object convert(String[] params) {
            Boolean[] args = new Boolean[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Boolean.valueOf(params[i]);
            }
            return args;
        }
    });

    addConverter(long[].class, new ListConverter() {
        public Object convert(String[] params) {
            long[] args = new long[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Long.parseLong(params[i]);
            }
            return args;
        }
    });

    addConverter(int[].class, new ListConverter() {
        public Object convert(String[] params) {
            int[] args = new int[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Integer.parseInt(params[i]);
            }
            return args;
        }
    });

    addConverter(double[].class, new ListConverter() {
        public Object convert(String[] params) {
            double[] args = new double[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Double.parseDouble(params[i]);
            }
            return args;
        }
    });

    addConverter(short[].class, new ListConverter() {
        public Object convert(String[] params) {
            short[] args = new short[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = Short.parseShort(params[i]);
            }
            return args;
        }
    });

    addConverter(boolean[].class, new ListConverter() {
        public Object convert(String[] params) {
            boolean[] args = new boolean[params.length];
            for (int i = 0; i < params.length; i++) {
                args[i] = params[i].equals("true") ? true : false;
            }
            return args;
        }
    });

    Class[][] aliases = { { String.class, Object.class }, { Short.TYPE, Short.class },
            { Integer.TYPE, Integer.class }, { Long.TYPE, Long.class }, { Double.TYPE, Double.class },
            { Boolean.TYPE, Boolean.class }, };

    for (int i = 0; i < aliases.length; i++) {
        addConverter(aliases[i][0], aliases[i][1]);
    }
}

From source file:org.openspaces.rest.utils.ControllerUtils.java

public static Object convertPropertyToPrimitiveType(String object, Class type, String propKey) {
    if (type.equals(Long.class) || type.equals(Long.TYPE))
        return Long.valueOf(object);

    if (type.equals(Boolean.class) || type.equals(Boolean.TYPE))
        return Boolean.valueOf(object);

    if (type.equals(Integer.class) || type.equals(Integer.TYPE))
        return Integer.valueOf(object);

    if (type.equals(Byte.class) || type.equals(Byte.TYPE))
        return Byte.valueOf(object);

    if (type.equals(Short.class) || type.equals(Short.TYPE))
        return Short.valueOf(object);

    if (type.equals(Float.class) || type.equals(Float.TYPE))
        return Float.valueOf(object);

    if (type.equals(Double.class) || type.equals(Double.TYPE))
        return Double.valueOf(object);

    if (type.isEnum())
        return Enum.valueOf(type, object);

    if (type.equals(String.class) || type.equals(Object.class))
        return String.valueOf(object);

    if (type.equals(java.util.Date.class)) {
        try {/*from  w  w  w  .ja  v a  2 s .c om*/
            return simpleDateFormat.parse(object);
        } catch (ParseException e) {
            throw new RestException(
                    "Unable to parse date [" + object + "]. Make sure it matches the format: " + date_format);
        }
    }

    //unknown type
    throw new UnsupportedTypeException("Non primitive type when converting property [" + propKey + "]:" + type);
}

From source file:com.google.flatbuffers.Table.java

@Override
public String toString() {
    HashMap<String, Object> v = new HashMap<String, Object>();
    try {//from w  w w. j a  v  a  2s .co m
        Method[] ms = this.getClass().getDeclaredMethods();
        for (Method m : ms) {
            String sMethodName = m.getName();
            if (m.getParameterTypes().length == 0 && !sMethodName.endsWith("AsByteBuffer")) {
                if (sMethodName.endsWith("Length")) {
                    int ii = sMethodName.lastIndexOf("Length");
                    String sMethodName1 = sMethodName.substring(0, ii);
                    List<Object> l = new ArrayList<>();
                    int iLength = 0;
                    try {
                        iLength = (int) m.invoke(this, new Object[] {});
                    } catch (Exception e) {
                    }
                    for (int i = 0; i < iLength; i++) {
                        Method m1 = this.getClass().getDeclaredMethod(sMethodName1,
                                new Class<?>[] { Integer.TYPE });
                        Object oKq = m1.invoke(this, new Object[] { i });
                        l.add(oKq);
                    }
                    v.put(sMethodName1, l);
                } else {
                    Object oKq = m.invoke(this, new Object[] {});
                    v.put(sMethodName, oKq);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return v.toString();
}

From source file:org.eiichiro.bootleg.AbstractRequest.java

/**
 * Returns the default value of the specified primitive type.
 * //from   w  w  w .  j  av  a 2  s . c  o m
 * @param type The primitive type.
 * @return The default value of the specified primitive type.
 */
protected Object primitive(Type type) {
    Class<?> rawType = Types.getRawType(type);

    if (rawType.equals(Boolean.TYPE)) {
        return (boolean) false;
    } else if (rawType.equals(Character.TYPE)) {
        return (char) 0;
    } else if (rawType.equals(Byte.TYPE)) {
        return (byte) 0;
    } else if (rawType.equals(Double.TYPE)) {
        return (double) 0.0;
    } else if (rawType.equals(Float.TYPE)) {
        return (float) 0.0;
    } else if (rawType.equals(Integer.TYPE)) {
        return (int) 0;
    } else {
        // short.
        return (short) 0;
    }
}

From source file:org.evosuite.testcase.statements.FunctionalMockStatementTest.java

@Test
public void testConfirmCast() {

    //note: TypeUtils can give different results because it takes autoboxing into account

    assertTrue(TypeUtils.isAssignable(Integer.class, Integer.TYPE));
    assertTrue(TypeUtils.isAssignable(Integer.TYPE, Integer.class));
    assertFalse(Integer.TYPE.isAssignableFrom(Integer.class));
    assertFalse(Integer.class.isAssignableFrom(Integer.TYPE));

    assertFalse(Integer.TYPE.isAssignableFrom(Character.TYPE));
    assertFalse(TypeUtils.isAssignable(Integer.TYPE, Character.TYPE));

    assertFalse(Character.TYPE.isAssignableFrom(Integer.TYPE));
    assertTrue(TypeUtils.isAssignable(Character.TYPE, Integer.TYPE)); //DIFFERENT

    assertFalse(Character.class.isAssignableFrom(Integer.TYPE));
    assertTrue(TypeUtils.isAssignable(Character.class, Integer.TYPE)); //DIFFERENT

    assertFalse(Character.class.isAssignableFrom(Integer.class));
    assertFalse(TypeUtils.isAssignable(Character.class, Integer.class));

    assertTrue(Integer.TYPE.isPrimitive());
    assertFalse(Integer.class.isPrimitive());

    char c = 'c'; //99
    int i = c;//from   ww  w .j a v  a2  s  .co  m

    assertEquals(99, i);

    Object aInt = i;
    Object aInteger = Integer.valueOf(7);

    Assert.assertTrue(aInt.getClass().equals(Integer.class));
    Assert.assertTrue(aInt.getClass().equals(aInteger.getClass()));

    Object aChar = c;
    Assert.assertTrue(aChar.getClass().equals(Character.class));

    //just recall the two diverge
    assertTrue(TypeUtils.isAssignable(aChar.getClass(), Integer.TYPE));
    assertFalse(Integer.TYPE.isAssignableFrom(aChar.getClass()));

    Object casted = null;
    try {
        casted = Integer.TYPE.cast(aChar);
        fail();
    } catch (Exception e) {
        //expected: cannot do direct cast from "Character" to "int"
    }

    try {
        casted = Integer.TYPE.cast(((Character) aChar).charValue());
        fail();
    } catch (Exception e) {
        //expected: "cast" takes an Object as input, so it does autoboxing :(
    }

    casted = (int) ((Character) aChar).charValue();

    assertTrue(casted.getClass().equals(Integer.class));
}

From source file:nl.strohalm.cyclos.utils.conversion.CoercionHelper.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object convert(Class toType, Object value) {
    if ("".equals(value)) {
        value = null;/*w  w w  . ja v  a  2  s  . c o m*/
    }
    // If we do not want a collection, but the value is one, use the first value
    if (value != null && !(Collection.class.isAssignableFrom(toType) || toType.isArray())
            && (value.getClass().isArray() || value instanceof Collection)) {
        final Iterator it = IteratorUtils.getIterator(value);
        if (!it.hasNext()) {
            value = null;
        } else {
            value = it.next();
        }
    }

    // Check for null values
    if (value == null) {
        if (toType.isPrimitive()) {
            // On primitives, use the default value
            if (toType == Boolean.TYPE) {
                value = Boolean.FALSE;
            } else if (toType == Character.TYPE) {
                value = '\0';
            } else {
                value = 0;
            }
        } else {
            // For objects, return null
            return null;
        }
    }

    // Check if the value is already of the expected type
    if (toType.isInstance(value)) {
        return value;
    }

    // If the class is primitive, use the wrapper, so we have an easier work of testing instances
    if (toType.isPrimitive()) {
        toType = ClassUtils.primitiveToWrapper(toType);
    }

    // Convert to well-known types
    if (String.class.isAssignableFrom(toType)) {
        if (value instanceof Entity) {
            final Long entityId = ((Entity) value).getId();
            return entityId == null ? null : entityId.toString();
        }
        return value.toString();
    } else if (Number.class.isAssignableFrom(toType)) {
        if (!(value instanceof Number)) {
            if (value instanceof String) {
                value = new BigDecimal((String) value);
            } else if (value instanceof Date) {
                value = ((Date) value).getTime();
            } else if (value instanceof Calendar) {
                value = ((Calendar) value).getTimeInMillis();
            } else if (value instanceof Entity) {
                value = ((Entity) value).getId();
                if (value == null) {
                    return null;
                }
            } else {
                throw new ConversionException("Invalid number: " + value);
            }
        }
        final Number number = (Number) value;
        if (Byte.class.isAssignableFrom(toType)) {
            return number.byteValue();
        } else if (Short.class.isAssignableFrom(toType)) {
            return number.shortValue();
        } else if (Integer.class.isAssignableFrom(toType)) {
            return number.intValue();
        } else if (Long.class.isAssignableFrom(toType)) {
            return number.longValue();
        } else if (Float.class.isAssignableFrom(toType)) {
            return number.floatValue();
        } else if (Double.class.isAssignableFrom(toType)) {
            return number.doubleValue();
        } else if (BigInteger.class.isAssignableFrom(toType)) {
            return new BigInteger(number.toString());
        } else if (BigDecimal.class.isAssignableFrom(toType)) {
            return new BigDecimal(number.toString());
        }
    } else if (Boolean.class.isAssignableFrom(toType)) {
        if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        } else if ("on".equalsIgnoreCase(value.toString())) {
            return true;
        } else {
            return Boolean.parseBoolean(value.toString());
        }
    } else if (Character.class.isAssignableFrom(toType)) {
        final String str = value.toString();
        return (str.length() == 0) ? null : str.charAt(0);
    } else if (Calendar.class.isAssignableFrom(toType)) {
        if (value instanceof Date) {
            final Calendar cal = new GregorianCalendar();
            cal.setTime((Date) value);
            return cal;
        }
    } else if (Date.class.isAssignableFrom(toType)) {
        if (value instanceof Calendar) {
            final long millis = ((Calendar) value).getTimeInMillis();
            try {
                return ConstructorUtils.invokeConstructor(toType, millis);
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }
    } else if (Enum.class.isAssignableFrom(toType)) {
        Object ret;
        try {
            ret = Enum.valueOf(toType, value.toString());
        } catch (final Exception e) {
            ret = null;
        }
        if (ret == null) {
            Object[] possible;
            try {
                possible = (Object[]) toType.getMethod("values").invoke(null);
            } catch (final Exception e) {
                throw new IllegalStateException(
                        "Couldn't invoke the 'values' method for enum " + toType.getName());
            }
            if (StringValuedEnum.class.isAssignableFrom(toType)) {
                final String test = coerce(String.class, value);
                for (final Object item : possible) {
                    if (((StringValuedEnum) item).getValue().equals(test)) {
                        ret = item;
                        break;
                    }
                }
            } else if (IntValuedEnum.class.isAssignableFrom(toType)) {
                final int test = coerce(Integer.TYPE, value);
                for (final Object item : possible) {
                    if (((IntValuedEnum) item).getValue() == test) {
                        ret = item;
                        break;
                    }
                }
            } else {
                throw new ConversionException("Invalid enum: " + value);
            }
        }
        return ret;
    } else if (Entity.class.isAssignableFrom(toType)) {
        final Long id = coerce(Long.class, value);
        return EntityHelper.reference(toType, id);
    } else if (Locale.class.isAssignableFrom(toType)) {
        return LocaleConverter.instance().valueOf(value.toString());
    } else if (Collection.class.isAssignableFrom(toType)) {
        final Collection collection = (Collection) ClassHelper.instantiate(toType);
        final Iterator iterator = IteratorUtils.getIterator(value);
        while (iterator.hasNext()) {
            collection.add(iterator.next());
        }
        return collection;
    } else if (toType.isArray()) {
        final Collection collection = coerceCollection(toType.getComponentType(), value);
        final Object[] array = (Object[]) Array.newInstance(toType.getComponentType(), collection.size());
        return collection.toArray(array);
    }

    // We don't know how to convert the value
    throw new ConversionException("Cannot coerce value to: " + toType.getName());
}

From source file:com.collaborne.jsonschema.generator.pojo.PojoGenerator.java

@Inject
@VisibleForTesting//w  ww  . j a v  a  2s  .c o  m
protected PojoGenerator(PojoClassGenerator classGenerator, PojoArrayGenerator arrayGenerator,
        PojoStringGenerator stringGenerator) {
    this.typeGenerators.put("object", classGenerator);
    this.typeGenerators.put("array", arrayGenerator);
    if (getFeature(FEATURE_USE_SIMPLE_PLAIN_TYPES)) {
        // TODO: if additional restrictions are given on these types we can either implement specific
        //       types (for example we provide a base library for each of the plain types, and configure them
        //       to check the restrictions), or we could simply ignore those.
        this.typeGenerators.put("string", new SimplePojoTypeGenerator(ClassName.create(String.class)));
    } else {
        this.typeGenerators.put("string", stringGenerator);
    }
    this.typeGenerators.put("integer", new SimplePojoTypeGenerator(ClassName.create(Integer.TYPE)));
    this.typeGenerators.put("number", new SimplePojoTypeGenerator(ClassName.create(Double.TYPE)));
    this.typeGenerators.put("boolean", new SimplePojoTypeGenerator(ClassName.create(Boolean.TYPE)));
}

From source file:org.bytesoft.bytejta.supports.springcloud.SpringCloudCoordinator.java

private String serialize(Serializable arg) throws IOException {
    if (Xid.class.isInstance(arg)) {
        Xid xid = (Xid) arg;
        byte[] globalTransactionId = xid.getGlobalTransactionId();
        return ByteUtils.byteArrayToString(globalTransactionId);
    } else if (Integer.class.isInstance(arg) || Integer.TYPE.isInstance(arg)) {
        return String.valueOf(arg);
    } else if (Boolean.class.isInstance(arg) || Boolean.TYPE.isInstance(arg)) {
        return String.valueOf(arg);
    } else {//  www .j a  v  a2  s. c om
        byte[] byteArray = CommonUtils.serializeObject(arg);
        return ByteUtils.byteArrayToString(byteArray);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFServiceTest.java

private Object buildRowIndexedLiteral(String language) {
    try {//from   ww w  .  ja  v a2  s. c  om
        Class<?> clazz = Class.forName(RIL_CLASSNAME);
        Class<?>[] argTypes = { LanguageFilteringRDFService.class, Literal.class, Integer.TYPE };
        Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes);
        constructor.setAccessible(true);

        Literal l = new LiteralStub(language);
        int i = literalIndex++;
        return constructor.newInstance(filteringRDFService, l, i);
    } catch (Exception e) {
        throw new RuntimeException("Could not create a row-indexed literal", e);
    }
}

From source file:com.nfwork.dbfound.json.JSONDynaBean.java

private boolean isInteger(Class clazz) {
    return Integer.class.isAssignableFrom(clazz) || clazz == Integer.TYPE;
}