You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2004/06/27 06:42:54 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ClassUtilsTest.java
bayard 2004/06/26 21:42:54
Modified: lang/src/java/org/apache/commons/lang ClassUtils.java
lang/src/test/org/apache/commons/lang ClassUtilsTest.java
Log:
This adds an array-argument based version of primitiveToWrapper. Apart from the argument of not having array based versions for every method, I can't see any reason not to add it, so have patched, modified and committing.
The origonal version optimised by returning the passed in array if there were no primitives. This seems a bit magical.
PR: #27640
Submitted by: Alban Peignier
Revision Changes Path
1.29 +26 -1 jakarta-commons/lang/src/java/org/apache/commons/lang/ClassUtils.java
Index: ClassUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ClassUtils.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- ClassUtils.java 24 Feb 2004 06:01:28 -0000 1.28
+++ ClassUtils.java 27 Jun 2004 04:42:54 -0000 1.29
@@ -511,6 +511,31 @@
}
return convertedClass;
}
+
+ /**
+ * <p>Converts the specified array of primitive Class objects to an array of
+ * its corresponding wrapper Class objects.</p>
+ *
+ * @param classes the class array to convert, may be null or empty
+ * @return an array which contains for each given class, the wrapper class or
+ * the original class if class is not a primitive. <code>null</code> if null input.
+ * Empty array if an empty array passed in.
+ */
+ public static Class[] primitivesToWrappers(Class[] classes) {
+ if (classes == null) {
+ return null;
+ }
+
+ if (classes.length == 0) {
+ return ArrayUtils.EMPTY_CLASS_ARRAY;
+ }
+
+ Class[] convertedClasses = new Class[classes.length];
+ for (int i=0; i < classes.length; i++) {
+ convertedClasses[i] = primitiveToWrapper( classes[i] );
+ }
+ return convertedClasses;
+ }
// Inner class
// ----------------------------------------------------------------------
1.12 +33 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/ClassUtilsTest.java
Index: ClassUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ClassUtilsTest.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ClassUtilsTest.java 18 Feb 2004 23:22:29 -0000 1.11
+++ ClassUtilsTest.java 27 Jun 2004 04:42:54 -0000 1.12
@@ -375,6 +375,38 @@
assertNull("null -> null",
ClassUtils.primitiveToWrapper(null));
}
+
+ public void testPrimitivesToWrappers() {
+ // test null
+ assertNull("null -> null",
+ ClassUtils.primitivesToWrappers(null));
+ // test empty array
+ assertEquals("empty -> empty",
+ ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY));
+
+ // test an array of various classes
+ final Class[] primitives = new Class[] {
+ Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE,
+ Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE,
+ String.class, ClassUtils.class
+ };
+ Class[] wrappers= ClassUtils.primitivesToWrappers(primitives);
+
+ for (int i=0; i < primitives.length; i++) {
+ // test each returned wrapper
+ Class primitive = primitives[i];
+ Class expectedWrapper = ClassUtils.primitiveToWrapper(primitive);
+
+ assertEquals(primitive + " -> " + expectedWrapper, expectedWrapper, wrappers[i]);
+ }
+
+ // test an array of no primitive classes
+ final Class[] noPrimitives = new Class[] {
+ String.class, ClassUtils.class
+ };
+ // This used to return the exact same array, but no longer does.
+ assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
+ }
public void testClassComparator() {
Comparator comparator = ClassUtils.CLASS_NAME_COMPARATOR;
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org