org.mmbase.util
Class Casting

java.lang.Object
  extended byorg.mmbase.util.Casting

public class Casting
extends Object


Nested Class Summary
static class Casting.ListWrapper
           
static class Casting.StringWrapper
          Wraps a String with an 'Escaper'.
 
Field Summary
static DateFormat ISO_8601_DATE
           
static DateFormat ISO_8601_LOOSE
          A Date formatter that creates a date based on a ISO 8601 date and a ISO 8601 time.
static DateFormat ISO_8601_TIME
           
static DateFormat ISO_8601_UTC
          A Date formatter that creates a ISO 8601 datetime according to UTC/GMT.
 
Constructor Summary
Casting()
           
 
Method Summary
static boolean isStringRepresentable(Class type)
          Whether or not Casting can more or less reliably cast a certain type to String and back.
static boolean isType(Class type, Object value)
          Returns whether the passed object is of the given class.
static boolean toBoolean(Object b)
          Convert an object to a boolean.
static byte[] toByte(Object obj)
          Convert an object to a byte array.
static Collection toCollection(Object o)
           
static Date toDate(Object d)
          Convert an object to a Date.
static double toDouble(Object i)
          Convert an object to an double.
static double toDouble(Object i, double def)
          Convert an object to an double.
static float toFloat(Object i)
          Convert an object to an float.
static float toFloat(Object i, float def)
          Convert an object to an float.
static InputStream toInputStream(Object obj)
           
static int toInt(Object i)
          Convert an object to an int.
static int toInt(Object i, int def)
          Convert an object to an int.
static Integer toInteger(Object i)
          Convert an object to an Integer.
static List toList(Object o)
          Convert an object to a List.
static List toList(Object o, String delimiter)
          As toList(Object) but with one extra argument.
static long toLong(Object i)
          Convert an object to a long.
static long toLong(Object i, long def)
          Convert an object to a long.
static Map toMap(Object o)
           
static Node toNode(Object i, Cloud cloud)
          Convert an object to an Node.
static String toString(Object o)
          Convert an object to a String.
static StringBuffer toStringBuffer(StringBuffer buffer, Object o)
          Convert an object to a string, using a StringBuffer.
static Object toType(Class type, Cloud cloud, Object value)
          Tries to 'cast' an object for use with the provided class.
static Object toType(Class type, Object value)
          Tries to 'cast' an object for use with the provided class.
static Writer toWriter(Writer writer, Object o)
          Convert an object to a string, using a Writer.
static Document toXML(Object o)
          Convert the value to a Document object.
static Object unWrap(Object o)
          When you want to undo the wrapping, this method can be used.
static Object wrap(Object o, CharTransformer escaper)
          Wraps it in an object with a toString as we desire.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ISO_8601_LOOSE

public static final DateFormat ISO_8601_LOOSE
A Date formatter that creates a date based on a ISO 8601 date and a ISO 8601 time. I.e. 2004-12-01 14:30:00. It is NOT 100% ISO 8601, as opposed to ISO_8601_UTC, as the standard actually requires a 'T' to be placed between the date and the time. The date given is the date for the local (server) time. Use this formatter if you want to display user-friendly dates in local time. XXX: According to http://en.wikipedia.org/wiki/ISO_8601, the standard allows ' ' in stead of 'T' if no misunderstanding arises, which is the case here. So I don't think this is 'loose'.


ISO_8601_UTC

public static final DateFormat ISO_8601_UTC
A Date formatter that creates a ISO 8601 datetime according to UTC/GMT. I.e. 2004-12-01T14:30:00Z. This is 100% ISO 8601, as opposed to ISO_8601_LOOSE. Use this formatter if you want to export dates. XXX: Hmm, we parse with UTC now, while we don't store them as such.


ISO_8601_DATE

public static final DateFormat ISO_8601_DATE

ISO_8601_TIME

public static final DateFormat ISO_8601_TIME
Constructor Detail

Casting

public Casting()
Method Detail

isType

public static boolean isType(Class type,
                             Object value)
Returns whether the passed object is of the given class. Unlike Class instanceof this also includes Object Types that are representative for primitive types (i.e. Integer for int).

Parameters:
type - the type (class) to check
value - the value whose type to check
Returns:
true if compatible
Since:
MMBase-1.8

toType

public static Object toType(Class type,
                            Object value)
Tries to 'cast' an object for use with the provided class. E.g. if value is a String, but the type passed is Integer, then the string is act to an Integer. If the type passed is a primitive type, the object is cast to an Object Types that is representative for that type (i.e. Integer for int).

Parameters:
type - the type (class)
value - The value to be converted
Returns:
value the converted value
Since:
MMBase-1.8

toType

public static Object toType(Class type,
                            Cloud cloud,
                            Object value)
Tries to 'cast' an object for use with the provided class. E.g. if value is a String, but the type passed is Integer, then the string is act to an Integer. If the type passed is a primitive type, the object is cast to an Object Types that is representative for that type (i.e. Integer for int).

Parameters:
type - the type (class)
cloud - When casting to Node, a cloud may be needed. May be null, for an anonymous cloud to be tried.
value - The value to be converted
Returns:
value the converted value
Since:
MMBase-1.8

isStringRepresentable

public static boolean isStringRepresentable(Class type)
Whether or not Casting can more or less reliably cast a certain type to String and back. For collection types also the entries of the collection must be string representable.

Since:
MMBase-1.8

toString

public static String toString(Object o)
Convert an object to a String. 'null' is converted to an empty string.

Parameters:
o - the object to convert
Returns:
the converted value as a String

toStringBuffer

