Example usage for java.lang Long TYPE

List of usage examples for java.lang Long TYPE

Introduction

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

Prototype

Class TYPE

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

Click Source Link

Document

The Class instance representing the primitive type long .

Usage

From source file:org.apache.struts.config.FormPropertyConfig.java

/**
 * Return a Class corresponds to the value specified for the
 * <code>type</code> property, taking into account the trailing "[]" for
 * arrays (as well as the ability to specify primitive Java types).
 *//*from   w  w w.  j  a  va  2  s . c  om*/
public Class getTypeClass() {
    // Identify the base class (in case an array was specified)
    String baseType = getType();
    boolean indexed = false;

    if (baseType.endsWith("[]")) {
        baseType = baseType.substring(0, baseType.length() - 2);
        indexed = true;
    }

    // Construct an appropriate Class instance for the base class
    Class baseClass = null;

    if ("boolean".equals(baseType)) {
        baseClass = Boolean.TYPE;
    } else if ("byte".equals(baseType)) {
        baseClass = Byte.TYPE;
    } else if ("char".equals(baseType)) {
        baseClass = Character.TYPE;
    } else if ("double".equals(baseType)) {
        baseClass = Double.TYPE;
    } else if ("float".equals(baseType)) {
        baseClass = Float.TYPE;
    } else if ("int".equals(baseType)) {
        baseClass = Integer.TYPE;
    } else if ("long".equals(baseType)) {
        baseClass = Long.TYPE;
    } else if ("short".equals(baseType)) {
        baseClass = Short.TYPE;
    } else {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        if (classLoader == null) {
            classLoader = this.getClass().getClassLoader();
        }

        try {
            baseClass = classLoader.loadClass(baseType);
        } catch (ClassNotFoundException ex) {
            log.error("Class '" + baseType + "' not found for property '" + name + "'");
            baseClass = null;
        }
    }

    // Return the base class or an array appropriately
    if (indexed) {
        return (Array.newInstance(baseClass, 0).getClass());
    } else {
        return (baseClass);
    }
}

From source file:com.projity.util.ClassUtils.java

/**
 * Get the corresponding object class from a primitive class
 * @param clazz primitive class//ww w .jav a 2s.  com
 * @return Object class.
 * @throws ClassCastException if class is unknown primitive
 */
public static Class primitiveToObjectClass(Class clazz) {
    //      return MethodUtils.toNonPrimitiveClass(clazz);
    if (clazz == Boolean.TYPE)
        return Boolean.class;
    else if (clazz == Character.TYPE)
        return Character.class;
    else if (clazz == Byte.TYPE)
        return Byte.class;
    else if (clazz == Short.TYPE)
        return Short.class;
    else if (clazz == Integer.TYPE)
        return Integer.class;
    else if (clazz == Long.TYPE)
        return Long.class;
    else if (clazz == Float.TYPE)
        return Float.class;
    else if (clazz == Double.TYPE)
        return Double.class;
    throw new ClassCastException("Cannot convert class" + clazz + " to an object class");
}

From source file:org.romaframework.core.schema.SchemaField.java

