|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.jbeans.BaseTypeConverter
This class is the base type converter for all the type converters it is also the type converter that handles strings and arrays of strings. This converter contains implementation for all the type converter methods and also includes standard implementations for the convert, convertArray, convertArrayToArray, convertToArray and convertStringToArray. In most cases, these methods do not need to be overridden. See each method for details on what exactly the method does in order to help determine if overriding is necessary.
Constructor Summary | |
BaseTypeConverter()
|
Method Summary | |
Object |
convert(Object value,
Class convertTo)
If the convertTo class is an array, this method calls convertToArray passing it the value and the convertTo component type (by calling the getComponentType method of java.lang.Class). |
Object |
convertArray(Object[] values,
Class convertTo)
If the convertTo type parameter is an array, this method calls the convertArrayToArray method with the parameters passed to this method. |
Object[] |
convertArrayToArray(Object[] values,
Class convertTo)
If the convertTo type is an array, this method uses the component type for the convertTo type. |
Object |
convertString(String value,
Class convertTo)
This method is usually the only method that needs to be overridden in sub-classes This is where a String value is converted to the correct type. |
Object[] |
convertStringToArray(String value,
String delimiter,
Class convertTo)
If the delimiter parameter is not empty or null it is used. |
Object[] |
convertToArray(Object value,
Class convertTo)
If the value is an instanceof an array, then this method calls the convertArrayToArray method by casting the value to an Object array and passing the convertTo type. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public BaseTypeConverter()
Method Detail |
public Object convert(Object value, Class convertTo) throws TypeConversionException
If the value is an instanceof an array, this method calls the convertArray method passing it the value, cast to an array and the convertTo type.
Otherwise, this method calls convertString passing it the value toString and the convertTo type.
If the value is null, then null is returned. If the value is an instance of the convertTo type, this returns the value.
Sub-classes should only override this method if they intend to handle conversion between two Object types, both of which are NOT String. For example, if a sub-class wished to convert Integer objects to Long objects, it could do that in this method. It is a good idea however, that sub-classes call this implementation if they do not handle the conversion.
convert
in interface TypeConverter
value
- The value to convertconvertTo
- The type to convert the value toTypeConversionException
- If there was a problem converting the given
value to the given typepublic Object convertString(String value, Class convertTo) throws TypeConversionException
This implementation does the above check passing null for the delimiter parameter (see convertStringToArray for an explaination of this parameter) if the check succeeds. Otherwise, this method returns the value parameter since the value parameter is already a String. If the value is null, empty or whitespace then this returns null.
This is an important distinction because normally empty Strings or whitspace Strings are considered non-null, but this method treats them as nulls.
convertString
in interface TypeConverter
value
- The String value to convert to the given typeconvertTo
- The type to convert toTypeConversionException
- If the convertTo type is an array and
the convertStringToArray method throws an exceptionpublic Object convertArray(Object[] values, Class convertTo) throws TypeConversionException
Otherwise, this method throws an exception because it is normally not needed to convert an array to an object. If the values array is null, then this method returns null.
convertArray
in interface TypeConverter
values
- The array of values to convert to the given typeconvertTo
- The type to convert toTypeConversionException
- If either the convertArray or convert
method throw an exception (depending on which method is called)
or always (see method comment)public Object[] convertArrayToArray(Object[] values, Class convertTo) throws TypeConversionException
convertArrayToArray
in interface TypeConverter
values
- The array to convert to an array of the given typeconvertTo
- The type to convert to an array of. if this parameter is an array
type, then this method converts to that type.
the an array of the type is converted to.TypeConversionException
- If the convert method throws a TypeConversionException
when each of the positions in the values array are convertedpublic Object[] convertToArray(Object value, Class convertTo) throws TypeConversionException
convertToArray
in interface TypeConverter
value
- The value to convert to an array of the given type or simply the
given type if the given type is an arrayconvertTo
- The type to convert to an array of. If this parameter is an
array type, this method converts to that type.TypeConversionException
- If convertArrayToArray or
convertStringToArray throw a TypeConversionException (depending
on which one is called)public Object[] convertStringToArray(String value, String delimiter, Class convertTo) throws TypeConversionException
Using this delimiter, the string is tokenized and each token is passed to the convertString method, along with the convertTo type. The result is added to a new array of the given convertTo type or the convertTo type (if the convertTo type is an array type). This means that the order of the tokens determines the order of the array elements and the array is as long as the number of tokens. If the value is null, then this returns null
convertStringToArray
in interface TypeConverter
value
- The string to convert to an arraydelimiter
- The string to use as the delimiter when tokenizingconvertTo
- The type to convert the string to. Can either be an array
or an objectTypeConversionException
- If the call to convertString throws an
exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |