Moved toClass from ArrayUtils to ClassUtils as per Joerg's comment in LANG-333

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@594551 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2007-11-13 14:35:33 +00:00
parent 70e7930e89
commit ecc7608faf
4 changed files with 40 additions and 41 deletions

View File

@ -4414,27 +4414,4 @@ public class ArrayUtils {
return result;
}
/**
* <p>Converts an array of <code>Object</code> in to an Array of <code>Class</code> objects.</p>
*
* <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
*
* @param array an <code>Object</code> array
* @return a <code>Class</code> array, <code>null</code> if null array input
* @since 2.4
*/
public static Class[] toClass(Object[] array)
{
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_CLASS_ARRAY;
}
Class[] classes = new Class[array.length];
for (int i = 0; i < array.length; i++) {
classes[i] = array[i].getClass();
}
return classes;
}
}

View File

@ -739,4 +739,26 @@ public class ClassUtils {
return className;
}
/**
* <p>Converts an array of <code>Object</code> in to an Array of <code>Class</code> objects.</p>
*
* <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
*
* @param array an <code>Object</code> array
* @return a <code>Class</code> array, <code>null</code> if null array input
* @since 2.4
*/
public static Class[] toClass(Object[] array)
{
if (array == null) {
return null;
} else if (array.length == 0) {
return ArrayUtils.EMPTY_CLASS_ARRAY;
}
Class[] classes = new Class[array.length];
for (int i = 0; i < array.length; i++) {
classes[i] = array[i].getClass();
}
return classes;
}
}

View File

@ -2420,24 +2420,6 @@ public class ArrayUtilsTest extends TestCase {
new double[] { Double.MIN_VALUE, Double.MAX_VALUE, 9999999 })));
}
public void testToClass_object() {
assertEquals(null, ArrayUtils.toClass(null));
assertSame(
ArrayUtils.EMPTY_CLASS_ARRAY,
ArrayUtils.toClass(new Class[0]));
Object[] array = new Object[3];
array[0] = new String("Test");
array[1] = new Integer(1);
array[2] = new Double(99);
Class[] results = ArrayUtils.toClass(array);
assertEquals("String", ClassUtils.getShortClassName(results[0]));
assertEquals("Integer", ClassUtils.getShortClassName(results[1]));
assertEquals("Double", ClassUtils.getShortClassName(results[2]));
}
//-----------------------------------------------------------------------
/**
* Test for {@link ArrayUtils#isEmpty(java.lang.Object[])}.

View File

@ -581,4 +581,22 @@ public class ClassUtilsTest extends TestCase {
assertEquals(Object.class.getMethod("toString", new Class[0]), toStringMethod);
}
public void testToClass_object() {
assertEquals(null, ClassUtils.toClass(null));
assertSame(
ArrayUtils.EMPTY_CLASS_ARRAY,
ClassUtils.toClass(new Class[0]));
Object[] array = new Object[3];
array[0] = new String("Test");
array[1] = new Integer(1);
array[2] = new Double(99);
Class[] results = ClassUtils.toClass(array);
assertEquals("String", ClassUtils.getShortClassName(results[0]));
assertEquals("Integer", ClassUtils.getShortClassName(results[1]));
assertEquals("Double", ClassUtils.getShortClassName(results[2]));
}
}