Default type conversion. Converts among numeric types and also strings. Contains the basic
type mapping code from OGNL.
| Method from com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter Detail: |
public static BigDecimal bigDecValue(Object value) throws NumberFormatException {
if (value == null)
return BigDecimal.valueOf(0L);
Class c = value.getClass();
if (c == BigDecimal.class)
return (BigDecimal) value;
if (c == BigInteger.class)
return new BigDecimal((BigInteger) value);
if (c.getSuperclass() == Number.class)
return new BigDecimal(((Number) value).doubleValue());
if (c == Boolean.class)
return BigDecimal.valueOf(((Boolean) value).booleanValue() ? 1 : 0);
if (c == Character.class)
return BigDecimal.valueOf(((Character) value).charValue());
return new BigDecimal(stringValue(value, true));
}
Evaluates the given object as a BigDecimal. |
public static BigInteger bigIntValue(Object value) throws NumberFormatException {
if (value == null)
return BigInteger.valueOf(0L);
Class c = value.getClass();
if (c == BigInteger.class)
return (BigInteger) value;
if (c == BigDecimal.class)
return ((BigDecimal) value).toBigInteger();
if (c.getSuperclass() == Number.class)
return BigInteger.valueOf(((Number) value).longValue());
if (c == Boolean.class)
return BigInteger.valueOf(((Boolean) value).booleanValue() ? 1 : 0);
if (c == Character.class)
return BigInteger.valueOf(((Character) value).charValue());
return new BigInteger(stringValue(value, true));
}
Evaluates the given object as a BigInteger. |
public static boolean booleanValue(Object value) {
if (value == null)
return false;
Class c = value.getClass();
if (c == Boolean.class)
return ((Boolean) value).booleanValue();
// if ( c == String.class )
// return ((String)value).length() > 0;
if (c == Character.class)
return ((Character) value).charValue() != 0;
if (value instanceof Number)
return ((Number) value).doubleValue() != 0;
return true; // non-null
}
Evaluates the given object as a boolean: if it is a Boolean object, it's
easy; if it's a Number or a Character, returns true for non-zero objects;
and otherwise returns true for non-null objects. |
public Object convertValue(Object value,
Class toType) {
Object result = null;
if (value != null) {
/* If array - > array then convert components of array individually */
if (value.getClass().isArray() && toType.isArray()) {
Class componentType = toType.getComponentType();
result = Array.newInstance(componentType, Array
.getLength(value));
for (int i = 0, icount = Array.getLength(value); i < icount; i++) {
Array.set(result, i, convertValue(Array.get(value, i),
componentType));
}
} else {
if ((toType == Integer.class) || (toType == Integer.TYPE))
result = new Integer((int) longValue(value));
if ((toType == Double.class) || (toType == Double.TYPE))
result = new Double(doubleValue(value));
if ((toType == Boolean.class) || (toType == Boolean.TYPE))
result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE;
if ((toType == Byte.class) || (toType == Byte.TYPE))
result = new Byte((byte) longValue(value));
if ((toType == Character.class) || (toType == Character.TYPE))
result = new Character((char) longValue(value));
if ((toType == Short.class) || (toType == Short.TYPE))
result = new Short((short) longValue(value));
if ((toType == Long.class) || (toType == Long.TYPE))
result = new Long(longValue(value));
if ((toType == Float.class) || (toType == Float.TYPE))
result = new Float(doubleValue(value));
if (toType == BigInteger.class)
result = bigIntValue(value);
if (toType == BigDecimal.class)
result = bigDecValue(value);
if (toType == String.class)
result = stringValue(value);
if (Enum.class.isAssignableFrom(toType))
result = enumValue((Class< Enum >)toType, value);
}
} else {
if (toType.isPrimitive()) {
result = primitiveDefaults.get(toType);
}
}
return result;
}
Returns the value converted numerically to the given class type
This method also detects when arrays are being converted and converts the
components of one array to the type of the other. |
public Object convertValue(Map context,
Object value,
Class toType) {
return convertValue(value, toType);
}
|
public Object convertValue(Map context,
Object target,
Member member,
String propertyName,
Object value,
Class toType) {
return convertValue(context, value, toType);
}
|
public static double doubleValue(Object value) throws NumberFormatException {
if (value == null)
return 0.0;
Class c = value.getClass();
if (c.getSuperclass() == Number.class)
return ((Number) value).doubleValue();
if (c == Boolean.class)
return ((Boolean) value).booleanValue() ? 1 : 0;
if (c == Character.class)
return ((Character) value).charValue();
String s = stringValue(value, true);
return (s.length() == 0) ? 0.0 : Double.parseDouble(s);
/*
* For 1.1 parseDouble() is not available
*/
// return Double.valueOf( value.toString() ).doubleValue();
}
Evaluates the given object as a double-precision floating-point number. |
public Enum enumValue(Class toClass,
Object o) {
Enum< ? > result = null;
if (o == null) {
result = null;
} else if (o instanceof String[]) {
result = Enum.valueOf(toClass, ((String[]) o)[0]);
} else if (o instanceof String) {
result = Enum.valueOf(toClass, (String) o);
}
return result;
}
|
public TypeConverter getTypeConverter(Map context) {
Object obj = context.get(TypeConverter.TYPE_CONVERTER_CONTEXT_KEY);
if (obj instanceof TypeConverter) {
return (TypeConverter) obj;
// for backwards-compatibility
} else if (obj instanceof ognl.TypeConverter) {
return new XWorkTypeConverterWrapper((ognl.TypeConverter) obj);
}
return null;
}
|
public static long longValue(Object value) throws NumberFormatException {
if (value == null)
return 0L;
Class c = value.getClass();
if (c.getSuperclass() == Number.class)
return ((Number) value).longValue();
if (c == Boolean.class)
return ((Boolean) value).booleanValue() ? 1 : 0;
if (c == Character.class)
return ((Character) value).charValue();
return Long.parseLong(stringValue(value, true));
}
Evaluates the given object as a long integer. |
public static String stringValue(Object value) {
return stringValue(value, false);
}
Evaluates the given object as a String. |
public static String stringValue(Object value,
boolean trim) {
String result;
if (value == null) {
result = NULL_STRING;
} else {
result = value.toString();
if (trim) {
result = result.trim();
}
}
return result;
}
Evaluates the given object as a String and trims it if the trim flag is
true. |