KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > dynamicany > DynUnionImpl


1 /*
2  * @(#)DynUnionImpl.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.dynamicany;
9
10 import org.omg.CORBA.TypeCode JavaDoc;
11 import org.omg.CORBA.TCKind JavaDoc;
12 import org.omg.CORBA.Any JavaDoc;
13 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
14 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
15 import org.omg.CORBA.portable.InputStream JavaDoc;
16 import org.omg.DynamicAny.*;
17 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
18 import org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
19 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc;
20
21 import com.sun.corba.se.spi.orb.ORB ;
22 import com.sun.corba.se.spi.logging.CORBALogDomains ;
23 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
24
25 public class DynUnionImpl extends DynAnyConstructedImpl implements DynUnion
26 {
27     //
28
// Instance variables
29
//
30

31     DynAny discriminator = null;
32     // index either points to the discriminator or the named member is it exists.
33
// The currently active member, which is of the same type as the discriminator.
34
DynAny currentMember = null;
35     int currentMemberIndex = NO_INDEX;
36
37     //
38
// Constructors
39
//
40

41     private DynUnionImpl() {
42         this(null, (Any JavaDoc)null, false);
43     }
44
45     protected DynUnionImpl(ORB orb, Any JavaDoc any, boolean copyValue) {
46         // We can be sure that typeCode is of kind tk_union
47
super(orb, any, copyValue);
48     }
49
50     protected DynUnionImpl(ORB orb, TypeCode JavaDoc typeCode) {
51         // We can be sure that typeCode is of kind tk_union
52
super(orb, typeCode);
53     }
54
55     protected boolean initializeComponentsFromAny() {
56         try {
57             InputStream JavaDoc input = any.create_input_stream();
58             Any JavaDoc discriminatorAny = DynAnyUtil.extractAnyFromStream(discriminatorType(), input, orb);
59             discriminator = DynAnyUtil.createMostDerivedDynAny(discriminatorAny, orb, false);
60             currentMemberIndex = currentUnionMemberIndex(discriminatorAny);
61             Any JavaDoc memberAny = DynAnyUtil.extractAnyFromStream(memberType(currentMemberIndex), input, orb);
62             currentMember = DynAnyUtil.createMostDerivedDynAny(memberAny, orb, false);
63             components = new DynAny[] {discriminator, currentMember};
64         } catch (InconsistentTypeCode JavaDoc ictc) { // impossible
65
}
66         return true;
67     }
68
69     // Sets the current position to zero.
70
// The discriminator value is set to a value consistent with the first named member
71
// of the union. That member is activated and (recursively) initialized to its default value.
72
protected boolean initializeComponentsFromTypeCode() {
73         //System.out.println(this + " initializeComponentsFromTypeCode");
74
try {
75             // We can be sure that memberCount() > 0 according to the IDL language spec
76
discriminator = DynAnyUtil.createMostDerivedDynAny(memberLabel(0), orb, false);
77             index = 0;
78             currentMemberIndex = 0;
79             currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(0), orb);
80             components = new DynAny[] {discriminator, currentMember};
81         } catch (InconsistentTypeCode JavaDoc ictc) { // impossible
82
}
83         return true;
84     }
85
86     //
87
// Convenience methods
88
//
89

90     private TypeCode JavaDoc discriminatorType() {
91         TypeCode JavaDoc discriminatorType = null;
92         try {
93             discriminatorType = any.type().discriminator_type();
94         } catch (BadKind JavaDoc bad) {
95         }
96         return discriminatorType;
97     }
98
99     private int memberCount() {
100         int memberCount = 0;
101         try {
102             memberCount = any.type().member_count();
103         } catch (BadKind JavaDoc bad) {
104         }
105         return memberCount;
106     }
107
108     private Any JavaDoc memberLabel(int i) {
109         Any JavaDoc memberLabel = null;
110         try {
111             memberLabel = any.type().member_label(i);
112         } catch (BadKind JavaDoc bad) {
113         } catch (Bounds JavaDoc bounds) {
114         }
115         return memberLabel;
116     }
117
118     private TypeCode JavaDoc memberType(int i) {
119         TypeCode JavaDoc memberType = null;
120         try {
121             memberType = any.type().member_type(i);
122         } catch (BadKind JavaDoc bad) {
123         } catch (Bounds JavaDoc bounds) {
124         }
125         return memberType;
126     }
127
128     private String JavaDoc memberName(int i) {
129         String JavaDoc memberName = null;
130         try {
131             memberName = any.type().member_name(i);
132         } catch (BadKind JavaDoc bad) {
133         } catch (Bounds JavaDoc bounds) {
134         }
135         return memberName;
136     }
137
138     private int defaultIndex() {
139         int defaultIndex = -1;
140         try {
141             defaultIndex = any.type().default_index();
142         } catch (BadKind JavaDoc bad) {
143         }
144         return defaultIndex;
145     }
146
147     private int currentUnionMemberIndex(Any JavaDoc discriminatorValue) {
148         int memberCount = memberCount();
149         Any JavaDoc memberLabel;
150         for (int i=0; i<memberCount; i++) {
151             memberLabel = memberLabel(i);
152             if (memberLabel.equal(discriminatorValue)) {
153                 return i;
154             }
155         }
156         if (defaultIndex() != -1) {
157             return defaultIndex();
158         }
159         return NO_INDEX;
160     }
161
162     protected void clearData() {
163         super.clearData();
164         discriminator = null;
165         // Necessary to guarantee OBJECT_NOT_EXIST in member()
166
currentMember.destroy();
167         currentMember = null;
168         currentMemberIndex = NO_INDEX;
169     }
170
171     //
172
// DynAny interface methods
173
//
174

175     // _REVISIT_ More efficient copy operation
176

177     //
178
// DynUnion interface methods
179
//
180

181     /**
182     * Returns the current discriminator value.
183     */

