List of usage examples for java.lang Float TYPE
Class TYPE
To view the source code for java.lang Float TYPE.
Click Source Link
From source file:org.robobinding.util.ClassUtils.java
/** * <p>//from ww w . j a v a 2s . com * Checks if one {@code Class} can be assigned to a variable of another * {@code Class}. * </p> * * <p> * Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this * method takes into account widenings of primitive classes and {@code null} * s. * </p> * * <p> * Primitive widenings allow an int to be assigned to a long, float or * double. This method returns the correct result for these cases. * </p> * * <p> * {@code Null} may be assigned to any reference type. This method will * return {@code true} if {@code null} is passed in and the toClass is * non-primitive. * </p> * * <p> * Specifically, this method tests whether the type represented by the * specified {@code Class} parameter can be converted to the type * represented by this {@code Class} object via an identity conversion * widening primitive or widening reference conversion. See * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em> * , sections 5.1.1, 5.1.2 and 5.1.4 for details. * </p> * * @param cls * the Class to check, may be null * @param toClass * the Class to try to assign into, returns false if null * @param autoboxing * whether to use implicit autoboxing/unboxing between primitives * and wrappers * @return {@code true} if assignment possible */ public static boolean isAssignable(Class<?> cls, final Class<?> toClass, final boolean autoboxing) { if (toClass == null) { return false; } // have to check for null, as isAssignableFrom doesn't if (cls == null) { return !toClass.isPrimitive(); } // autoboxing: if (autoboxing) { if (cls.isPrimitive() && !toClass.isPrimitive()) { cls = primitiveToWrapper(cls); if (cls == null) { return false; } } if (toClass.isPrimitive() && !cls.isPrimitive()) { cls = wrapperToPrimitive(cls); if (cls == null) { return false; } } } if (cls.equals(toClass)) { return true; } if (cls.isPrimitive()) { if (toClass.isPrimitive() == false) { return false; } if (Integer.TYPE.equals(cls)) { return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Long.TYPE.equals(cls)) { return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Boolean.TYPE.equals(cls)) { return false; } if (Double.TYPE.equals(cls)) { return false; } if (Float.TYPE.equals(cls)) { return Double.TYPE.equals(toClass); } if (Character.TYPE.equals(cls)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Short.TYPE.equals(cls)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Byte.TYPE.equals(cls)) { return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } // should never get here return false; } return toClass.isAssignableFrom(cls); }
From source file:org.jdto.util.ClassUtils.java
/** * <p>Checks if one/*from w w w .j av a 2 s .c om*/ * <code>Class</code> can be assigned to a variable of another * <code>Class</code>.</p> * * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, * this method takes into account widenings of primitive classes and * <code>null</code>s.</p> * * <p>Primitive widenings allow an int to be assigned to a long, float or * double. This method returns the correct result for these cases.</p> * * <p><code>Null</code> may be assigned to any reference type. This method * will return * <code>true</code> if * <code>null</code> is passed in and the toClass is non-primitive.</p> * * <p>Specifically, this method tests whether the type represented by the * specified * <code>Class</code> parameter can be converted to the type represented by * this * <code>Class</code> object via an identity conversion widening primitive * or widening reference conversion. See <em><a * href="http://java.sun.com/docs/books/jls/">The Java Language * Specification</a></em>, sections 5.1.1, 5.1.2 and 5.1.4 for details.</p> * * @param cls the Class to check, may be null * @param toClass the Class to try to assign into, returns false if null * @param autoboxing whether to use implicit autoboxing/unboxing between * primitives and wrappers * @return * <code>true</code> if assignment possible * @since 2.5 */ public static boolean isAssignable(Class cls, Class toClass, boolean autoboxing) { if (toClass == null) { return false; } // have to check for null, as isAssignableFrom doesn't if (cls == null) { return !(toClass.isPrimitive()); } //autoboxing: if (autoboxing) { if (cls.isPrimitive() && !toClass.isPrimitive()) { cls = primitiveToWrapper(cls); if (cls == null) { return false; } } if (toClass.isPrimitive() && !cls.isPrimitive()) { cls = wrapperToPrimitive(cls); if (cls == null) { return false; } } } if (cls.equals(toClass)) { return true; } if (cls.isPrimitive()) { if (toClass.isPrimitive() == false) { return false; } if (Integer.TYPE.equals(cls)) { return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Long.TYPE.equals(cls)) { return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Boolean.TYPE.equals(cls)) { return false; } if (Double.TYPE.equals(cls)) { return false; } if (Float.TYPE.equals(cls)) { return Double.TYPE.equals(toClass); } if (Character.TYPE.equals(cls)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Short.TYPE.equals(cls)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Byte.TYPE.equals(cls)) { return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } // should never get here return false; } return toClass.isAssignableFrom(cls); }
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;//w ww. j av a 2s .c om 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.apache.struts.action.TestDynaActionForm.java
/** * Positive getDynaProperty on property <code>floatProperty</code>. *///from ww w .j ava 2 s . c o m public void testGetDescriptorFloat() { testGetDescriptorBase("floatProperty", Float.TYPE); }
From source file:org.romaframework.core.schema.SchemaHelper.java
public static Object assignDefaultValueToLiteral(SchemaClass type) { Object value;//from w w w . j a v a2 s . c o m if (type.isOfType(Integer.TYPE)) { value = Integer.valueOf(0); } else if (type.isOfType(Long.TYPE)) { value = Long.valueOf(0); } else if (type.isOfType(Short.TYPE)) { value = Short.valueOf((short) 0); } else if (type.isOfType(Byte.TYPE)) { value = Byte.valueOf((byte) 0); } else if (type.isOfType(Float.TYPE)) { value = Float.valueOf(0); } else if (type.isOfType(Double.TYPE)) { value = Double.valueOf(0); } else { value = null; } return value; }
From source file:org.kordamp.ezmorph.bean.MorphDynaBean.java
protected boolean isDynaAssignable(Class dest, Class src) { boolean assignable = dest.isAssignableFrom(src); if (assignable) { return true; }// w ww . j ava 2s. c om assignable = (dest == Boolean.TYPE && src == Boolean.class) || assignable; assignable = (dest == Byte.TYPE && src == Byte.class) || assignable; assignable = (dest == Character.TYPE && src == Character.class) || assignable; assignable = (dest == Short.TYPE && src == Short.class) || assignable; assignable = (dest == Integer.TYPE && src == Integer.class) || assignable; assignable = (dest == Long.TYPE && src == Long.class) || assignable; assignable = (dest == Float.TYPE && src == Float.class) || assignable; assignable = (dest == Double.TYPE && src == Double.class) || assignable; if (src == Double.TYPE || Double.class.isAssignableFrom(src)) { assignable = (isByte(dest) || isShort(dest) || isInteger(dest) || isLong(dest) || isFloat(dest)) || assignable; } if (src == Float.TYPE || Float.class.isAssignableFrom(src)) { assignable = (isByte(dest) || isShort(dest) || isInteger(dest) || isLong(dest)) || assignable; } if (src == Long.TYPE || Long.class.isAssignableFrom(src)) { assignable = (isByte(dest) || isShort(dest) || isInteger(dest)) || assignable; } if (src == Integer.TYPE || Integer.class.isAssignableFrom(src)) assignable = (isByte(dest) || isShort(dest)) || assignable; if (src == Short.TYPE || Short.class.isAssignableFrom(src)) { assignable = (isByte(dest)) || assignable; } return assignable; }
From source file:org.apache.struts.action.DynaActionForm.java
/** * <p>Return the value of a simple property with the specified name.</p> * * @param name Name of the property whose value is to be retrieved * @return The value of a simple property with the specified name. * @throws IllegalArgumentException if there is no property of the * specified name * @throws NullPointerException if the type specified for the property * is invalid *//*from w w w .ja v a 2s . co m*/ public Object get(String name) { // Return any non-null value for the specified property Object value = dynaValues.get(name); if (value != null) { return (value); } // Return a null value for a non-primitive property Class type = getDynaProperty(name).getType(); if (type == null) { throw new NullPointerException("The type for property " + name + " is invalid"); } if (!type.isPrimitive()) { return (value); } // Manufacture default values for primitive properties if (type == Boolean.TYPE) { return (Boolean.FALSE); } else if (type == Byte.TYPE) { return (new Byte((byte) 0)); } else if (type == Character.TYPE) { return (new Character((char) 0)); } else if (type == Double.TYPE) { return (new Double(0.0)); } else if (type == Float.TYPE) { return (new Float((float) 0.0)); } else if (type == Integer.TYPE) { return (new Integer(0)); } else if (type == Long.TYPE) { return (new Long(0)); } else if (type == Short.TYPE) { return (new Short((short) 0)); } else { return (null); } }
From source file:nz.co.senanque.validationengine.ValidationUtils.java
public static Comparable<?> convertTo(final Class<Comparable<?>> clazz, Object obj) { if (obj == null) { return null; }//from w w w .ja va 2s.c o m if (clazz.isAssignableFrom(obj.getClass())) { return (Comparable<?>) obj; } final String oStr = String.valueOf(obj); if (clazz.equals(String.class)) { return oStr; } if (clazz.equals(Long.class) || clazz.equals(Long.TYPE)) { if (Number.class.isAssignableFrom(obj.getClass())) { return new Long(((Number) obj).longValue()); } return Long.valueOf(oStr); } if (clazz.equals(Integer.class) || clazz.equals(Integer.TYPE)) { if (Number.class.isAssignableFrom(obj.getClass())) { return new Integer(((Number) obj).intValue()); } return Integer.valueOf(oStr); } if (clazz.equals(Double.class) || clazz.equals(Double.TYPE)) { if (Number.class.isAssignableFrom(obj.getClass())) { return new Double(((Number) obj).doubleValue()); } return Double.valueOf(oStr); } if (clazz.equals(Float.class) || clazz.equals(Float.TYPE)) { if (Number.class.isAssignableFrom(obj.getClass())) { return new Float(((Number) obj).floatValue()); } return Float.valueOf(oStr); } if (clazz.equals(BigDecimal.class)) { return new BigDecimal(oStr); } if (clazz.equals(java.util.Date.class)) { return java.sql.Date.valueOf(oStr); } if (clazz.equals(java.sql.Date.class)) { return java.sql.Date.valueOf(oStr); } return null; }
From source file:org.apache.mina.statemachine.transition.MethodTransition.java
@SuppressWarnings("unchecked") private boolean match(Class<?> paramType, Object arg, Class argType) { if (paramType.isPrimitive()) { if (paramType.equals(Boolean.TYPE)) { return arg instanceof Boolean; }/*from w w w . j av a 2 s. c om*/ if (paramType.equals(Integer.TYPE)) { return arg instanceof Integer; } if (paramType.equals(Long.TYPE)) { return arg instanceof Long; } if (paramType.equals(Short.TYPE)) { return arg instanceof Short; } if (paramType.equals(Byte.TYPE)) { return arg instanceof Byte; } if (paramType.equals(Double.TYPE)) { return arg instanceof Double; } if (paramType.equals(Float.TYPE)) { return arg instanceof Float; } if (paramType.equals(Character.TYPE)) { return arg instanceof Character; } } return argType.isAssignableFrom(paramType) && paramType.isAssignableFrom(arg.getClass()); }
From source file:org.pentaho.ui.xul.impl.AbstractXulDomContainer.java
private Class unBoxPrimative(Class clazz) { if (clazz == Boolean.class) { return Boolean.TYPE; } else if (clazz == Integer.class) { return Integer.TYPE; } else if (clazz == Float.class) { return Float.TYPE; } else if (clazz == Double.class) { return Double.TYPE; } else if (clazz == Short.class) { return Short.TYPE; } else if (clazz == Long.class) { return Long.TYPE; } else {//from w w w. j a v a2s.c o m return clazz; } }