1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
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:
64: public class gnuDynUnion
65: extends DivideableAny
66: implements DynUnion, Serializable, ValueChangeListener
67: {
68:
71: private static final long serialVersionUID = 1;
72:
73:
76: DynAny discriminator;
77:
78:
81: static String NOAM = "No active member";
82:
83:
88: public gnuDynUnion(TypeCode oType, TypeCode aType, gnuDynAnyFactory aFactory,
89: ORB anOrb
90: )
91: throws InconsistentTypeCode
92: {
93: super(oType, aType, aFactory, anOrb);
94: try
95: {
96: discriminator =
97: factory.create_dyn_any_from_type_code(final_type.discriminator_type());
98:
99: ((AbstractAny) discriminator).listener = this;
100:
101: if (final_type.default_index() >= 0)
102: set_to_default_member();
103: else
104: set_to_no_active_member();
105: }
106: catch (Exception ex)
107: {
108: InconsistentTypeCode inc = new InconsistentTypeCode("discriminator");
109: inc.initCause(ex);
110: throw inc;
111: }
112: }
113:
114:
119: public Any to_any()
120: {
121: Any a = createAny();
122: OutputStream ou = a.create_output_stream();
123: discriminator.to_any().write_value(ou);
124: if (array.length == 2)
125: array [ 1 ].to_any().write_value(ou);
126: a.read_value(ou.create_input_stream(), final_type);
127: return a;
128: }
129:
130:
133: public void assign(DynAny from)
134: throws TypeMismatch
135: {
136: checkType(official_type, from.type());
137: if (!(from instanceof DynUnion))
138: throw new TypeMismatch("DynUnion required");
139: else
140: {
141: try
142: {
143: DynUnion u = (DynUnion) from;
144: discriminator.assign(u.get_discriminator());
145: if (u.has_no_active_member())
146: {
147: if (array.length != 1)
148: array = new DynAny[] { discriminator };
149: }
150: else
151: {
152: if (array.length != 2)
153: array = new DynAny[] { discriminator, u.member().copy() };
154: else
155: array [ 1 ] = u.member().copy();
156: }
157: }
158: catch (InvalidValue e)
159: {
160: throw new Unexpected(e);
161: }
162: }
163: valueChanged();
164: }
165:
166:
167: public DynAny copy()
168: {
169: try
170: {
171: gnuDynUnion other =
172: new gnuDynUnion(official_type, final_type, factory, orb);
173: other.discriminator = discriminator.copy();
174: ((AbstractAny) other.discriminator).listener = other;
175: if (array.length == 1)
176: {
177: other.array = new DynAny[] { other.discriminator };
178: }
179: else
180: {
181: other.array =
182: new DynAny[] { other.discriminator, array [ 1 ].copy() };
183: }
184: return other;
185: }
186: catch (InconsistentTypeCode ex)
187: {
188: throw new Unexpected(ex);
189: }
190: }
191:
192:
195: public void from_any(Any an_any)
196: throws TypeMismatch, InvalidValue
197: {
198: checkType(official_type, an_any.type());
199:
200: Any adis = createAny();
201: try
202: {
203: InputStream stream = an_any.create_input_stream();
204: adis.read_value(stream, final_type.discriminator_type());
205:
206: DynAny nd = factory.create_dyn_any(adis);
207:
208: set_discriminator(nd);
209: if (array.length == 2)
210: {
211:
212: adis.read_value(stream, array [ 1 ].type());
213: array [ 1 ].from_any(adis);
214: }
215: }
216: catch (InconsistentTypeCode it)
217: {
218: TypeMismatch t = new TypeMismatch();
219: t.initCause(it);
220: throw t;
221: }
222: catch (MARSHAL m)
223: {
224: InvalidValue t = new InvalidValue();
225: t.initCause(m);
226: throw t;
227: }
228: catch (BadKind b)
229: {
230: throw new Unexpected(b);
231: }
232: valueChanged();
233: }
234:
235:
236: public TCKind discriminator_kind()
237: {
238: return discriminator.type().kind();
239: }
240:
241:
242: public DynAny get_discriminator()
243: {
244: return discriminator;
245: }
246:
247:
248: public boolean has_no_active_member()
249: {
250: return array.length == 1;
251: }
252:
253:
254: public TCKind member_kind()
255: throws InvalidValue
256: {
257: return member().type().kind();
258: }
259:
260:
263: public String member_name()
264: throws InvalidValue
265: {
266: if (array.length == 1)
267: throw new InvalidValue(NOAM);
268: try
269: {
270: Any da = discriminator.to_any();
271:
272:
273:
274: for (int i = 0; i < final_type.member_count(); i++)
275: {
276: if (final_type.member_label(i).equal(da))
277: return final_type.member_name(i);
278: }
279: throw new InvalidValue(NOAM);
280: }
281: catch (Exception e)
282: {
283: InvalidValue t = new InvalidValue("Err");
284: t.initCause(e);
285: throw t;
286: }
287: }
288:
289:
290: public DynAny member()
291: throws InvalidValue
292: {
293: if (array.length < 2)
294: throw new InvalidValue(NOAM);
295: else
296: return array [ 1 ];
297: }
298:
299:
302: public void set_discriminator(DynAny aDiscriminator)
303: throws TypeMismatch
304: {
305: try
306: {
307: if (!aDiscriminator.type().equal(final_type.discriminator_type()))
308: throw new TypeMismatch("Wrong discriminator final_type for " +
309: final_type.name()
310: );
311:
312:
313:
314: if (!discriminator.equal(aDiscriminator))
315: {
316: discriminator.assign(aDiscriminator);
317: updateMember();
318: }
319: else
320: {
321: pos = array.length == 2 ? 1 : 0;
322: }
323: }
324: catch (Exception e)
325: {
326: TypeMismatch t = new TypeMismatch();
327: t.initCause(e);
328: throw t;
329: }
330: }
331:
332:
335: public void set_to_default_member()
336: throws TypeMismatch
337: {
338: try
339: {
340: int di = final_type.default_index();
341: if (di < 0)
342: throw new TypeMismatch("Union " + final_type.name() +
343: "has no default index"
344: );
345:
346: Any da = final_type.member_label(di);
347: discriminator.from_any(da);
348: updateMember();
349: }
350: catch (TypeMismatch m)
351: {
352:
353: throw m;
354: }
355: catch (Exception e)
356: {
357: TypeMismatch t = new TypeMismatch();
358: t.initCause(e);
359: throw t;
360: }
361: }
362:
363:
364: public void set_to_no_active_member()
365: throws TypeMismatch
366: {
367: try
368: {
369: if (final_type.default_index() >= 0)
370: {
371: throw new TypeMismatch("Explicit default case defined.");
372: }
373: }
374: catch (BadKind ex)
375: {
376:
377: }
378: array = new DynAny[] { discriminator };
379: valueChanged();
380: }
381:
382:
385: public void updateMember()
386: throws TypeMismatch
387: {
388: try
389: {
390: Any da = discriminator.to_any();
391:
392:
393:
394: for (int i = 0; i < final_type.member_count(); i++)
395: {
396: if (final_type.member_label(i).equal(da))
397: {
398: array =
399: new DynAny[]
400: {
401: discriminator,
402: factory.create_dyn_any_from_type_code(final_type.member_type(i))
403: };
404: pos = 1;
405: valueChanged();
406: return;
407: }
408: }
409: }
410: catch (Exception e)
411: {
412: TypeMismatch t = new TypeMismatch();
413: t.initCause(e);
414: throw t;
415: }
416:
417:
418: array = new DynAny[] { discriminator };
419: pos = 0;
420: valueChanged();
421: }
422:
423:
426: public void changed()
427: {
428: try
429: {
430: updateMember();
431: }
432: catch (TypeMismatch ex)
433: {
434: throw new Unexpected(ex);
435: }
436: }
437: }