184     public org.omg.DynamicAny.DynAny JavaDoc get_discriminator () {
185         if (status == STATUS_DESTROYED) {
186         throw wrapper.dynAnyDestroyed() ;
187         }
188         return (checkInitComponents() ? discriminator : null);
189     }
190
191     // Sets the discriminator of the DynUnion to the specified value.
192
// If the TypeCode of the parameter is not equivalent
193
// to the TypeCode of the unions discriminator, the operation raises TypeMismatch.
194
//
195
// Setting the discriminator to a value that is consistent with the currently
196
// active union member does not affect the currently active member.
197
// Setting the discriminator to a value that is inconsistent with the currently
198
// active member deactivates the member and activates the member that is consistent
199
// with the new discriminator value (if there is a member for that value)
200
// by initializing the member to its default value.
201
//
202
// If the discriminator value indicates a non-existent union member
203
// this operation sets the current position to 0
204
// (has_no_active_member returns true in this case).
205
// Otherwise the current position is set to 1 (has_no_active_member returns false and
206
// component_count returns 2 in this case).
207
public void set_discriminator (org.omg.DynamicAny.DynAny JavaDoc newDiscriminator)
208         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc
209     {
210         if (status == STATUS_DESTROYED) {
211         throw wrapper.dynAnyDestroyed() ;
212         }
213         if ( ! newDiscriminator.type().equal(discriminatorType())) {
214             throw new TypeMismatch JavaDoc();
215         }
216         newDiscriminator = DynAnyUtil.convertToNative(newDiscriminator, orb);
217         Any JavaDoc newDiscriminatorAny = getAny(newDiscriminator);
218         int newCurrentMemberIndex = currentUnionMemberIndex(newDiscriminatorAny);
219         if (newCurrentMemberIndex == NO_INDEX) {
220             clearData();
221             index = 0;
222         } else {
223             // _REVISIT_ Could possibly optimize here if we don't need to initialize components
224
checkInitComponents();
225             if (currentMemberIndex == NO_INDEX || newCurrentMemberIndex != currentMemberIndex) {
226                 clearData();
227                 index = 1;
228                 currentMemberIndex = newCurrentMemberIndex;
229                 try {
230                 currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(currentMemberIndex), orb);
231                 } catch (InconsistentTypeCode JavaDoc ictc) {}
232                 discriminator = newDiscriminator;
233                 components = new DynAny[] { discriminator, currentMember };
234                 representations = REPRESENTATION_COMPONENTS;
235             }
236         }
237     }
238
239     // Sets the discriminator to a value that is consistent with the value
240
// of the default case of a union; it sets the current position to
241
// zero and causes component_count to return 2.
242
// Calling set_to_default_member on a union that does not have an explicit
243
// default case raises TypeMismatch.
244
public void set_to_default_member ()
245         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc
246     {
247         if (status == STATUS_DESTROYED) {
248         throw wrapper.dynAnyDestroyed() ;
249         }
250         int defaultIndex = defaultIndex();
251         if (defaultIndex == -1) {
252             throw new TypeMismatch JavaDoc();
253         }
254         try {
255             clearData();
256             index = 1;
257             currentMemberIndex = defaultIndex;
258             currentMember = DynAnyUtil.createMostDerivedDynAny(memberType(defaultIndex), orb);
259             components = new DynAny[] {discriminator, currentMember};
260             Any JavaDoc discriminatorAny = orb.create_any();
261             discriminatorAny.insert_octet((byte)0);
262             discriminator = DynAnyUtil.createMostDerivedDynAny(discriminatorAny, orb, false);
263             representations = REPRESENTATION_COMPONENTS;
264         } catch (InconsistentTypeCode JavaDoc ictc) {}
265     }
266
267     // Sets the discriminator to a value that does not correspond
268
// to any of the unions case labels.
269
// It sets the current position to zero and causes component_count to return 1.
270
// Calling set_to_no_active_member on a union that has an explicit default case
271
// or on a union that uses the entire range of discriminator values
272
// for explicit case labels raises TypeMismatch.
273
public void set_to_no_active_member ()
274         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc
275     {
276         if (status == STATUS_DESTROYED) {
277         throw wrapper.dynAnyDestroyed() ;
278         }
279         // _REVISIT_ How does one check for "entire range of discriminator values"?
280
if (defaultIndex() != -1) {
281             throw new TypeMismatch JavaDoc();
282         }
283         checkInitComponents();
284         Any JavaDoc discriminatorAny = getAny(discriminator);
285         // erase the discriminators value so that it does not correspond
286
// to any of the unions case labels
287
discriminatorAny.type(discriminatorAny.type());
288         index = 0;
289         currentMemberIndex = NO_INDEX;
290         // Necessary to guarantee OBJECT_NOT_EXIST in member()
291
currentMember.destroy();
292         currentMember = null;
293         components[0] = discriminator;
294         representations = REPRESENTATION_COMPONENTS;
295     }
296
297     // Returns true if the union has no active member
298
// (that is, the unions value consists solely of its discriminator because the
299
// discriminator has a value that is not listed as an explicit case label).
300
// Calling this operation on a union that has a default case returns false.
301
// Calling this operation on a union that uses the entire range of discriminator
302
// values for explicit case labels returns false.
303
public boolean has_no_active_member () {
304         if (status == STATUS_DESTROYED) {
305         throw wrapper.dynAnyDestroyed() ;
306         }
307         // _REVISIT_ How does one check for "entire range of discriminator values"?
308
if (defaultIndex() != -1) {
309             return false;
310         }
311         checkInitComponents();
312         return (checkInitComponents() ? (currentMemberIndex == NO_INDEX) : false);
313     }
314
315     public org.omg.CORBA.TCKind JavaDoc discriminator_kind () {
316         if (status == STATUS_DESTROYED) {
317         throw wrapper.dynAnyDestroyed() ;
318         }
319         return discriminatorType().kind();
320     }
321
322     // Returns the currently active member.
323
// If the union has no active member, the operation raises InvalidValue.
324
// Note that the returned reference remains valid only for as long
325
// as the currently active member does not change.
326
// Using the returned reference beyond the life time
327
// of the currently active member raises OBJECT_NOT_EXIST.
328
public org.omg.DynamicAny.DynAny JavaDoc member ()
329         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
330     {
331         if (status == STATUS_DESTROYED) {
332         throw wrapper.dynAnyDestroyed() ;
333         }
334         if ( ! checkInitComponents() || currentMemberIndex == NO_INDEX)
335             throw new InvalidValue JavaDoc();
336         return currentMember;
337     }
338
339     // Returns the name of the currently active member.
340
// If the unions TypeCode does not contain a member name for the currently active member,
341
// the operation returns an empty string.
342
// Calling member_name on a union without an active member raises InvalidValue.
343
public String JavaDoc member_name ()
344         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
345     {
346         if (status == STATUS_DESTROYED) {
347         throw wrapper.dynAnyDestroyed() ;
348         }
349         if ( ! checkInitComponents() || currentMemberIndex == NO_INDEX)
350             throw new InvalidValue JavaDoc();
351         String JavaDoc memberName = memberName(currentMemberIndex);
352         return (memberName == null ? "" : memberName);
353     }
354
355     // Returns the TCKind value of the TypeCode of the currently active member.
356
// If the union has no active member, the operation raises InvalidValue.
357
public org.omg.CORBA.TCKind JavaDoc member_kind ()
358         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
359     {
360         if (status == STATUS_DESTROYED) {
361         throw wrapper.dynAnyDestroyed() ;
362         }
363         if ( ! checkInitComponents() || currentMemberIndex == NO_INDEX)
364             throw new InvalidValue JavaDoc();
365         return memberType(currentMemberIndex).kind();
366     }
367 }
368
Popular Tags