List of usage examples for java.lang Number byteValue
public byte byteValue()
From source file:org.objectstyle.cayenne.dataview.DataTypeSpec.java
public Object fromDataType(Class untypedValueClass, DataTypeEnum dataType, Object typedValue) { if (typedValue == null) return null; Class dataTypeClass = getJavaClass(dataType); // Validate.isTrue(typedValue.getClass().equals(dataTypeClass)); if (untypedValueClass == null) return typedValue; if (ClassUtils.isAssignable(dataTypeClass, untypedValueClass)) return typedValue; String strTypedValue = null;//from ww w .jav a 2s.co m boolean isStringTypedValue; Number numTypedValue = null; boolean isNumberTypedValue; Boolean boolTypedValue = null; boolean isBooleanTypedValue; Date dateTypedValue = null; boolean isDateTypedValue; if (isStringTypedValue = typedValue instanceof String) strTypedValue = (String) typedValue; if (isNumberTypedValue = typedValue instanceof Number) numTypedValue = (Number) typedValue; if (isBooleanTypedValue = typedValue instanceof Boolean) boolTypedValue = (Boolean) typedValue; if (isDateTypedValue = typedValue instanceof Date) dateTypedValue = (Date) typedValue; Object v = null; if (String.class.equals(untypedValueClass)) { v = ObjectUtils.toString(typedValue); } else if (BigDecimal.class.equals(untypedValueClass)) { if (isStringTypedValue) v = NumberUtils.createBigDecimal(strTypedValue); else if (isNumberTypedValue) v = new BigDecimal(numTypedValue.doubleValue()); else if (isBooleanTypedValue) v = new BigDecimal(BooleanUtils.toInteger(boolTypedValue.booleanValue())); else if (isDateTypedValue) v = new BigDecimal(dateTypedValue.getTime()); } else if (Boolean.class.equals(untypedValueClass)) { if (isStringTypedValue) v = BooleanUtils.toBooleanObject(strTypedValue); else if (isNumberTypedValue) v = BooleanUtils.toBooleanObject(numTypedValue.intValue()); else if (isDateTypedValue) v = BooleanUtils.toBooleanObject((int) dateTypedValue.getTime()); } else if (Byte.class.equals(untypedValueClass)) { if (isStringTypedValue) v = Byte.valueOf(strTypedValue); else if (isNumberTypedValue) v = new Byte(numTypedValue.byteValue()); else if (isBooleanTypedValue) v = new Byte((byte) BooleanUtils.toInteger(boolTypedValue.booleanValue())); else if (isDateTypedValue) v = new Byte((byte) dateTypedValue.getTime()); } else if (byte[].class.equals(untypedValueClass)) { if (isStringTypedValue) v = strTypedValue.getBytes(); } else if (Double.class.equals(untypedValueClass)) { if (isStringTypedValue) v = NumberUtils.createDouble(strTypedValue); else if (isNumberTypedValue) v = new Double(numTypedValue.doubleValue()); else if (isBooleanTypedValue) v = new Double(BooleanUtils.toInteger(boolTypedValue.booleanValue())); else if (isDateTypedValue) v = new Double(dateTypedValue.getTime()); } else if (Float.class.equals(untypedValueClass)) { if (isStringTypedValue) v = NumberUtils.createFloat(strTypedValue); else if (isNumberTypedValue) v = new Float(numTypedValue.floatValue()); else if (isBooleanTypedValue) v = new Float(BooleanUtils.toInteger(boolTypedValue.booleanValue())); else if (isDateTypedValue) v = new Float(dateTypedValue.getTime()); } else if (Integer.class.equals(untypedValueClass)) { if (isStringTypedValue) v = NumberUtils.createInteger(strTypedValue); else if (isNumberTypedValue) v = new Integer(numTypedValue.intValue()); else if (isBooleanTypedValue) v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue()); else if (isDateTypedValue) v = new Integer((int) dateTypedValue.getTime()); } else if (Long.class.equals(untypedValueClass)) { if (isStringTypedValue) v = NumberUtils.createLong(strTypedValue); else if (isNumberTypedValue) v = new Long(numTypedValue.longValue()); else if (isBooleanTypedValue) v = new Long(BooleanUtils.toInteger(boolTypedValue.booleanValue())); else if (isDateTypedValue) v = new Long(dateTypedValue.getTime()); } else if (java.sql.Date.class.equals(untypedValueClass)) { if (isNumberTypedValue) v = new java.sql.Date(numTypedValue.longValue()); else if (isDateTypedValue) v = new java.sql.Date(dateTypedValue.getTime()); } else if (java.sql.Time.class.equals(untypedValueClass)) { if (isNumberTypedValue) v = new java.sql.Time(numTypedValue.longValue()); else if (isDateTypedValue) v = new java.sql.Time(dateTypedValue.getTime()); } else if (java.sql.Timestamp.class.equals(untypedValueClass)) { if (isNumberTypedValue) v = new java.sql.Timestamp(numTypedValue.longValue()); else if (isDateTypedValue) v = new java.sql.Timestamp(dateTypedValue.getTime()); } else if (Date.class.equals(untypedValueClass)) { if (isNumberTypedValue) v = new Date(numTypedValue.longValue()); } return v; }
From source file:org.structr.core.entity.AbstractNode.java
private Object convert(Object value, Class type) { Object convertedObject = null; if (type.equals(String.class)) { // strings can be returned immediately return value.toString(); } else if (value instanceof Number) { Number number = (Number) value; if (type.equals(Integer.class) || type.equals(Integer.TYPE)) { return number.intValue(); } else if (type.equals(Long.class) || type.equals(Long.TYPE)) { return number.longValue(); } else if (type.equals(Double.class) || type.equals(Double.TYPE)) { return number.doubleValue(); } else if (type.equals(Float.class) || type.equals(Float.TYPE)) { return number.floatValue(); } else if (type.equals(Short.class) || type.equals(Integer.TYPE)) { return number.shortValue(); } else if (type.equals(Byte.class) || type.equals(Byte.TYPE)) { return number.byteValue(); }//w w w . j a va 2s . c o m } else if (value instanceof List) { return value; } else if (value instanceof Map) { return value; } // fallback try { Method valueOf = type.getMethod("valueOf", String.class); if (valueOf != null) { convertedObject = valueOf.invoke(null, value.toString()); } else { logger.log(Level.WARNING, "Unable to find static valueOf method for type {0}", type); } } catch (Throwable t) { logger.log(Level.WARNING, "Unable to deserialize value {0} of type {1}, Class has no static valueOf method.", new Object[] { value, type }); } return convertedObject; }