| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.util; |
| 17 | |
|
| 18 | |
import java.beans.Beans; |
| 19 | |
import java.lang.reflect.Constructor; |
| 20 | |
import java.lang.reflect.Modifier; |
| 21 | |
import java.util.ArrayList; |
| 22 | |
|
| 23 | |
import org.apache.commons.lang.ArrayUtils; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public class TypedArrayList extends ArrayList { |
| 31 | |
private static final long serialVersionUID = 6238521951259126730L; |
| 32 | |
private final Class listObjectType; |
| 33 | |
|
| 34 | |
|
| 35 | |
public TypedArrayList(Class listObjectType) { |
| 36 | 0 | super(); |
| 37 | |
|
| 38 | 0 | if (listObjectType == null) { |
| 39 | 0 | throw new RuntimeException("class type for list is required."); |
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | boolean foundDefaultConstructor = false; |
| 45 | |
|
| 46 | 0 | Constructor[] constructors = listObjectType.getConstructors(); |
| 47 | 0 | for (Constructor constructor : constructors) { |
| 48 | 0 | Class[] parameterType = constructor.getParameterTypes(); |
| 49 | |
|
| 50 | |
|
| 51 | 0 | if(ArrayUtils.isEmpty(parameterType) && |
| 52 | |
Modifier.isPublic(constructor.getModifiers())) { |
| 53 | 0 | foundDefaultConstructor = true; |
| 54 | 0 | break; |
| 55 | |
} |
| 56 | |
} |
| 57 | |
|
| 58 | 0 | if(!foundDefaultConstructor) { |
| 59 | 0 | throw new RuntimeException("unable to get default constructor for " + listObjectType.getName()); |
| 60 | |
} |
| 61 | 0 | this.listObjectType = listObjectType; |
| 62 | |
|
| 63 | 0 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public void add(int index, Object element) { |
| 69 | 0 | checkType(element); |
| 70 | 0 | super.add(index, element); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public boolean add(Object o) { |
| 77 | 0 | checkType(o); |
| 78 | 0 | return super.add(o); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public Object get(int index) { |
| 85 | 0 | growArray(index); |
| 86 | 0 | return super.get(index); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public Object set(int index, Object element) { |
| 93 | 0 | growArray(index); |
| 94 | 0 | return super.set(index, element); |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
private void growArray(int index) { |
| 102 | 0 | if (index < 0) { |
| 103 | 0 | throw new IndexOutOfBoundsException("Index must be positive."); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | 0 | while (size() <= index) { |
| 108 | |
try { |
| 109 | 0 | super.add(listObjectType.newInstance()); |
| 110 | |
} |
| 111 | 0 | catch (InstantiationException e) { |
| 112 | 0 | throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName()); |
| 113 | |
} |
| 114 | 0 | catch (IllegalAccessException e) { |
| 115 | 0 | throw new RuntimeException("Cannot get new instance of class " + listObjectType.getName()); |
| 116 | 0 | } |
| 117 | |
} |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
private void checkType(Object element) { |
| 125 | 0 | if (element != null) { |
| 126 | 0 | if (!Beans.isInstanceOf(element, listObjectType)) { |
| 127 | 0 | throw new RuntimeException("element is not of correct type."); |
| 128 | |
} |
| 129 | |
} |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public Class getListObjectType() { |
| 136 | 0 | return listObjectType; |
| 137 | |
} |
| 138 | |
|
| 139 | |
} |