ilog.views.util.convert
Class IlvConvert

java.lang.Object
  extended by ilog.views.util.convert.IlvConvert
All Implemented Interfaces:
Serializable

public class IlvConvert
extends Object
implements Serializable

The class IlvConvert is used to convert a value from one type to another. All methods are static.

Conversions are handled by a table of IlvConverter objects. Each converter handles one or several "from" types and one or several "to" types. The conversion is actually performed by the convert() method of IlvConverter.

A number of predefined filters are provided:

More filters supplied by other packages or by the application can be added.

It is also possible to register a PropertyEditor which converts value from/to String through its get/setAsText() methods.

This is a bit different from IlvConvertForSetters, which converts a value from a PropertyDescriptor.

The code comes mainly from BGO (IlvValueConverter)

Since:
JViews 6.0
See Also:
IlvConverter, IlvLocalizedConverter, IlvConvertForSetters, Serialized Form

Method Summary
static void addConverter(IlvConverter converter)
          Adds a converter to the global list of converters used to convert values.
static void addConverter(PropertyEditor pe, Class type)
          Adds a converter to the global list of converters used to convert values.
static Object convert(Object value, Class toType)
          Converts the type of a value to a new type.
static Object convert(Object value, Class toType, Locale locale)
          Converts the type of a value to a new type.
static boolean convertToBoolean(Object value)
          Converts a value from its current type to a boolean value.
static byte convertToByte(Object value)
          Converts a value from its current type to a byte.
static double convertToDouble(Object value)
          Converts a value from its current type to a double.
static float convertToFloat(Object value)
          Converts a value from its current type to a float.
static int convertToInt(Object value)
          Converts a value from its current type to an integer.
static long convertToLong(Object value)
          Converts a value from its current type to a long integer.
static short convertToShort(Object value)
          Converts a value from its current type to a short.
static String convertToString(Object value)
          Converts a value from its current type to a string.
static IlvConverter getConverter(Class from, Class to)
          Returns the converter which can handle given parameters
static boolean isConverterLocalized(Class from, Class to)
          Returns true whether the converter which can handle given parameters is localized.
static boolean isConvertible(Class fromType, Class toType)
          Returns true if a values from type fromType can be converted to a value of type toType.
static void removeConverter(Class from, Class to)
          Removes the converter between given types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addConverter

public static final void addConverter(IlvConverter converter)
Adds a converter to the global list of converters used to convert values.

Parameters:
converter - the new value converter. The source and target types handled by the converter are determined by the result of the methods fromTypes and toTypes of the converter.

addConverter

public static final void addConverter(PropertyEditor pe,
                                      Class type)
Adds a converter to the global list of converters used to convert values. A PropertyEditor converts from/to String to/from its property value.

Parameters:
pe - the new value converter which must implement its getAsTex() and setAsText() methods.
type - the type of the property associated to the given property editor.

getConverter

public static IlvConverter getConverter(Class from,
                                        Class to)
Returns the converter which can handle given parameters

Parameters:
from - the source type
to - the target type

isConverterLocalized

public static boolean isConverterLocalized(Class from,
                                           Class to)
Returns true whether the converter which can handle given parameters is localized. Returns false also if the converter doesn't exist.

Parameters:
from - the source type
to - the target type

removeConverter

public static void removeConverter(Class from,
                                   Class to)
Removes the converter between given types.

Parameters:
from - the source type
to - the target type

convert

public static final Object convert(Object value,
                                   Class toType)
                            throws IlvConvertException
Converts the type of a value to a new type.

Parameters:
value - the value to be converted.
toType - the type to which the value must be converted.
Returns:
the converted value, which is an instance of the class toType.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.

isConvertible

public static boolean isConvertible(Class fromType,
                                    Class toType)
Returns true if a values from type fromType can be converted to a value of type toType.

Parameters:
fromType - the source type
toType - the destination type

convert

public static final Object convert(Object value,
                                   Class toType,
                                   Locale locale)
                            throws IlvConvertException
Converts the type of a value to a new type.

Parameters:
value - the value to be converted.
toType - the type to which the value must be converted.
locale - the locale used for the conversion
Returns:
the converted value, which is an instance of the class toType.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.

convertToString

public static final String convertToString(Object value)
                                    throws IlvConvertException
Converts a value from its current type to a string. This method is a shortcut for:
     (String)convert(value, String.class)
 

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToInt

public static final int convertToInt(Object value)
                              throws IlvConvertException
Converts a value from its current type to an integer. This method is a shortcut for:
     ((Integer)convert(value, Integer.class)).intValue()
 

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToLong

public static final long convertToLong(Object value)
                                throws IlvConvertException
Converts a value from its current type to a long integer. This method is a shortcut for:
     ((Long)convert(value, Long.class)).longValue()
 

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToBoolean

public static final boolean convertToBoolean(Object value)
                                      throws IlvConvertException
Converts a value from its current type to a boolean value. This method is a shortcut for:
     ((Boolean)convert(value, Boolean.class)).booleanValue()
 

Parameters:
value - the value to convert
Throws:
IlvConvertException - if not converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToFloat

public static final float convertToFloat(Object value)
                                  throws IlvConvertException
Converts a value from its current type to a float. This method is a shortcut for:
     ((Float)convert(value, Float.class)).floatValue()
 

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToDouble

public static final double convertToDouble(Object value)
                                    throws IlvConvertException
Converts a value from its current type to a double. This method is a shortcut for:
     ((Double)convert(value, Double.class)).doubleValue()
 

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToShort

public static final short convertToShort(Object value)
                                  throws IlvConvertException
Converts a value from its current type to a short. This method is a shortcut for:
     ((Short)convert(value, Short.class)).shortValue()
 

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)

convertToByte

public static final byte convertToByte(Object value)
                                throws IlvConvertException
Converts a value from its current type to a byte.

Parameters:
value - the value to be converted.
Throws:
IlvConvertException - if no converter was registered for the requested conversion or if the value could not be converted to the requested type.
See Also:
convert(java.lang.Object, java.lang.Class)


Copyright © 1996-2007 ILOG S.A. All rights reserved.   Documentation homepage.