protected Object convertValue(Object iFieldValue) {
    if (type == null || isArray())
        return iFieldValue;

    SchemaClass typeClass = getType().getSchemaClass();
    if (typeClass.equals(Roma.schema().getSchemaClass(iFieldValue)))
        return iFieldValue;

    String textValue = null;//from   ww  w .j a v a 2s.c o  m
    if (iFieldValue instanceof String) {
        textValue = (String) iFieldValue;
    } else if (iFieldValue != null) {
        textValue = iFieldValue.toString();
    }

    Object value = null;

    if (textValue != null) {
        // TRY A SOFT CONVERSION
        if (typeClass.isOfType(Integer.class) || typeClass.isOfType(Integer.TYPE)) {
            try {
                value = textValue.equals("") ? null : Integer.parseInt(textValue);
            } catch (Exception e) {
                value = textValue.equals("") ? null : Double.valueOf(textValue).intValue();
            }
        } else if (typeClass.isOfType(Long.class) || typeClass.isOfType(Long.TYPE)) {
            value = textValue.equals("") ? null : Long.parseLong(textValue);
        } else if (typeClass.isOfType(Short.class) || typeClass.isOfType(Short.TYPE)) {
            value = textValue.equals("") ? null : Short.parseShort(textValue);
        } else if (typeClass.isOfType(Byte.class) || typeClass.isOfType(Byte.TYPE)) {
            value = textValue.equals("") ? null : Byte.parseByte(textValue);
        } else if (typeClass.isOfType(Character.class) || typeClass.isOfType(Character.TYPE)) {
            if (textValue.length() > 0) {
                value = new Character(textValue.charAt(0));
            }
        } else if (typeClass.isOfType(Float.class) || typeClass.isOfType(Float.TYPE)) {
            value = textValue.equals("") ? null : Float.parseFloat(textValue);
        } else if (typeClass.isOfType(Double.class) || typeClass.isOfType(Double.TYPE)) {
            value = textValue.equals("") ? null : Double.parseDouble(textValue);
        } else if (typeClass.isOfType(BigDecimal.class)) {
            value = textValue.equals("") ? null : new BigDecimal(textValue);
        } else if (iFieldValue != null && !typeClass.isArray() && iFieldValue.getClass().isArray()) {
            // DESTINATION VALUE IS NOT AN ARRAY: ASSIGN THE FIRST ONE ELEMENT
            value = ((Object[]) iFieldValue)[0];
        } else {
            value = iFieldValue;
        }
    }

    if (value != null) {
        // TODO is this the right place to do this...?
        Class<?> valueClass = value.getClass();
        // SUCH A MONSTER!!! MOVE THIS LOGIC IN SchemaClass.isAssignableFrom...
        if (value instanceof VirtualObject
                && !(typeClass.getLanguageType() instanceof Class<?>
                        && ((Class<?>) typeClass.getLanguageType()).isAssignableFrom(VirtualObject.class))
                && ((VirtualObject) value).getSuperClassObject() != null) {
            if (ComposedEntity.class
                    .isAssignableFrom(((VirtualObject) value).getSuperClassObject().getClass())) {
                value = ((VirtualObject) value).getSuperClassObject();
                valueClass = value.getClass();
            }
        }

        if (value instanceof ComposedEntity<?> && !typeClass.isAssignableFrom(valueClass)) {
            value = ((ComposedEntity<?>) value).getEntity();
        }
    }

    if (value == null && typeClass.isPrimitive()) {
        log.warn("Cannot set the field value to null for primitive types! Field: " + getEntity() + "." + name
                + " of class " + getType().getName() + ". Setting value to 0.");
        // SET THE VALUE TO 0
        value = SchemaHelper.assignDefaultValueToLiteral(typeClass);
    }
    return value;
}

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  ww. j  a v a2 s .  c o  m
            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:org.wisdom.template.thymeleaf.OgnlOpsByReflectionTest.java

@Test
public void testNewInteger() throws Exception {
    Class[] classes = new Class[] { Integer.TYPE, Long.TYPE };
    assertThat(invoke("newInteger", classes, NumericTypes.INT, 42l)).isEqualTo(42);
    assertThat(invoke("newInteger", classes, NumericTypes.LONG, 42l)).isEqualTo(42l);
    assertThat(invoke("newInteger", classes, NumericTypes.BYTE, 8)).isEqualTo(new Byte("8"));
    assertThat(invoke("newInteger", classes, NumericTypes.SHORT, 42)).isEqualTo(new Short("42"));

    // Also works with float and double, but only if there are no decimal part
    assertThat(invoke("newInteger", classes, NumericTypes.FLOAT, 42)).isEqualTo(42f);
    assertThat(invoke("newInteger", classes, NumericTypes.DOUBLE, 42)).isEqualTo(42d);
}

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

/**
 * Test the conversion from Strings.// w w w . ja v a2  s . c  om
 */
@Test
public void toStrings() {
    GlobalConverter converter = new NumberConverter(new MockConfiguration());
    String str = converter.convertToString(Integer.class, null, "testExpr", null);
    assertNull(str);

    str = converter.convertToString(Byte.class, null, "testExpr", (byte) 42);
    assertEquals(str, "42");

    str = converter.convertToString(Byte.TYPE, null, "testExpr", (byte) 42);
    assertEquals(str, "42");

    str = converter.convertToString(Short.class, null, "testExpr", (short) 42);
    assertEquals(str, "42");

    str = converter.convertToString(Short.TYPE, null, "testExpr", (short) 42);
    assertEquals(str, "42");

    str = converter.convertToString(Integer.class, null, "testExpr", 42);
    assertEquals(str, "42");

    str = converter.convertToString(Integer.class, null, "testExpr", 42);
    assertEquals(str, "42");

    str = converter.convertToString(Long.class, null, "testExpr", 42l);
    assertEquals(str, "42");

    str = converter.convertToString(Long.TYPE, null, "testExpr", 42l);
    assertEquals(str, "42");

    str = converter.convertToString(Float.class, null, "testExpr", 42f);
    assertEquals(str, "42.0");

    str = converter.convertToString(Float.TYPE, null, "testExpr", 42f);
    assertEquals(str, "42.0");

    str = converter.convertToString(Double.class, null, "testExpr", 42.0);
    assertEquals(str, "42.0");

    str = converter.convertToString(Double.TYPE, null, "testExpr", 42.0);
    assertEquals(str, "42.0");
}

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  w ww.j  a va 2  s. c  om
    });

    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.briljantframework.data.resolver.Resolve.java

private static Resolver<LocalDate> initializeLocalDateResolver() {
    Resolver<LocalDate> resolver = new Resolver<>(LocalDate.class);
    Converter<Long, LocalDate> longToLocalDate = (l) -> Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault())
            .toLocalDate();/*  ww w  .j  ava 2s.co m*/

    resolver.put(String.class, StringToLocalDate.ISO_DATE);
    resolver.put(Long.class, longToLocalDate);
    resolver.put(Long.TYPE, longToLocalDate);
    return resolver;
}

From source file:org.apache.jcs.config.PropertySetter.java

/**
 * Convert <code>val</code> a String parameter to an object of a given
 * type.//from w ww  .  j a va  2 s  .c  o m
 * @param val
 * @param type
 * @return Object
 */
protected Object convertArg(String val, Class type) {
    if (val == null) {
        return null;
    }

    String v = val.trim();
    if (String.class.isAssignableFrom(type)) {
        return val;
    } else if (Integer.TYPE.isAssignableFrom(type)) {
        return new Integer(v);
    } else if (Long.TYPE.isAssignableFrom(type)) {
        return new Long(v);
    } else if (Boolean.TYPE.isAssignableFrom(type)) {
        if ("true".equalsIgnoreCase(v)) {
            return Boolean.TRUE;
        } else if ("false".equalsIgnoreCase(v)) {
            return Boolean.FALSE;
        }
    }
    return null;
}

From source file:RealFunctionValidation.java

public static Object readAndWritePrimitiveValue(final DataInputStream in, final DataOutputStream out,
        final Class<?> type) throws IOException {

    if (!type.isPrimitive()) {
        throw new IllegalArgumentException("type must be primitive");
    }/*from  w  w  w.ja  va 2  s  .c om*/
    if (type.equals(Boolean.TYPE)) {
        final boolean x = in.readBoolean();
        out.writeBoolean(x);
        return Boolean.valueOf(x);
    } else if (type.equals(Byte.TYPE)) {
        final byte x = in.readByte();
        out.writeByte(x);
        return Byte.valueOf(x);
    } else if (type.equals(Character.TYPE)) {
        final char x = in.readChar();
        out.writeChar(x);
        return Character.valueOf(x);
    } else if (type.equals(Double.TYPE)) {
        final double x = in.readDouble();
        out.writeDouble(x);
        return Double.valueOf(x);
    } else if (type.equals(Float.TYPE)) {
        final float x = in.readFloat();
        out.writeFloat(x);
        return Float.valueOf(x);
    } else if (type.equals(Integer.TYPE)) {
        final int x = in.readInt();
        out.writeInt(x);
        return Integer.valueOf(x);
    } else if (type.equals(Long.TYPE)) {
        final long x = in.readLong();
        out.writeLong(x);
        return Long.valueOf(x);
    } else if (type.equals(Short.TYPE)) {
        final short x = in.readShort();
        out.writeShort(x);
        return Short.valueOf(x);
    } else {
        // This should never occur.
        throw new IllegalStateException();
    }
}