diff --git a/src/java/org/apache/commons/collections/TransformerUtils.java b/src/java/org/apache/commons/collections/TransformerUtils.java index dfe39d1a1..1e5e4d952 100644 --- a/src/java/org/apache/commons/collections/TransformerUtils.java +++ b/src/java/org/apache/commons/collections/TransformerUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/TransformerUtils.java,v 1.3 2003/08/31 17:26:44 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/TransformerUtils.java,v 1.4 2003/09/17 20:28:30 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -82,13 +82,15 @@ import java.util.Map; *
  • Null - always returns null *
  • NOP - returns the input object, which should be immutable *
  • Exception - always throws an exception + *
  • StringValue - returns a java.lang.String representation of the input object * * All the supplied transformers are Serializable. * * @since Commons Collections 3.0 - * @version $Revision: 1.3 $ $Date: 2003/08/31 17:26:44 $ + * @version $Revision: 1.4 $ $Date: 2003/09/17 20:28:30 $ * * @author Stephen Colebourne + * @author James Carman */ public class TransformerUtils { @@ -113,6 +115,12 @@ public class TransformerUtils { */ private static final Transformer INSTANTIATE_TRANSFORMER = new InstantiateTransformer(null, null); + /** + * A transformer that returns a java.lang.String representation + * of the input object. + */ + private static final Transformer STRING_VALUE_TRANSFORMER = new StringValueTransformer(); + /** * This class is not normally instantiated. */ @@ -490,6 +498,17 @@ public class TransformerUtils { return new InvokerTransformer(methodName, paramTypes, args); } + /** + * Gets a transformer that returns a java.lang.String + * representation of the input object. This is achieved via the + * toString method, null returns 'null'. + * + * @return the transformer + */ + public static Transformer stringValueTransformer() { + return STRING_VALUE_TRANSFORMER; + } + /** * Copy method * @@ -934,5 +953,23 @@ public class TransformerUtils { } } } + + // StringValueTransformer + //---------------------------------------------------------------------------------- -} + /** + * StringValueTransformer returns a java.lang.String representation + * of the input object using the String.valueOf() method. + */ + private static class StringValueTransformer implements Transformer, Serializable { + + /** + * returns a java.lang.String representation of the input object + * using the String.valueOf() method. + */ + public Object transform(Object input) { + return String.valueOf(input); + } + } + +} \ No newline at end of file diff --git a/src/test/org/apache/commons/collections/TestTransformerUtils.java b/src/test/org/apache/commons/collections/TestTransformerUtils.java index 12d93d1d0..8de57e8b6 100644 --- a/src/test/org/apache/commons/collections/TestTransformerUtils.java +++ b/src/test/org/apache/commons/collections/TestTransformerUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTransformerUtils.java,v 1.2 2003/08/31 17:28:43 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTransformerUtils.java,v 1.3 2003/09/17 20:28:30 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -72,9 +72,10 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.collections.TransformerUtils class. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:28:43 $ + * @version $Revision: 1.3 $ $Date: 2003/09/17 20:28:30 $ * * @author Stephen Colebourne + * @author James Carman */ public class TestTransformerUtils extends junit.framework.TestCase { @@ -535,4 +536,16 @@ public class TestTransformerUtils extends junit.framework.TestCase { fail(); } + // stringValueTransformer + //------------------------------------------------------------------ + + public void testStringValueTransformer() { + assertNotNull( "StringValueTransformer should NEVER return a null value.", + TransformerUtils.stringValueTransformer().transform(null)); + assertEquals( "StringValueTransformer should return \"null\" when given a null argument.", "null", + TransformerUtils.stringValueTransformer().transform(null)); + assertEquals( "StringValueTransformer should return toString value", "6", + TransformerUtils.stringValueTransformer().transform(new Integer(6))); + } + }