public static StringBuffer toStringBuffer(StringBuffer buffer,
                                          Object o)
Convert an object to a string, using a StringBuffer.

Parameters:
buffer - The StringBuffer with which to create the string
o - the object to convert
Returns:
the StringBuffer used for conversion (same as the buffer parameter)
Since:
MMBase-1.7

toWriter

public static Writer toWriter(Writer writer,
                              Object o)
                       throws IOException
Convert an object to a string, using a Writer.

Parameters:
writer - The Writer with which to create (write) the string
o - the object to convert
Returns:
the Writer used for conversion (same as the writer parameter)
Throws:
IOException
Since:
MMBase-1.7

wrap

public static Object wrap(Object o,
                          CharTransformer escaper)
Wraps it in an object with a toString as we desire. Casting can now be done with toString() on the resulting object. This is used to make JSTL en EL behave similarly as mmbase taglib when writing objects to the page (taglib calls Casting, but they of course don't).

Parameters:
o - The object to be wrapped
escaper - null or a CharTransformer to pipe the strings through
Since:
MMBase-1.8
To Do:
Not everything is wrapped (and can be unwrapped) already.

unWrap

public static Object unWrap(Object o)
When you want to undo the wrapping, this method can be used.

Since:
MMBase-1.8

toList

public static List toList(Object o)
Convert an object to a List. A String is split up (as if it was a comma-separated String). Individual objects are wrapped and returned as Lists with one item. null and the empty string are returned as an empty list.

Parameters:
o - the object to convert
Returns:
the converted value as a List
Since:
MMBase-1.7

toList

public static List toList(Object o,
                          String delimiter)
As toList(Object) but with one extra argument.

Parameters:
delimiter - Regexp to use when splitting up the string if the object is a String. null or the empty string mean the default, which is a comma.
Since:
MMBase-1.8

toMap

public static Map toMap(Object o)
Since:
MMBase-1.8

toCollection

public static Collection toCollection(Object o)
Since:
MMBase-1.8

toXML

public static Document toXML(Object o)
Convert the value to a Document object. If the value is not itself a Document, the method attempts to attempts to convert the String value into an XML. A null value is returned as null. If the value cannot be converted, this method throws an IllegalArgumentException.

Parameters:
o - the object to be converted to an XML document
Returns:
the value as a DOM Element or null
Throws:
IllegalArgumentException - if the value could not be converted
Since:
MMBase-1.6

toByte

public static byte[] toByte(Object obj)
Convert an object to a byte array.

Parameters:
obj - The object to be converted
Returns:
the value as an byte[] (binary/blob field)

toInputStream

public static InputStream toInputStream(Object obj)

toNode

public static Node toNode(Object i,
                          Cloud cloud)
Convert an object to an Node. If the value is Numeric, the method tries to obtrain the object with that number. If it is a String, the method tries to obtain the object with that alias. All remaining situations return null.

Parameters:
i - the object to convert
cloud - the Cloud to use for loading a node
Returns:
the value as a Node
Since:
MMBase-1.7

toInt

public static int toInt(Object i,
                        int def)
Convert an object to an int. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. If a value is an Node, it's number field is returned. All remaining values return the provided default value.

Parameters:
i - the object to convert
def - the default value if conversion is impossible
Returns:
the converted value as an int
Since:
MMBase-1.7

toInt

public static int toInt(Object i)
Convert an object to an int. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. If a value is a Node, it's number field is returned. All remaining values return -1.

Parameters:
i - the object to convert
Returns:
the converted value as an int

toBoolean

public static boolean toBoolean(Object b)
Convert an object to a boolean. If the value is numeric, this call returns true if the value is a positive, non-zero, value. In other words, values '0' and '-1' are considered false. If the value is a string, this call returns true if the value is "true" or "yes" (case-insensitive). In all other cases (including calling byte fields), false is returned.

Parameters:
b - the object to convert
Returns:
the converted value as a boolean

toInteger

public static Integer toInteger(Object i)
Convert an object to an Integer. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return -1.

Parameters:
i - the object to convert
Returns:
the converted value as a Integer

toLong

public static long toLong(Object i,
                          long def)
Convert an object to a long. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return the provided default value.

Parameters:
i - the object to convert
def - the default value if conversion is impossible
Returns:
the converted value as a long
Since:
MMBase-1.7

toLong

public static long toLong(Object i)
Convert an object to a long. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return -1.

Parameters:
i - the object to convert
Returns:
the converted value as a long
Since:
MMBase-1.7

toFloat

public static float toFloat(Object i,
                            float def)
Convert an object to an float. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return the default value.

Parameters:
i - the object to convert
def - the default value if conversion is impossible
Returns:
the converted value as a float

toFloat

public static float toFloat(Object i)
Convert an object to an float. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return -1.

Parameters:
i - the object to convert
Returns:
the converted value as a float

toDouble

public static double toDouble(Object i,
                              double def)
Convert an object to an double. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return the default value.

Parameters:
i - the object to convert
def - the default value if conversion is impossible
Returns:
the converted value as a double

toDouble

public static double toDouble(Object i)
Convert an object to an double. Boolean values return 0 for false, 1 for true. String values are parsed to a number, if possible. All remaining values return -1.

Parameters:
i - the object to convert
Returns:
the converted value as a double

toDate

public static Date toDate(Object d)
Convert an object to a Date. String values are parsed to a date, if possible. Numeric values are assumed to represent number of seconds since 1970. All remaining values return 1969-12-31 23:59 GMT.

Parameters:
d - the object to convert
Returns:
the converted value as a Date, never null
Since:
MMBase-1.7


MMBase build 1.8.1.20060716