Open source type coercion framework?

S

Simon Brooke

I'm rewriting a library I wrote back in 1997, bringing it up to date with
Java 6 and at the same time trying to replace as much as possible of my
own custom code with relatively well-known standard libraries for ease of
maintenance.

One of the particularly useful things my library could do was take an
arbitrary object, and make the best possible stab at interpreting that
object as an object of a different class - often useful, for example,
when writing to a SQL database. So, for example, if your database table
has a date field, you'd call context.getValueAsCalendar( String token ),
and the context would attempt to interpret whatever object it had stored
against that token as a valid date and return that date as a Calendar
(actual code below, in case anyone is interested).

In re-engineering this I'm thinking of an extensible type coercion
framework, in which you have some form of an Alchemist object into which
you can plug Transmutations, such that you can do something like:

Alchemist zosimus = new Alchemist();

zosimus.registerTransmutation( new StringToCalendarTransmutation());

Calendar when =
(Calendar)zosimus.transmuteTo<Calendar>( "27th December 1992");

This is a useful thing to be able to do. Not earth shattering, but
useful. And thinking about it it seemed to me that someone must already
have done it; but I haven't found such a library (possibly because I'm
using the wrong keywords). Anyone have any suggestions?



In case you're interested the legacy code I'm trying to replace has
methods of the form:

