commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Carman" <ja...@carmanconsulting.com>
Subject RE: [collections] New Transformer Class(es)...
Date Mon, 19 Sep 2005 11:11:38 GMT
I just thought of something.  We might add in a special case for String
input.  It would return String.charAt( index ) as an object.

-----Original Message-----
From: James Carman [mailto:james@carmanconsulting.com] 
Sent: Monday, September 19, 2005 6:57 AM
To: 'Jakarta Commons Developers List'
Subject: [collections] New Transformer Class(es)...

How about a couple of new transformer classes (or maybe one smart one) which
transforms either an array or java.util.List into the object within it at a
particular index?  We could either create two classes ArrayIndexTransformer
and ListIndexTransformer or we could create one smart one called
IndexTransformer which figures out which type of object it's dealing with
(probably the best option, since we have to determine the type of array
anyway using if/else statements).  Of course, an index value (an integer)
would need to be passed into the constructor.  Again, we have used this on
our current project and it turns out to be quite handy.  It would look
something like this (don't know about the null case; we could just return
null):

 

public class IndexTransformer implements Transformer, Serializable

{

    private final int index;

 

    public IndexTransformer( int index )

    {

        this.index = index;

    }

 

    public Object transform( Object input )

    {

        if( input == null )

        {

            throw new IllegalArgumentException( "Cannot transform a null
object." );

        }

        if( input instanceof List )

        {

            return ( ( List )input ).get( index );

        }

        else if( input instanceof Object[] )

        {

            return ( ( Object[] )input )[index];

        }

        else if( input instanceof int[] )

        {

            return new Integer( ( ( int[] )input )[index] );

        }

        else if( input instanceof long[] )

        {

            return new Long( ( ( long[] )input )[index] );

        }

        else if( input instanceof short[] )

        {

            return new Short( ( ( short[] )input )[index] );

        }

        else if( input instanceof byte[] )

        {

            return new Byte( ( ( byte[] )input )[index] );

        }

        else if( input instanceof char[] )

        {

            return new Character( ( ( char[] )input )[index] );

        }

        else if( input instanceof float[] )

        {

            return new Float( ( ( float[] )input )[index] );

        }

        else if( input instanceof double[] )

        {

            return new Double( ( ( double[] )input )[index] );

        }

        else if( input instanceof boolean[] )

        {

            return new Boolean( ( ( boolean[] )input )[index] );

        }

        throw new IllegalArgumentException( "Unable to transform object of
type " + input.getClass().getName() + "." );

    }

}




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message