commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From morten.simon...@Storebrand.no
Subject Re: Suggestion to imrovement of org.apache.commons.lang.ArrayUtils
Date Tue, 06 Jul 2004 19:45:22 GMT
It seems to me like my attachment in my previous mail never got through. 
It's probably obvious to you who are experienced users of this 
mail-system, but
I was not aware of it. Nevertheless, here's my code, and I hope it won't 
be to
much trouble importing the code into your IDE. Suggestions on how this
is done properly (mailing source code) is welcome

########################################################

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/*
 * This class is able to convert (to some extend) and manipulate
 * arrays. The point is that working with arrays can be somewhat
 * tedious, and this class will help you! T
 * 
 */

public class ArrayConverter {
        private static Map stringMap;

        public static void main(String[] args) {

                cast(stringMap.values().toArray(), String.class)
                // String[] inputArray = new String[] { "124929353554", 
"22494929942424" };
                // Integer[] inputArray = new Integer[] { new Integer(1), 
new Integer(2)};
                int[] inputArray = new int[] { 1, 2 };
                //long[] inputArray = new long[] { 1, 2 };

                Class inputType = 
inputArray.getClass().getComponentType();
                System.out.println("Type of input-array is " + inputType);

                Object returnObject = cast(inputArray, Integer.class);

                Class returnType = 
