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