I have dug it out from my old project.

The code is not very beautiful, you can modify it if you wish,
but please don't change the author information.

Here is the code:

========================================

package com.ewsoft.common.utils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

/**
 * A List class which can create items as need.<br />
 * When method get(index) and add(index, element) is called,<br />
 * and if there is no item in this index, item instances will be created
 * automatically to avoid a NullPointerException.
 *
 * @author Li Ying(liying.cn.2...@gmail.com)
 */
public class AutoList<E extends Object> extends ArrayList<E> {
        private Class<E> elementClass;

        public AutoList(final Class<E> elementClass) {
                super();
                this.elementClass = elementClass;
        }

        private void ensureSize(final int size) {
                synchronized (this) {
                        while (this.size() < size) {
                                add(createNewElement());
                        }
                }
        }

        private E createElementByConstructor() {
                try {
                        Constructor<E>[] constrArray = (Constructor<E>[]) 
elementClass
                                        .getConstructors();

                        Constructor<E> constructor = null;
                        Class[] paramTypes = null;

                        for (Constructor<E> constr : constrArray) {
                                if (constructor == null) {
                                        constructor = constr;
                                        paramTypes = constr.getParameterTypes();
                                } else if (constr.getParameterTypes().length < 
paramTypes.length) {
                                        constructor = constr;
                                        paramTypes = constr.getParameterTypes();
                                }
                        }

                        List paramList = new ArrayList();
                        for (int i = 0; i < paramTypes.length; i++) {
                                paramList.add(null);
                        }

                        return constructor.newInstance(paramList.toArray());
                } catch (Exception e) {
                        return null;
                }
        }

        private E createNewElement() {
                return createElementByConstructor();
        }

        /*
         * (non-Javadoc)
         *
         * @see java.util.ArrayList#get(int)
         */
        @Override
        public final E get(final int index) {
                ensureSize(index + 1);

                return super.get(index);
        }

        /*
         * (non-Javadoc)
         *
         * @see java.util.ArrayList#add(int, java.lang.Object)
         */
        @Override
        public final void add(final int index, final E element) {
                ensureSize(index);

                super.add(index, element);
        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to