1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57:
58: import ;
59: import ;
60:
61:
67: public class gnuDynArray
68: extends DivideableAny
69: implements DynArray, Serializable
70: {
71:
74: private static final long serialVersionUID = 1;
75:
76:
79: final TypeCode official_components;
80:
81:
84: final TypeCode final_components;
85:
86:
98: public gnuDynArray(TypeCode oType, TypeCode aType, gnuDynAnyFactory aFactory,
99: ORB anOrb, boolean initialise_array
100: )
101: throws BAD_PARAM
102: {
103: super(oType, aType, aFactory, anOrb);
104:
105: try
106: {
107: official_components = final_type.content_type();
108:
109: TypeCode component = official_components;
110: while (component.kind().value() == TCKind._tk_alias)
111: component = component.content_type();
112: final_components = component;
113:
114: if (initialise_array)
115: {
116: array = new DynAny[ aType.length() ];
117: for (int i = 0; i < array.length; i++)
118: {
119: array [ i ] =
120: factory.create_dyn_any_from_type_code(official_components);
121: }
122: }
123: }
124: catch (Exception e)
125: {
126: BAD_PARAM bad = new BAD_PARAM("Unable to initialise array");
127: bad.initCause(e);
128: throw bad;
129: }
130: }
131:
132:
135: public void assign(DynAny from)
136: throws TypeMismatch
137: {
138: checkType(official_type, from.type());
139: if (from instanceof DynArray && from.component_count() == array.length)
140: {
141: DynArray dyn = (DynArray) from;
142: array = dyn.get_elements_as_dyn_any();
143: }
144: else
145: throw new TypeMismatch();
146: }
147:
148:
151: public DynAny copy()
152: {
153: DynAny[] c = new DynAny[ array.length ];
154: for (int i = 0; i < c.length; i++)
155: {
156: c [ i ] = array [ i ].copy();
157: }
158:
159: gnuDynArray d =
160: new gnuDynArray(official_type, final_type, factory, orb, false);
161: d.array = c;
162: return d;
163: }
164:
165:
168: public Any[] get_elements()
169: {
170: Any[] r = new Any[ array.length ];
171: for (int i = 0; i < r.length; i++)
172: r [ i ] = array [ i ].to_any();
173: return r;
174: }
175:
176:
177: public DynAny[] get_elements_as_dyn_any()
178: {
179: DynAny[] a = new DynAny[ array.length ];
180: for (int i = 0; i < a.length; i++)
181: {
182: a [ i ] = array [ i ].copy();
183: }
184: return a;
185: }
186:
187:
191: public void set_elements_as_dyn_any(DynAny[] value)
192: throws InvalidValue, TypeMismatch
193: {
194: if (value.length != array.length)
195: throw new InvalidValue(sizeMismatch(array.length, value.length));
196: for (int i = 0; i < value.length; i++)
197: {
198: checkType(official_components, value [ i ].type());
199: array [ i ].assign(value [ i ]);
200: }
201: pos = 0;
202: valueChanged();
203: }
204:
205:
208: public void set_elements(Any[] value)
209: throws InvalidValue, TypeMismatch
210: {
211: if (value.length != array.length)
212: throw new InvalidValue(sizeMismatch(array.length, value.length));
213:
214: for (int i = 0; i < value.length; i++)
215: {
216: checkType(official_components, value [ i ].type());
217: try
218: {
219: array [ i ] = factory.create_dyn_any(value [ i ]);
220: }
221: catch (InconsistentTypeCode e)
222: {
223: TypeMismatch t = new TypeMismatch();
224: t.initCause(e);
225: throw t;
226: }
227: }
228: pos = 0;
229: valueChanged();
230: }
231:
232:
235: public Any to_any()
236: {
237: try
238: {
239: Streamable memberHolder =
240: HolderLocator.createHolder(official_components);
241:
242: if (memberHolder == null)
243: memberHolder = HolderLocator.createHolder(final_components);
244:
245: Class memberHolderClass = memberHolder.getClass();
246: Class memberClass = memberHolderClass.getField("value").getType();
247:
248: Object members = Array.newInstance(memberClass, array.length);
249: Object member;
250: Any am;
251: Field value = memberHolder.getClass().getField("value");
252:
253: for (int i = 0; i < array.length; i++)
254: {
255:
256: am = array [ i ].to_any();
257: memberHolder = am.extract_Streamable();
258: member = value.get(memberHolder);
259: Array.set(members, i, member);
260: }
261:
262: Streamable arrayHolder = HolderLocator.createHolder(official_type);
263: arrayHolder.getClass().getField("value").set(arrayHolder, members);
264:
265: Any g = createAny();
266: g.insert_Streamable(arrayHolder);
267: g.type(official_type);
268: return g;
269: }
270: catch (Exception e)
271: {
272: throw new Unexpected(e);
273: }
274: }
275:
276:
279: public void from_any(Any an_any)
280: throws TypeMismatch, InvalidValue
281: {
282: checkType(official_type, an_any.type());
283: try
284: {
285: Streamable s = an_any.extract_Streamable();
286: Object members = s.getClass().getField("value").get(s);
287:
288: checkArrayValid(members);
289:
290: Any member;
291: Streamable holder;
292: Class holderClass = null;
293:
294: for (int i = 0; i < array.length; i++)
295: {
296: if (holderClass == null)
297: {
298: holder = HolderLocator.createHolder(official_components);
299: if (holder == null)
300: holder = HolderLocator.createHolder(final_components);
301: holderClass = holder.getClass();
302: }
303: else
304: holder = (Streamable) holderClass.newInstance();
305:
306: member = createAny();
307: holder.getClass().getField("value").set(holder,
308: Array.get(members, i)
309: );
310: member.insert_Streamable(holder);
311: member.type(official_components);
312:
313:
314:
315: array [ i ].from_any(member);
316: }
317: }
318: catch (Exception ex)
319: {
320: TypeMismatch t = new TypeMismatch();
321: t.initCause(ex);
322: throw t;
323: }
324: valueChanged();
325: }
326:
327:
331: protected void checkArrayValid(Object members)
332: throws TypeMismatch, InvalidValue
333: {
334: if (array.length != Array.getLength(members))
335: throw new InvalidValue(sizeMismatch(array.length, Array.getLength(members)));
336: }
337: }