You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/09/17 22:28:30 UTC
cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestTransformerUtils.java
scolebourne 2003/09/17 13:28:30
Modified: collections/src/java/org/apache/commons/collections
TransformerUtils.java
collections/src/test/org/apache/commons/collections
TestTransformerUtils.java
Log:
Add StringValue transformer
from James Carman
Revision Changes Path
1.4 +40 -3 jakarta-commons/collections/src/java/org/apache/commons/collections/TransformerUtils.java
Index: TransformerUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/TransformerUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TransformerUtils.java 31 Aug 2003 17:26:44 -0000 1.3
+++ TransformerUtils.java 17 Sep 2003 20:28:30 -0000 1.4
@@ -82,6 +82,7 @@
* <li>Null - always returns null
* <li>NOP - returns the input object, which should be immutable
* <li>Exception - always throws an exception
+ * <li>StringValue - returns a <code>java.lang.String</code> representation of the input object
* </ul>
* All the supplied transformers are Serializable.
*
@@ -89,6 +90,7 @@
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
+ * @author James Carman
*/
public class TransformerUtils {
@@ -114,6 +116,12 @@
private static final Transformer INSTANTIATE_TRANSFORMER = new InstantiateTransformer(null, null);
/**
+ * A transformer that returns a <code>java.lang.String</code> representation
+ * of the input object.
+ */
+ private static final Transformer STRING_VALUE_TRANSFORMER = new StringValueTransformer();
+
+ /**
* This class is not normally instantiated.
*/
public TransformerUtils() {
@@ -491,6 +499,17 @@
}
/**
+ * Gets a transformer that returns a <code>java.lang.String</code>
+ * representation of the input object. This is achieved via the
+ * <code>toString</code> method, <code>null</code> returns 'null'.
+ *
+ * @return the transformer
+ */
+ public static Transformer stringValueTransformer() {
+ return STRING_VALUE_TRANSFORMER;
+ }
+
+ /**
* Copy method
*
* @param predicates the predicates to copy
@@ -934,5 +953,23 @@
}
}
}
+
+ // StringValueTransformer
+ //----------------------------------------------------------------------------------
+
+ /**
+ * StringValueTransformer returns a <code>java.lang.String</code> representation
+ * of the input object using the <code>String.valueOf()</code> method.
+ */
+ private static class StringValueTransformer implements Transformer, Serializable {
+
+ /**
+ * returns a <code>java.lang.String</code> representation of the input object
+ * using the <code>String.valueOf()</code> method.
+ */
+ public Object transform(Object input) {
+ return String.valueOf(input);
+ }
+ }
-}
+}
\ No newline at end of file
1.3 +15 -2 jakarta-commons/collections/src/test/org/apache/commons/collections/TestTransformerUtils.java
Index: TestTransformerUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestTransformerUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestTransformerUtils.java 31 Aug 2003 17:28:43 -0000 1.2
+++ TestTransformerUtils.java 17 Sep 2003 20:28:30 -0000 1.3
@@ -75,6 +75,7 @@
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
+ * @author James Carman
*/
public class TestTransformerUtils extends junit.framework.TestCase {
@@ -533,6 +534,18 @@
return;
}
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)));
}
}