org.mmbase.util
Class Casting

java.lang.Object
  extended by org.mmbase.util.Casting

public class Casting
extends Object

Collects MMBase specific 'cast' information, as static to... functions. This is used (and used to be implemented) in MMObjectNode. But this functionality is more generic to MMbase.

Since:
MMBase-1.6
Version:
$Id: Casting.java 42623 2010-06-21 09:42:51Z michiel $
Author:
Michiel Meeuwissen

Nested Class Summary
static class Casting.ListWrapper
           
(package private) static class Casting.StringSerializableInputStream
          A SerializableInputStream where the toString represents the (escaped) contents of the stream itself.
static class Casting.StringWrapper
          Wraps a String with an 'Escaper'.
static interface Casting.Unwrappable
          Clases implementing this will not be wrapped by wrap(java.lang.Object, org.mmbase.util.transformers.CharTransformer), even if the e.g.
 
Field Summary
static Pattern BOOLEAN_PATTERN
           
(package private) static DocumentBuilder DOCUMENTBUILDER
           
static Pattern DOUBLE_PATTERN
           
static ThreadLocal<DateFormat> ISO_8601_DATE
           
static ThreadLocal<DateFormat> ISO_8601_LOOSE
          A Date formatter that creates a date based on a ISO 8601 date and a ISO 8601 time.
static ThreadLocal<DateFormat> ISO_8601_TIME
           
static ThreadLocal<DateFormat> ISO_8601_UTC
          A Date formatter that creates a ISO 8601 datetime according to UTC/GMT.
 
Method Summary
static boolean canCast(Class<? extends Object> from, Class<? extends Object> to)
          This method should report whether toType(java.lang.Class, java.lang.Object) is implemented for given 2 classes.
static boolean equals(Object o1, Object o2)
           
(package private) static String escape(CharTransformer escaper, CharSequence string)
           
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 void setHelper(Caster h)
           
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 Collection toCollection(Object o, String delimiter)
          Transforms an object to a collection.
static Date toDate(Object d)
          Convert an object to a Date.
static BigDecimal toDecimal(Object i)
           
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 SerializableInputStream toSerializableInputStream(Object obj)
           
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 StringBuilder toStringBuilder(StringBuilder buffer, Object o)
          Convert an object to a string, using a StringBuilder
static
<C> C
toType(Class<C> type, Object value)
          Tries to 'cast' an object for use with the provided class.
static
<C> C
toType(Class<C> type, Object cloud, 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 an object in another 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 ThreadLocal<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 ThreadLocal<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 ThreadLocal<DateFormat> ISO_8601_DATE

ISO_8601_TIME

public static final ThreadLocal<DateFormat> ISO_8601_TIME

DOCUMENTBUILDER

static DocumentBuilder DOCUMENTBUILDER

BOOLEAN_PATTERN

public static final Pattern BOOLEAN_PATTERN

DOUBLE_PATTERN

public static final Pattern DOUBLE_PATTERN
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 <C> C toType(Class<C> 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 <C> C toType(Class<C> type,
                           Object 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

canCast

public static boolean canCast(Class<? extends Object> from,
                              Class<? extends Object> to)
This method should report whether toType(java.lang.Class, java.lang.Object) is implemented for given 2 classes. It is not yet fully implemented.

Since:
MMBase-1.9.2

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

toStringBuilder

public static StringBuilder toStringBuilder(StringBuilder buffer,
                                            Object o)
Convert an object to a string, using a StringBuilder

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

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 an object in another 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 JSTL of course doesn'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.

escape

static String escape(CharTransformer escaper,
                     CharSequence string)

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,
                                      String delimiter)
Transforms an object to a collection. If the object is a collection already, then nothing happens. If it is a Map, then the 'entry set' is returned. A string is interpreted as a comma-separated list of strings. Other objects are wrapped in an ArrayList with one element.

Since:
MMBase-1.8.5

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)

toSerializableInputStream

public static SerializableInputStream toSerializableInputStream(Object obj)

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

toDecimal

public static BigDecimal toDecimal(Object i)
Since:
MMBase-1.9.1

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

equals

public static boolean equals(Object o1,
                             Object o2)
Since:
MMBase-1.9

setHelper

public static void setHelper(Caster h)
Since:
MMBase-2.0


MMBase 2.0-SNAPSHOT - null