View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.util;
17  
18  import java.beans.Beans;
19  import java.lang.reflect.InvocationTargetException;
20  import java.util.ArrayList;
21  
22  /**
23   * This class is setup to allow an argument to a created ArrayList (it could be a
24   * possible extension to the other ArrayList
25   */
26  public class PurApArrayList extends ArrayList {
27      // private static final long serialVersionUID = 6238521951259126730L;
28      private final Class listObjectType;
29      private final Class[] argumentClasses;
30      private final Object[] arguments;
31  
32      /**
33       * Default Constructor
34       *
35       * @param listObjectType the class
36       */
37      public PurApArrayList(Class listObjectType) {
38          this(listObjectType, null, null);
39      }
40  
41      /**
42       * Constructor
43       *
44       * @param listObjectType  the object type
45       * @param methodClasses   classes
46       * @param methodArguments arguments
47       */
48      public PurApArrayList(Class listObjectType, Class[] methodClasses, Object[] methodArguments) {
49          super();
50  
51          Class[] assignArgumentClasses = null;
52          Object[] assignArguments = null;
53  
54          if (listObjectType == null) {
55              throw new RuntimeException("class type for list is required.");
56          }
57  
58          // attempt to get an instance of the class to check it has a visible default constructor
59          if (methodClasses == null && methodArguments == null) {
60              try {
61  
62                  Object listObj = listObjectType.newInstance();
63              } catch (InstantiationException e) {
64                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
65              } catch (IllegalAccessException e) {
66                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
67              }
68          } else {
69              try {
70                  listObjectType.getConstructor(methodClasses).newInstance(methodArguments);
71              } catch (SecurityException e) {
72                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
73              } catch (NoSuchMethodException e) {
74                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
75              } catch (IllegalArgumentException e) {
76                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
77              } catch (InstantiationException e) {
78                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
79              } catch (IllegalAccessException e) {
80                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
81              } catch (InvocationTargetException e) {
82                  throw new RuntimeException("unable to get instance of class" + listObjectType.getName());
83              }
84              assignArgumentClasses = methodClasses;
85              assignArguments = methodArguments;
86          }
87  
88          this.listObjectType = listObjectType;
89          this.argumentClasses = assignArgumentClasses;
90          this.arguments = assignArguments;
91      }
92  
93      /**
94       * @see java.util.List#add(int, java.lang.Object)
95       */
96      public void add(int index, Object element) {
97          checkType(element);
98          super.add(index, element);
99      }
100 
101     /**
102      * @see java.util.Collection#add(java.lang.Object)
103      */
104     public boolean add(Object o) {
105         checkType(o);
106         return super.add(o);
107     }
108 
109     /**
110      * @see java.util.List#get(int)
111      */
112     public Object get(int index) {
113         growArray(index);
114         return super.get(index);
115     }
116 
117     /**
118      * @see java.util.List#set(int, java.lang.Object)
119      */
120     public Object set(int index, Object element) {
121         growArray(index);
122         return super.set(index, element);
123     }
124 
125 
126     /**
127      * Adds new instances of type listObjectType to the arraylist until the size of the list is greater than the index required.
128      *
129      * @param index the index to grow to
130      */
131     private void growArray(int index) {
132         if (index < 0) {
133             throw new IndexOutOfBoundsException("Index must be positive.");
134         }
135         // ensureCapacity(index); // Increments modCount
136 
137         while (size() <= index) {
138             try {
139                 if (this.arguments == null && this.argumentClasses == null) {
140                     super.add(listObjectType.newInstance());
141                 } else {
142                     super.add(listObjectType.getConstructor(argumentClasses).newInstance(arguments));
143                 }
144             } catch (InstantiationException e) {
145                 throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName());
146             } catch (IllegalAccessException e) {
147                 throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName());
148             } catch (IllegalArgumentException e) {
149                 throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName());
150             } catch (SecurityException e) {
151                 throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName());
152             } catch (InvocationTargetException e) {
153                 throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName());
154             } catch (NoSuchMethodException e) {
155                 throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName());
156             }
157         }
158     }
159 
160 
161     /**
162      * Checks the type of an element matches the underlying list type.
163      */
164     private void checkType(Object element) {
165         if (element != null) {
166             if (!Beans.isInstanceOf(element, listObjectType)) {
167                 throw new RuntimeException("element is not of correct type.");
168             }
169         }
170     }
171 
172     public Class getListObjectType() {
173         return listObjectType;
174     }
175 
176 }