Open source type coercion framework?

Discussion in 'Java' started by Simon Brooke, Jun 27, 2010.

  1. Simon Brooke

    Simon Brooke Guest

    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;
    }



    --

    ;; Semper in faecibus sumus, sole profundam variat
    Simon Brooke, Jun 27, 2010
    #1
    1. Advertising

  2. Simon Brooke

    Jeff Higgins Guest

    On 6/27/2010 5:25 AM, Simon Brooke wrote:
    > 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;
    }
    Jeff Higgins, Jun 27, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harald Kirsch

    jython type coercion to interface type

    Harald Kirsch, Jun 25, 2003, in forum: Python
    Replies:
    1
    Views:
    1,687
    Harald Kirsch
    Jun 26, 2003
  2. Ian Bicking

    list() coercion

    Ian Bicking, Jul 17, 2003, in forum: Python
    Replies:
    5
    Views:
    339
    Ian Bicking
    Jul 18, 2003
  3. Bob Gailer

    RE: list() coercion

    Bob Gailer, Jul 17, 2003, in forum: Python
    Replies:
    1
    Views:
    309
    Raymond Hettinger
    Jul 17, 2003
  4. Holger Joukl

    python 2 coercion

    Holger Joukl, Aug 31, 2004, in forum: Python
    Replies:
    0
    Views:
    375
    Holger Joukl
    Aug 31, 2004
  5. Neil Schemenauer

    PEP: Generalised String Coercion

    Neil Schemenauer, Aug 6, 2005, in forum: Python
    Replies:
    1
    Views:
    275
Loading...

Share This Page