returnObject.getClass().getComponentType();
                System.out.println("Type of return-array is " + 
returnType);
                int length = 0;
                if (returnType.isPrimitive()) {
                        length = primitiveArrayLength(returnObject);
                }
                else {
                        length = ((Object[]) returnObject).length;
                }
                System.out.println("The size of the return-array is " + 
length);
                for (int index = 0; index < length; index++) {
                        Object indexObject = Array.get(returnObject, 
index);
                        System.out.println("Return-array[" + index + "]=" 
+ indexObject);
                }

        }

        /*
         * The method can cast an array from one type to another. One
         * typical case is from Object[] to String[]. This wil work
         * within limits. You can probably cast from almost any object
         * to String, but then your objects should (in most cases) 
implement 
         * toString() correctly, at least if you want the casting to make
         * sense. You can cast from any kind of object to every
         * other kind of object, if you have a correct constructor
         * in the object to casting to. Casting between primitives
         * and primitive wrappers (back and forth) should be allright.
         * Some casts are impossible (Map[] --> List[]), because we
         * can't write a constructor (List(Map map)) but that's obvious. 
         * 
         * The exceptions are written to standard out. This should
         * be changed in the future.
         * 
         */
        public static Object cast(Object array, Class type) {
                if (array == null)
                        return null;
                else {
                        int arrayLength = getArrayLength(array);
                        Object noA = Array.newInstance(type, arrayLength);

                        for (int i = 0; i < arrayLength; i++) {
                                Object objectI = Array.get(array, i);
                                Class objectClass = objectI.getClass();
                                try {
                                        Array.set(noA, i, objectI);
                                }
                                catch (Exception e) {
                                        try {
                                                type = 
convertToWrapperIfNecessary(type);
                                                Constructor constructor = 
type.getConstructor(new Class[] { objectClass });
                                                Object objectC = 
constructor.newInstance(new Object[] { objectI });
                                                Array.set(noA, i, 
objectC);
                                        }
                                        catch (NoSuchMethodException nsme) 
{
                                                if (type == String.class) 
{
                                                        Array.set(noA, i, 
"" + objectI);
                                                }
                                                else {
 System.out.println("Casting went wrong because " + type + "(" + 
objectClass.getName() + ")-constructor was not found");
                                                }
                                        }
                                        catch (InvocationTargetException 
ite) {
 System.out.println("Something failed while invoking " + type + "(" + 
objectClass.getName() + ")-constructor");
                                        }
                                        catch (IllegalAccessException iae) 
{
                                                System.out.println("Not 
allowed to run " + type + "(" + objectClass.getName() + ")-constructor");
                                        }
                                        catch (InstantiationException ie) 
{
                                                System.out.println("Didn't 
allow to instantiate " + type + "(" + objectClass.getName() + 
")-constructor");
                                        }
                                }

                        }
                        return noA;
                }
        }

        private static Class convertToWrapperIfNecessary(Class 
objectClass) {
                if (objectClass.isPrimitive()) {
                        if (objectClass == int.class)
                                return Integer.class;
                        else if (objectClass == long.class)
                                return Long.class;
                        else if (objectClass == float.class)
                                return Float.class;
                        else if (objectClass == double.class)
                                return Double.class;
                        else if (objectClass == boolean.class)
                                return Boolean.class;
                        else if (objectClass == short.class)
                                return Short.class;
                        else if (objectClass == char.class)
                                return Character.class;
                        else //if (objectClass == byte.class)
                                return Byte.class;
                }
                else {
                        return objectClass;
                }

        }

        private static int getArrayLength(Object array) {
                Class arrayClass = array.getClass().getComponentType();
                int arrayLength = 0;
                if (arrayClass.isPrimitive())
                        arrayLength = primitiveArrayLength(array);
                else
                        arrayLength = ((Object[]) array).length;
                return arrayLength;
        }

        private static int primitiveArrayLength(Object o) {
                if (o.getClass().getComponentType() == Integer.TYPE) {
                        return ((int[]) o).length;
                }
                if (o.getClass().getComponentType() == Long.TYPE) {
                        return ((long[]) o).length;
                }
                if (o.getClass().getComponentType() == Float.TYPE) {
                        return ((float[]) o).length;
                }
                if (o.getClass().getComponentType() == Double.TYPE) {
                        return ((double[]) o).length;
                }
                if (o.getClass().getComponentType() == Short.TYPE) {
                        return ((short[]) o).length;
                }
                if (o.getClass().getComponentType() == Byte.TYPE) {
                        return ((byte[]) o).length;
                }
                if (o.getClass().getComponentType() == Character.TYPE) {
                        return ((char[]) o).length;
                }
                if (o.getClass().getComponentType() == Boolean.TYPE) {
                        return ((boolean[]) o).length;
                }
                return 0;
        }

        /*
         * The method is just a wrapper around System.arraycopy, because 
it
         * is to complicated to use that method directly (that's what I 
think!)
         */
        public static Object addArrays(Object array1, Object array2) {
                if (array1 == null)
                        return array2;
                else if (array2 == null)
                        return array1;
                else if 
(!array1.getClass().getComponentType().equals(array2.getClass().getComponentType()))
                        throw new IllegalArgumentException("The first 
array-argument is not of the same type as the second array-argument");
                else {
                        Class arrayClass = 
array1.getClass().getComponentType();
                        int array1Length = getArrayLength(array1);
                        int array2Length = getArrayLength(array2);
                        Object nyArray = Array.newInstance(arrayClass, 
array1Length + array2Length);
                        System.arraycopy(array1, 0, nyArray, 0, 
array1Length);
                        System.arraycopy(array2, 0, nyArray, array1Length, 
array2Length);
                        return nyArray;
                }
        }

        /*
         * Converts from an array to an ArrayList-object.
         */
        public static List toArrayList(Object array) {
                if (array == null)
                        return null;
                else {
                        List list = new ArrayList();
                        for (int i = 0; i < getArrayLength(array); i++) {
                                list.add(Array.get(array, i));
                        }
                        return list;
                }
        }

        /*
         * Converts from an array to an Vector-object. (ThreadSafe)
         */
        public static List toVector(Object array) {
                if (array == null)
                        return null;
                else {
                        List list = new Vector();
                        for (int i = 0; i < getArrayLength(array); i++) {
                                list.add(Array.get(array, i));
                        }
                        return list;
                }
        }

        /*
         * Converts from a list to an array. Uses the cast-method
         * in this method, so all the limitations of that method
         * also applies here.
         */
        public static Object toArray(List list, Class type) {
                Object[] objArray = list.toArray();
                return cast(objArray, type);
        }

}

Morten Simonsen
Mime
View raw message