View Javadoc

1   package uk.ac.ebi.intenz.webapp.utilities;
2   
3   import org.apache.log4j.Logger;
4   
5   import java.util.ArrayList;
6   
7   /**
8    * AutoGrowingList
9    *
10   * @author pmatos
11   * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:09 $
12   */
13  public class AutoGrowingList extends ArrayList {
14  
15    private Class clazz;
16    Logger logger = Logger.getRootLogger();
17  
18    public AutoGrowingList(Class clazz) {
19      super();
20      this.clazz = clazz;
21    }
22  
23    public Object get(int index) {
24      Object obj = null;
25      if (this.size() > index)
26        obj = super.get(index);
27  
28      if (obj == null) {
29        try {
30          obj = clazz.newInstance();
31        } catch (InstantiationException e) {
32          e.printStackTrace();
33        } catch (IllegalAccessException e) {
34          e.printStackTrace();
35        }
36        if (index > this.size()) {
37          int indexCounter = this.size();
38          while (indexCounter < index) {
39            try {
40              super.add(clazz.newInstance());
41              indexCounter++;
42            } catch (InstantiationException e) {
43              logger.error("Error instantiating object of type " + clazz.getName());
44            } catch (IllegalAccessException e) {
45              logger.error(("Error in illegal access of the class " + clazz.getName()));
46            }
47          }
48        }
49        super.add(obj);
50      }
51  
52      return obj;
53    }
54  
55    public Object set(int index, Object obj) {
56      if (this.size() > index)
57        super.set(index, obj);
58      else {
59        int indexCounter = this.size();
60        while (indexCounter < index) {
61          try {
62            super.add(clazz.newInstance());
63            indexCounter++;
64          } catch (InstantiationException e) {
65            logger.error("Error instantiating object of type " + clazz.getName());
66          } catch (IllegalAccessException e) {
67            logger.error(("Error in illegal access of the class " + clazz.getName()));
68          }
69        }
70        super.add(obj);
71      }
72      return obj;
73    }
74  }