37 |
package gnu.java.beans.decoder; |
package gnu.java.beans.decoder; |
38 |
|
|
39 |
import java.lang.reflect.Array; |
import java.lang.reflect.Array; |
|
import java.util.ArrayList; |
|
40 |
|
|
41 |
/** A Context implementation for a growable array. The array |
/** A Context implementation for a growable array. The array |
42 |
* elements have to be set using expressions. |
* elements have to be set using expressions. |
45 |
*/ |
*/ |
46 |
class GrowableArrayContext extends AbstractContext |
class GrowableArrayContext extends AbstractContext |
47 |
{ |
{ |
48 |
|
private static final int INITIAL_SIZE = 16; |
49 |
|
|
50 |
private Class klass; |
private Class klass; |
|
private ArrayList list = new ArrayList(); |
|
51 |
private Object array; |
private Object array; |
52 |
|
private int length; |
53 |
|
|
54 |
GrowableArrayContext(String id, Class newClass) |
GrowableArrayContext(String id, Class newClass) |
55 |
{ |
{ |
56 |
setId(id); |
setId(id); |
57 |
klass = newClass; |
klass = newClass; |
58 |
|
array = Array.newInstance(klass, INITIAL_SIZE); |
59 |
} |
} |
60 |
|
|
61 |
/* (non-Javadoc) |
/* (non-Javadoc) |
63 |
*/ |
*/ |
64 |
public void addParameterObject(Object o) throws AssemblyException |
public void addParameterObject(Object o) throws AssemblyException |
65 |
{ |
{ |
66 |
if (!klass.isInstance(o)) |
if (length == Array.getLength(array)) |
67 |
throw new AssemblyException( |
{ |
68 |
new IllegalArgumentException( |
Object tmp = Array.newInstance(klass, length * 2); |
69 |
"Cannot add object " |
System.arraycopy(array, 0, tmp, 0, length); |
70 |
+ o |
array = tmp; |
71 |
+ " to array where the elements are of class " |
} |
72 |
+ klass)); |
|
73 |
|
try { |
74 |
list.add(o); |
Array.set(array, length++, o); |
75 |
|
} catch(IllegalArgumentException iae) { |
76 |
|
throw new AssemblyException(iae); |
77 |
|
} |
78 |
} |
} |
79 |
|
|
80 |
/* (non-Javadoc) |
/* (non-Javadoc) |
91 |
*/ |
*/ |
92 |
public Object endContext(Context outerContext) throws AssemblyException |
public Object endContext(Context outerContext) throws AssemblyException |
93 |
{ |
{ |
94 |
if (array == null) |
if (length != Array.getLength(array)) |
95 |
{ |
{ |
96 |
array = Array.newInstance(klass, list.size()); |
Object tmp = Array.newInstance(klass, length); |
97 |
|
System.arraycopy(array, 0, tmp, 0, length); |
98 |
for (int i = 0; i < list.size(); i++) |
array = tmp; |
99 |
Array.set(array, i, list.get(i)); |
} |
100 |
} |
|
|
|
|
101 |
return array; |
return array; |
102 |
} |
} |
103 |
|
|
116 |
*/ |
*/ |
117 |
public void set(int index, Object o) throws AssemblyException |
public void set(int index, Object o) throws AssemblyException |
118 |
{ |
{ |
119 |
if (array == null) |
try { |
120 |
{ |
Array.set(array, index, o); |
121 |
if (klass.isInstance(o)) |
} catch(IllegalArgumentException iae) { |
122 |
list.add(index, o); |
throw new AssemblyException(iae); |
123 |
else |
} |
|
throw new AssemblyException( |
|
|
new IllegalArgumentException("Argument is not compatible to array component type.")); |
|
|
} |
|
|
else |
|
|
Array.set(array, index, o); |
|
124 |
} |
} |
125 |
|
|
126 |
/* (non-Javadoc) |
/* (non-Javadoc) |
128 |
*/ |
*/ |
129 |
public Object get(int index) throws AssemblyException |
public Object get(int index) throws AssemblyException |
130 |
{ |
{ |
131 |
if (array == null) |
return Array.get(array, index); |
|
return list.get(index); |
|
|
else |
|
|
return Array.get(array, index); |
|
132 |
} |
} |
133 |
|
|
134 |
public Object getResult() |
public Object getResult() |