1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
24
25
26 public class PurApArrayList extends ArrayList {
27
28 private final Class listObjectType;
29 private final Class[] argumentClasses;
30 private final Object[] arguments;
31
32
33
34
35
36
37 public PurApArrayList(Class listObjectType) {
38 this(listObjectType, null, null);
39 }
40
41
42
43
44
45
46
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
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
95
96 public void add(int index, Object element) {
97 checkType(element);
98 super.add(index, element);
99 }
100
101
102
103
104 public boolean add(Object o) {
105 checkType(o);
106 return super.add(o);
107 }
108
109
110
111
112 public Object get(int index) {
113 growArray(index);
114 return super.get(index);
115 }
116
117
118
119
120 public Object set(int index, Object element) {
121 growArray(index);
122 return super.set(index, element);
123 }
124
125
126
127
128
129
130
131 private void growArray(int index) {
132 if (index < 0) {
133 throw new IndexOutOfBoundsException("Index must be positive.");
134 }
135
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
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 }