com.abstractics.utils.converter
Class Converter

java.lang.Object
  extended bycom.abstractics.utils.converter.Converter

public final class Converter
extends java.lang.Object

Singleton that can translate objects into other objects. Conversion is handled through com.abstractics.xmlpanel.utils.stringtoobject.ObjectConverter objects that can be loaded into this class.

The following classes are supported and pre-loaded into the converter upon startup:

Version:
$Id: Converter.java,v 1.3 2006/03/15 05:37:30 andyman99 Exp $
Author:
Andrew Lawrence, Copyright 2006 Abstractics, L.L.C., Licensed under the Apache License, Version 2.0

Method Summary
static void addConstant(java.lang.Object obj, java.lang.Class convertInto, java.lang.Object value)
          Gives the ability to have the ConvertObject return "static" values for certain conversion classes and values.
static java.lang.Object convert(java.lang.Object obj, java.lang.Class convertInto)
          Chained method that calls convert(Object, Class, Locale) with the default locale returned from Locale.getDefault()
static java.lang.Object convert(java.lang.Object obj, java.lang.Class convertInto, java.util.Locale locale)
          Converts the given object into an instance of the given class, using ObjectConverters that have been registered.
static boolean convertToBoolean(java.lang.Object obj)
          Convenience method to return a primative boolean from conversion
static double convertToDouble(java.lang.Object obj)
          Convenience method to return a primative double from conversion
static int convertToInt(java.lang.Object obj)
          Convenience method to return a primative int from conversion
static long convertToLong(java.lang.Object obj)
          Convenience method to return a primative long from conversion
static void deregister(java.lang.Class forClass)
          Any ObjectConverters that are registered to convert into the class given are removed.
static void register(ObjectConverter converter)
          Registers the given ObjectConverter
static void registerResourceBaseName(java.lang.String baseName)
          Registers the base name for a ResourceBundle that should be used for String replacement operations.
static void reset()
          Resets the state of the converter to have the defaults.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

convert

public static java.lang.Object convert(java.lang.Object obj,
                                       java.lang.Class convertInto)
                                throws ObjectConverterException
Chained method that calls convert(Object, Class, Locale) with the default locale returned from Locale.getDefault()

Parameters:
obj - Object to convert
convertInto - Class to convert into
Returns:
Converted object - will be an instance of the convertInto class
Throws:
ObjectConverterException - Thrown if conversion is not possible
java.lang.IllegalArgumentException - Thrown if convertInto is null

convert

public static java.lang.Object convert(java.lang.Object obj,
                                       java.lang.Class convertInto,
                                       java.util.Locale locale)
                                throws ObjectConverterException
Converts the given object into an instance of the given class, using ObjectConverters that have been registered.

Parameters:
obj - Object to convert
convertInto - Class to convert into
locale - Locale to use for conversion. This locale is used as the basis for string replacement with any registered resource base names. The locale is also passed to the ObjectConverter in case locale specific conversion is necessary
Returns:
Converted object - will be an instance of the convertInto class
Throws:
ObjectConverterException - Thrown if conversion is not possible
java.lang.IllegalArgumentException - Thrown if convertInto is null

convertToBoolean

public static boolean convertToBoolean(java.lang.Object obj)
                                throws ObjectConverterException
Convenience method to return a primative boolean from conversion

Parameters:
obj - Object to convert
Returns:
boolean representation of the object. If conversion resulted in null, then false is returned
Throws:
ObjectConverterException - Thrown if conversion is not possible

convertToInt

public static int convertToInt(java.lang.Object obj)
                        throws ObjectConverterException
Convenience method to return a primative int from conversion

Parameters:
obj - Object to convert
Returns:
int representation of the object. If conversion resulted in null, then 0 is returned
Throws:
ObjectConverterException - Thrown if conversion is not possible

convertToLong

public static long convertToLong(java.lang.Object obj)
                          throws ObjectConverterException
Convenience method to return a primative long from conversion

Parameters:
obj - Object to convert
Returns:
long representation of the object. If conversion resulted in null, then 0 is returned
Throws:
ObjectConverterException - Thrown if conversion is not possible

convertToDouble

public static double convertToDouble(java.lang.Object obj)
                              throws ObjectConverterException
Convenience method to return a primative double from conversion

Parameters:
obj - Object to convert
Returns:
double representation of the object. If conversion resulted in null, then 0.0 is returned
Throws:
ObjectConverterException - Thrown if conversion is not possible

register

public static void register(ObjectConverter converter)
Registers the given ObjectConverter

Parameters:
converter - Converter to register

registerResourceBaseName

public static void registerResourceBaseName(java.lang.String baseName)
Registers the base name for a ResourceBundle that should be used for String replacement operations. Strings that are sent into the converter with $R{resourcekey} text will have that text replaces according to the Resource.

Parameters:
baseName -

deregister

public static void deregister(java.lang.Class forClass)
Any ObjectConverters that are registered to convert into the class given are removed.

Parameters:
forClass - Class to remove ObjectConverters for

reset

public static void reset()
Resets the state of the converter to have the defaults. Restoring all default loaded converters, removing all constant values, and removing all resource base names.


addConstant

public static void addConstant(java.lang.Object obj,
                               java.lang.Class convertInto,
                               java.lang.Object value)
Gives the ability to have the ConvertObject return "static" values for certain conversion classes and values. This becomes useful to "cache" items into the conversion process.
For example, it may be desirous to have a company logo image. It would then be convenient to be able to get that image by simply specifying "companyLogo" as the value to convert, rather than the entire path to the logo. To accomplish this, the image would need to be pre-registered as a constant using this method:
addConstant("companyLogo", Icon.class, new ImageIcon("Foo"))
When a conversion is asked to be done with a value of "companyLogo" and a class to convert to of convertInto (or a subclass thereof) then the value specified to this method will automatically be returned, without a call to the ObjectConverter registered for that class.

Parameters:
obj - Object that should trigger use of the constant (in coordination with the convertInto class)
convertInto - Class that should trigger use of the constant (in coordination with the obj Object)
value - The value that should be returned for this constant
Throws:
java.lang.IllegalArgumentException - Thrown if the object or class provided is null