Joda System API

Package org.joda.typeconvertor

Object and String type conversion

See:
          Description

Interface Summary
TypeConvertor A TypeConvertor converts between one object type and another.
TypeConvertor.ConvertorFactory A type convertor factory allows for dynamically created type convertors.
 

Class Summary
TypeConvertorManager TypeConvertorManager controls all the object to object type convertors.
 

Exception Summary
ConversionException Exception thrown when an object cannot be converted to the required type.
NoTypeConvertorException Exception thrown when no type convertor can be found for a given class.
 

Package org.joda.typeconvertor Description

Object and String type conversion

The type convertor package gives access to classes that can convert one base Java type and another. They are intended to convert base types, not beans. A base type is defined as a simple object, often immutable, that can easily and desirably express all it's data in a string. Beans do not and should not have type convertors as they can be broken down into base types.

The conversion routines are accessed by the TypeConvertorManager class which defines a static getInstance() method. The returned instance gives access to the actual conversion routines. The default implementation defines type convertors for all the common Java classes. (see the org.joda.typeconvertor.impl package for the predefined types).

Adding custom type conversions

Many domain models will have their own base types. Two typical examples are enumerations and currency. Currency is an example of a simple type convertor that can be implemented in a similar fashion to those supplied by Joda. The only difference is that your custom convertor must be registered by calling:

MyConvertor conv = new MyConvertor();
TypeConvertorManager.getInstance().putTypeConvertor(conv);

Enumerations are an example of a complex type conversion case. The current Java standard for enumerations is to write an enumeration superclass that is extended to a specific class for each enumeration. (Details of the pattern can be found elsewhere.) To avoid the overhead of coding one type convertor for each enumeration class, a way has been developed to allow one type convertor to be written for the superclass.

The type convertor should be written in a similar manner to before, except that there should be a public constructor taking a Class object. The Class object is the class that this instance of the convertor is to convert. As such it must be stored in an instance variable and returned by the getType() method. For example:

public final class MyEnumConvertor extends AbstractTypeConvertor {
  public static final TypeConvertor.ConvertorFactory FACTORY = new Factory();
  
  private static class Factory {
    public TypeConvertor createTypeConvertor(Class type) {
      return new MyEnumConvertor(type);
    }
    public Class getSuperType() {
      return EnumSuperClass.class;
    }
  }

  private final iType;
  private MyEnumConvertor(Class type) {
    iType = type;
  }
  
  public getType() {
    return iType
  }
  
  // actual conversion methods go here...
  
}

This time the convertor must be registered slightly differently. :

TypeConvertorManager.getInstance().putTypeConvertorFactory(
  MyEnumConvertor.FACTORY
);


Joda System API

Copyright © 2001-2003 Stephen Colebourne. All Rights Reserved.