KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DynEnumImpl.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.Any JavaDoc;
12 import org.omg.CORBA.BAD_OPERATION JavaDoc;
13 import org.omg.CORBA.INTERNAL JavaDoc;
14 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
15 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
16 import org.omg.DynamicAny.*;
17 import org.omg.DynamicAny.DynAnyPackage.*;
18
19 import com.sun.corba.se.spi.orb.ORB ;
20 import com.sun.corba.se.spi.logging.CORBALogDomains ;
21 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
22
23 public class DynEnumImpl extends DynAnyBasicImpl implements DynEnum
24 {
25     //
26
// Instance variables
27
//
28

29     // This int and the any value are kept in sync at all times
30
int currentEnumeratorIndex = NO_INDEX;
31
32     //
33
// Constructors
34
//
35

36     private DynEnumImpl() {
37         this(null, (Any JavaDoc)null, false);
38     }
39
40     // The current position of a DynEnum is always -1.
41
protected DynEnumImpl(ORB orb, Any JavaDoc anAny, boolean copyValue) {
42         super(orb, anAny, copyValue);
43         index = NO_INDEX;
44         // The any doesn't have to be initialized. We have a default value in this case.
45
try {
46             currentEnumeratorIndex = any.extract_long();
47         } catch (BAD_OPERATION JavaDoc e) {
48             // _REVISIT_: Fix Me
49
currentEnumeratorIndex = 0;
50             any.type(any.type());
51             any.insert_long(0);
52         }
53     }
54
55     // Sets the current position to -1 and sets the value of the enumerator
56
// to the first enumerator value indicated by the TypeCode.
57
protected DynEnumImpl(ORB orb, TypeCode JavaDoc typeCode) {
58         super(orb, typeCode);
59         index = NO_INDEX;
60         currentEnumeratorIndex = 0;
61         any.insert_long(0);
62     }
63
64     //
65
// Utility methods
66
//
67

68     private int memberCount() {
69         int memberCount = 0;
70         try {
71             memberCount = any.type().member_count();
72         } catch (BadKind JavaDoc bad) {
73         }
74         return memberCount;
75     }
76
77     private String JavaDoc memberName(int i) {
78         String JavaDoc memberName = null;
79         try {
80             memberName = any.type().member_name(i);
81         } catch (BadKind JavaDoc bad) {
82         } catch (Bounds JavaDoc bounds) {
83         }
84         return memberName;
85     }
86
87     private int computeCurrentEnumeratorIndex(String JavaDoc value) {
88         int memberCount = memberCount();
89         for (int i=0; i<memberCount; i++) {
90             if (memberName(i).equals(value)) {
91                 return i;
92             }
93         }
94         return NO_INDEX;
95     }
96
97     //
98
// DynAny interface methods
99
//
100

101     // Returns always 0 for DynEnum
102
public int component_count() {
103     return 0;
104     }
105
106     // Calling current_component on a DynAny that cannot have components,
107
// such as a DynEnum or an empty exception, raises TypeMismatch.
108
public org.omg.DynamicAny.DynAny JavaDoc current_component()
109         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc
110     {
111         if (status == STATUS_DESTROYED) {
112         throw wrapper.dynAnyDestroyed() ;
113         }
114         throw new TypeMismatch();
115     }
116
117     //
118
// DynEnum interface methods
119
//
120

121     // Returns the value of the DynEnum as an IDL identifier.
122
public String JavaDoc get_as_string () {
123         if (status == STATUS_DESTROYED) {
124         throw wrapper.dynAnyDestroyed() ;
125         }
126     return memberName(currentEnumeratorIndex);
127     }
128
129     // Sets the value of the DynEnum to the enumerated value
130
// whose IDL identifier is passed in the value parameter.
131
// If value contains a string that is not a valid IDL identifier
132
// for the corresponding enumerated type, the operation raises InvalidValue.
133
public void set_as_string (String JavaDoc value)
134         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
135     {
136         if (status == STATUS_DESTROYED) {
137         throw wrapper.dynAnyDestroyed() ;
138         }
139     int newIndex = computeCurrentEnumeratorIndex(value);
140         if (newIndex == NO_INDEX) {
141             throw new InvalidValue();
142         }
143     currentEnumeratorIndex = newIndex;
144         any.insert_long(newIndex);
145     }
146
147     // Returns the value of the DynEnum as the enumerated values ordinal value.
148
// Enumerators have ordinal values 0 to n-1,
149
// as they appear from left to right in the corresponding IDL definition.
150
public int get_as_ulong () {
151         if (status == STATUS_DESTROYED) {
152         throw wrapper.dynAnyDestroyed() ;
153         }
154     return currentEnumeratorIndex;
155     }
156
157     // Sets the value of the DynEnum as the enumerated values ordinal value.
158
// If value contains a value that is outside the range of ordinal values
159
// for the corresponding enumerated type, the operation raises InvalidValue.
160
public void set_as_ulong (int value)
161         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
162     {
163         if (status == STATUS_DESTROYED) {
164         throw wrapper.dynAnyDestroyed() ;
165         }
166         if (value < 0 || value >= memberCount()) {
167             throw new InvalidValue();
168         }
169     currentEnumeratorIndex = value;
170         any.insert_long(value);
171     }
172 }
173
Popular Tags