Dashboard > GeoTools 2.5 Users Guide > ... > 04 API > 09 Converter Tutorial
GeoTools 2.5 Users Guide
09 Converter Tutorial
Added by Justin Deoliveira , last edited by jgarnett on Mar 23, 2007  (view change)
Labels: 
(None)

Overview

The Converter API is a way to convert a value of one type into a value of another type with an extension point.

The most common case of this is to parse a string into another type of value.

Converter Interface

The Converter interface looks like this:

public interface Converter {
    Object convert( Object source, Class target ) throws Exception;
}

When you implement this you can either:

  • Handle it correctly (returning an object of the target class
  • Return null (if you could not handle it correctly)
  • Throw an exeption (if you could not handle it correctly)

Java 5 Example - Enum

Lets look at a real world example coming to us from the land of Java 5. Java 5 features a new type of construct called Enum. This is not something GeoTools can usually deal with as a Java 1.4 project - but by supplying a converter you can teach GeoTools to get along with your Java 5 application.

Example enum:

public enum Choice {
   THIS,
   THAT;
}

Example enum converter:

class Choice2TextConverter {
   Object convert( Object source, Class target ){
        if( target != Choice.class ) return null;
        return Choice.valueOf( (String) source ) );
   }
}

In general the freedom to return null or throw an exception lets you program converters very quickly.

Example Use (requires main)

A common case that we see in many places in GeoTools is parsing a string into the type defined by an AttributeType.

GeoTools 2.3 code (before converters were around):

FeatureType featureType = .... ; 
AttributeType intType = featureType.getAttributeType( "intProperty" );
String string = "1234";

Integer integer = (Integer) intType.parse( string );

Using the Converters utility class this becomes:

FeatureType featureType = .... ; 
AttributeType intType = featureType.getAttributeType( "intProperty" );
String string = "1234";

Integer integer = Converters.convert( string, intType.getType() );

Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators