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


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137852 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2004-06-27 04:42:54 +00:00
parent f16ee38329
commit 6ca1ffce84
2 changed files with 59 additions and 2 deletions

View File

@ -32,7 +32,7 @@ import java.util.Map;
* @author Gary Gregory * @author Gary Gregory
* @author Norm Deane * @author Norm Deane
* @since 2.0 * @since 2.0
* @version $Id: ClassUtils.java,v 1.28 2004/02/24 06:01:28 ggregory Exp $ * @version $Id: ClassUtils.java,v 1.29 2004/06/27 04:42:54 bayard Exp $
*/ */
public class ClassUtils { public class ClassUtils {
@ -511,6 +511,31 @@ public class ClassUtils {
} }
return convertedClass; 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 // Inner class
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View File

@ -33,7 +33,7 @@ import junit.textui.TestRunner;
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Gary D. Gregory * @author Gary D. Gregory
* @version $Id: ClassUtilsTest.java,v 1.11 2004/02/18 23:22:29 ggregory Exp $ * @version $Id: ClassUtilsTest.java,v 1.12 2004/06/27 04:42:54 bayard Exp $
*/ */
public class ClassUtilsTest extends TestCase { public class ClassUtilsTest extends TestCase {
@ -375,6 +375,38 @@ public class ClassUtilsTest extends TestCase {
assertNull("null -> null", assertNull("null -> null",
ClassUtils.primitiveToWrapper(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() { public void testClassComparator() {
Comparator comparator = ClassUtils.CLASS_NAME_COMPARATOR; Comparator comparator = ClassUtils.CLASS_NAME_COMPARATOR;