001: /*
002: * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.impl.dynamicany;
027:
028: import org.omg.CORBA.TypeCode;
029: import org.omg.CORBA.Any;
030: import org.omg.CORBA.BAD_OPERATION;
031: import org.omg.CORBA.INTERNAL;
032: import org.omg.CORBA.TypeCodePackage.BadKind;
033: import org.omg.CORBA.TypeCodePackage.Bounds;
034: import org.omg.DynamicAny.*;
035: import org.omg.DynamicAny.DynAnyPackage.*;
036:
037: import com.sun.corba.se.spi.orb.ORB;
038: import com.sun.corba.se.spi.logging.CORBALogDomains;
039: import com.sun.corba.se.impl.logging.ORBUtilSystemException;
040:
041: public class DynEnumImpl extends DynAnyBasicImpl implements DynEnum {
042: //
043: // Instance variables
044: //
045:
046: // This int and the any value are kept in sync at all times
047: int currentEnumeratorIndex = NO_INDEX;
048:
049: //
050: // Constructors
051: //
052:
053: private DynEnumImpl() {
054: this (null, (Any) null, false);
055: }
056:
057: // The current position of a DynEnum is always -1.
058: protected DynEnumImpl(ORB orb, Any anAny, boolean copyValue) {
059: super (orb, anAny, copyValue);
060: index = NO_INDEX;
061: // The any doesn't have to be initialized. We have a default value in this case.
062: try {
063: currentEnumeratorIndex = any.extract_long();
064: } catch (BAD_OPERATION e) {
065: // _REVISIT_: Fix Me
066: currentEnumeratorIndex = 0;
067: any.type(any.type());
068: any.insert_long(0);
069: }
070: }
071:
072: // Sets the current position to -1 and sets the value of the enumerator
073: // to the first enumerator value indicated by the TypeCode.
074: protected DynEnumImpl(ORB orb, TypeCode typeCode) {
075: super (orb, typeCode);
076: index = NO_INDEX;
077: currentEnumeratorIndex = 0;
078: any.insert_long(0);
079: }
080:
081: //
082: // Utility methods
083: //
084:
085: private int memberCount() {
086: int memberCount = 0;
087: try {
088: memberCount = any.type().member_count();
089: } catch (BadKind bad) {
090: }
091: return memberCount;
092: }
093:
094: private String memberName(int i) {
095: String memberName = null;
096: try {
097: memberName = any.type().member_name(i);
098: } catch (BadKind bad) {
099: } catch (Bounds bounds) {
100: }
101: return memberName;
102: }
103:
104: private int computeCurrentEnumeratorIndex(String value) {
105: int memberCount = memberCount();
106: for (int i = 0; i < memberCount; i++) {
107: if (memberName(i).equals(value)) {
108: return i;
109: }
110: }
111: return NO_INDEX;
112: }
113:
114: //
115: // DynAny interface methods
116: //
117:
118: // Returns always 0 for DynEnum
119: public int component_count() {
120: return 0;
121: }
122:
123: // Calling current_component on a DynAny that cannot have components,
124: // such as a DynEnum or an empty exception, raises TypeMismatch.
125: public org.omg.DynamicAny.DynAny current_component()
126: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch {
127: if (status == STATUS_DESTROYED) {
128: throw wrapper.dynAnyDestroyed();
129: }
130: throw new TypeMismatch();
131: }
132:
133: //
134: // DynEnum interface methods
135: //
136:
137: // Returns the value of the DynEnum as an IDL identifier.
138: public String get_as_string() {
139: if (status == STATUS_DESTROYED) {
140: throw wrapper.dynAnyDestroyed();
141: }
142: return memberName(currentEnumeratorIndex);
143: }
144:
145: // Sets the value of the DynEnum to the enumerated value
146: // whose IDL identifier is passed in the value parameter.
147: // If value contains a string that is not a valid IDL identifier
148: // for the corresponding enumerated type, the operation raises InvalidValue.
149: public void set_as_string(String value)
150: throws org.omg.DynamicAny.DynAnyPackage.InvalidValue {
151: if (status == STATUS_DESTROYED) {
152: throw wrapper.dynAnyDestroyed();
153: }
154: int newIndex = computeCurrentEnumeratorIndex(value);
155: if (newIndex == NO_INDEX) {
156: throw new InvalidValue();
157: }
158: currentEnumeratorIndex = newIndex;
159: any.insert_long(newIndex);
160: }
161:
162: // Returns the value of the DynEnum as the enumerated values ordinal value.
163: // Enumerators have ordinal values 0 to n-1,
164: // as they appear from left to right in the corresponding IDL definition.
165: public int get_as_ulong() {
166: if (status == STATUS_DESTROYED) {
167: throw wrapper.dynAnyDestroyed();
168: }
169: return currentEnumeratorIndex;
170: }
171:
172: // Sets the value of the DynEnum as the enumerated values ordinal value.
173: // If value contains a value that is outside the range of ordinal values
174: // for the corresponding enumerated type, the operation raises InvalidValue.
175: public void set_as_ulong(int value)
176: throws org.omg.DynamicAny.DynAnyPackage.InvalidValue {
177: if (status == STATUS_DESTROYED) {
178: throw wrapper.dynAnyDestroyed();
179: }
180: if (value < 0 || value >= memberCount()) {
181: throw new InvalidValue();
182: }
183: currentEnumeratorIndex = value;
184: any.insert_long(value);
185: }
186: }
|