/**
* Extract the value associated with this token as a
java.util.Calendar
* object
*/
public java.util.Calendar getValueAsCalendar( String token )
throws DataFormatException
{
Object value = this.get( token );

java.util.Calendar when = new uk.co.weft.dbutil.Calendar
( );

// if not now, when?
DateFormat f = DateFormat.getTimeInstance
( DateFormat.MEDIUM );
f.setCalendar( when );
f.setLenient( true );

if ( value instanceof java.util.Calendar )
{
when = (java.util.Calendar) value;
}
else
{
if ( value instanceof java.util.Date )
{
when.setTime( (java.util.Date) value );
}
else
{
if ( value instanceof String )
{
String string = (String) value;

try
{
/* try to parse it as a
time... */
when.setTime( f.parse
( string ) );
}
catch ( ParseException p )
{
try
{
/* no? Then try
as a date... */
f =
DateFormat.getDateInstance( DateFormat.MEDIUM );
f.setCalendar
( when );
f.setLenient
( true );
when.setTime
( f.parse( string ) );
}
catch ( ParseException q )
{
try
{
/* still
no? try as a date/time (timestamp) */
f =
DateFormat.getDateTimeInstance( DateFormat.MEDIUM,

DateFormat.MEDIUM );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}
catch
( ParseException r )
{
try
{
/
* OK, what about ISO format? */
f
= new SimpleDateFormat(
"yyyy-
MM-dd'T'hh:mm:ss'Z'z" );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}
catch
( ParseException s )
{

try
{
/
* OK, what about ISO date only? */

f = new SimpleDateFormat( "yyyy-MM-dd" );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}

catch ( ParseException t )
{

try

{
/
* OK, what about ISO time only? */

f = new SimpleDateFormat(
"hh:mm:ss" );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}

catch ( ParseException u )

{
/
* still no? Give up */

throw new DataFormatException( u.getMessage( ) );
}
}
}
}
}
}
}
else
{
if ( ( value == null ) || value
instanceof DataNull )
{
when = null;
}
else
{
when.setTime( new
java.util.Date( ) );

// TODO: Is this wise? Is
it *safe*?
}
}
}
}

return when;
}
 
J

Jeff Higgins

I'm rewriting a library I wrote back in 1997, bringing it up to date with
Java 6 and at the same time trying to replace as much as possible of my
own custom code with relatively well-known standard libraries for ease of
maintenance.

One of the particularly useful things my library could do was take an
arbitrary object, and make the best possible stab at interpreting that
object as an object of a different class - often useful, for example,
when writing to a SQL database. So, for example, if your database table
has a date field, you'd call context.getValueAsCalendar( String token ),
and the context would attempt to interpret whatever object it had stored
against that token as a valid date and return that date as a Calendar
(actual code below, in case anyone is interested).

In re-engineering this I'm thinking of an extensible type coercion
framework, in which you have some form of an Alchemist object into which
you can plug Transmutations, such that you can do something like:

Alchemist zosimus = new Alchemist();

zosimus.registerTransmutation( new StringToCalendarTransmutation());

Calendar when =
(Calendar)zosimus.transmuteTo<Calendar>( "27th December 1992");

This is a useful thing to be able to do. Not earth shattering, but
useful. And thinking about it it seemed to me that someone must already
have done it; but I haven't found such a library (possibly because I'm
using the wrong keywords). Anyone have any suggestions?



In case you're interested the legacy code I'm trying to replace has
methods of the form:

/**
* Extract the value associated with this token as a
java.util.Calendar
* object
*/
public java.util.Calendar getValueAsCalendar( String token )
throws DataFormatException
{
Object value = this.get( token );

java.util.Calendar when = new uk.co.weft.dbutil.Calendar
( );

// if not now, when?
DateFormat f = DateFormat.getTimeInstance
( DateFormat.MEDIUM );
f.setCalendar( when );
f.setLenient( true );

if ( value instanceof java.util.Calendar )
{
when = (java.util.Calendar) value;
}
else
{
if ( value instanceof java.util.Date )
{
when.setTime( (java.util.Date) value );
}
else
{
if ( value instanceof String )
{
String string = (String) value;

try
{
/* try to parse it as a
time... */
when.setTime( f.parse
( string ) );
}
catch ( ParseException p )
{
try
{
/* no? Then try
as a date... */
f =
DateFormat.getDateInstance( DateFormat.MEDIUM );
f.setCalendar
( when );
f.setLenient
( true );
when.setTime
( f.parse( string ) );
}
catch ( ParseException q )
{
try
{
/* still
no? try as a date/time (timestamp) */
f =
DateFormat.getDateTimeInstance( DateFormat.MEDIUM,

DateFormat.MEDIUM );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}
catch
( ParseException r )
{
try
{
/
* OK, what about ISO format? */
f
= new SimpleDateFormat(
"yyyy-
MM-dd'T'hh:mm:ss'Z'z" );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}
catch
( ParseException s )
{

try
{
/
* OK, what about ISO date only? */

f = new SimpleDateFormat( "yyyy-MM-dd" );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}

catch ( ParseException t )
{

try

{
/
* OK, what about ISO time only? */

f = new SimpleDateFormat(
"hh:mm:ss" );


f.setCalendar( when );

f.setLenient( true );

when.setTime( f.parse( string ) );
}

catch ( ParseException u )

{
/
* still no? Give up */

throw new DataFormatException( u.getMessage( ) );
}
}
}
}
}
}
}
else
{
if ( ( value == null ) || value
instanceof DataNull )
{
when = null;
}
else
{
when.setTime( new
java.util.Date( ) );

// TODO: Is this wise? Is
it *safe*?
}
}
}
}

return when;
}

wtf

/**
* Extract the value associated with this token
* as a java.util.Calendar object
*/
public java.util.Calendar getValueAsCalendar(String token)
throws DataFormatException {
Object value = this.get(token);
java.util.Calendar when = new uk.co.weft.dbutil.Calendar();

// if not now, when?
DateFormat f = DateFormat.getTimeInstance(DateFormat.MEDIUM);
f.setCalendar(when);
f.setLenient(true);

if (value instanceof java.util.Calendar) {
when = (java.util.Calendar) value;
} else {
if (value instanceof java.util.Date) {
when.setTime((java.util.Date) value);
} else {
if (value instanceof String) {
String string = (String) value;
try {
/* try to parse it as a time... */
when.setTime(f.parse(string));
} catch (ParseException p) {
try {
/* no? Then try as a date... */
f = DateFormat.getDateInstance(DateFormat.MEDIUM);
f.setCalendar(when);
f.setLenient(true);
when.setTime(f.parse(string));
} catch (ParseException q) {
try {
/* still no? try as a date/time (timestamp) */
f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM);
f.setCalendar(when);
f.setLenient(true);
when.setTime(f.parse(string));
} catch (ParseException r) {
try {
/* OK, what about ISO format? */
f = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'z");
f.setCalendar(when);
f.setLenient(true);
when.setTime(f.parse(string));
} catch (ParseException s) {
try {
/* OK, what about ISO date only? */
f = new SimpleDateFormat("yyyy-MM-dd");
f.setCalendar(when);
f.setLenient(true);
when.setTime(f.parse(string));
} catch (ParseException t) {
try {
/* OK, what about ISO time only? */
f = new SimpleDateFormat("hh:mm:ss");
f.setCalendar(when);
f.setLenient(true);
when.setTime(f.parse(string));
} catch (ParseException u) {
/* still no? Give up */
throw new DataFormatException(u.getMessage());
}
}
}
}
}
}
} else {
if ((value == null) || value instanceof DataNull) {
when = null;
} else {
when.setTime(new java.util.Date());

// TODO: Is this wise? Is it *safe*?
}
}
}
}
return when;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top