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. package com.sun.corba.se.impl.dynamicany;
  8. import org.omg.CORBA.TypeCode;
  9. import org.omg.CORBA.Any;
  10. import org.omg.CORBA.BAD_OPERATION;
  11. import org.omg.CORBA.INTERNAL;
  12. import org.omg.CORBA.TypeCodePackage.BadKind;
  13. import org.omg.CORBA.TypeCodePackage.Bounds;
  14. import org.omg.DynamicAny.*;
  15. import org.omg.DynamicAny.DynAnyPackage.*;
  16. import com.sun.corba.se.spi.orb.ORB ;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  18. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  19. public class DynEnumImpl extends DynAnyBasicImpl implements DynEnum
  20. {
  21. //
  22. // Instance variables
  23. //
  24. // This int and the any value are kept in sync at all times
  25. int currentEnumeratorIndex = NO_INDEX;
  26. //
  27. // Constructors
  28. //
  29. private DynEnumImpl() {
  30. this(null, (Any)null, false);
  31. }
  32. // The current position of a DynEnum is always -1.
  33. protected DynEnumImpl(ORB orb, Any anAny, boolean copyValue) {
  34. super(orb, anAny, copyValue);
  35. index = NO_INDEX;
  36. // The any doesn't have to be initialized. We have a default value in this case.
  37. try {
  38. currentEnumeratorIndex = any.extract_long();
  39. } catch (BAD_OPERATION e) {
  40. // _REVISIT_: Fix Me
  41. currentEnumeratorIndex = 0;
  42. any.type(any.type());
  43. any.insert_long(0);
  44. }
  45. }
  46. // Sets the current position to -1 and sets the value of the enumerator
  47. // to the first enumerator value indicated by the TypeCode.
  48. protected DynEnumImpl(ORB orb, TypeCode typeCode) {
  49. super(orb, typeCode);
  50. index = NO_INDEX;
  51. currentEnumeratorIndex = 0;
  52. any.insert_long(0);
  53. }
  54. //
  55. // Utility methods
  56. //
  57. private int memberCount() {
  58. int memberCount = 0;
  59. try {
  60. memberCount = any.type().member_count();
  61. } catch (BadKind bad) {
  62. }
  63. return memberCount;
  64. }
  65. private String memberName(int i) {
  66. String memberName = null;
  67. try {
  68. memberName = any.type().member_name(i);
  69. } catch (BadKind bad) {
  70. } catch (Bounds bounds) {
  71. }
  72. return memberName;
  73. }
  74. private int computeCurrentEnumeratorIndex(String value) {
  75. int memberCount = memberCount();
  76. for (int i=0; i<memberCount; i++) {
  77. if (memberName(i).equals(value)) {
  78. return i;
  79. }
  80. }
  81. return NO_INDEX;
  82. }
  83. //
  84. // DynAny interface methods
  85. //
  86. // Returns always 0 for DynEnum
  87. public int component_count() {
  88. return 0;
  89. }
  90. // Calling current_component on a DynAny that cannot have components,
  91. // such as a DynEnum or an empty exception, raises TypeMismatch.
  92. public org.omg.DynamicAny.DynAny current_component()
  93. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch
  94. {
  95. if (status == STATUS_DESTROYED) {
  96. throw wrapper.dynAnyDestroyed() ;
  97. }
  98. throw new TypeMismatch();
  99. }
  100. //
  101. // DynEnum interface methods
  102. //
  103. // Returns the value of the DynEnum as an IDL identifier.
  104. public String get_as_string () {
  105. if (status == STATUS_DESTROYED) {
  106. throw wrapper.dynAnyDestroyed() ;
  107. }
  108. return memberName(currentEnumeratorIndex);
  109. }
  110. // Sets the value of the DynEnum to the enumerated value
  111. // whose IDL identifier is passed in the value parameter.
  112. // If value contains a string that is not a valid IDL identifier
  113. // for the corresponding enumerated type, the operation raises InvalidValue.
  114. public void set_as_string (String value)
  115. throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
  116. {
  117. if (status == STATUS_DESTROYED) {
  118. throw wrapper.dynAnyDestroyed() ;
  119. }
  120. int newIndex = computeCurrentEnumeratorIndex(value);
  121. if (newIndex == NO_INDEX) {
  122. throw new InvalidValue();
  123. }
  124. currentEnumeratorIndex = newIndex;
  125. any.insert_long(newIndex);
  126. }
  127. // Returns the value of the DynEnum as the enumerated values ordinal value.
  128. // Enumerators have ordinal values 0 to n-1,
  129. // as they appear from left to right in the corresponding IDL definition.
  130. public int get_as_ulong () {
  131. if (status == STATUS_DESTROYED) {
  132. throw wrapper.dynAnyDestroyed() ;
  133. }
  134. return currentEnumeratorIndex;
  135. }
  136. // Sets the value of the DynEnum as the enumerated values ordinal value.
  137. // If value contains a value that is outside the range of ordinal values
  138. // for the corresponding enumerated type, the operation raises InvalidValue.
  139. public void set_as_ulong (int value)
  140. throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
  141. {
  142. if (status == STATUS_DESTROYED) {
  143. throw wrapper.dynAnyDestroyed() ;
  144. }
  145. if (value < 0 || value >= memberCount()) {
  146. throw new InvalidValue();
  147. }
  148. currentEnumeratorIndex = value;
  149. any.insert_long(value);
  150. }
  151. }