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