1. /*
  2. * @(#)DynAnyCollectionImpl.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.TypeCodePackage.BadKind;
  14. import org.omg.CORBA.TypeCodePackage.Bounds;
  15. import org.omg.DynamicAny.*;
  16. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  17. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  18. import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  19. abstract class DynAnyCollectionImpl extends DynAnyConstructedImpl
  20. {
  21. //
  22. // Instance variables
  23. //
  24. // Keep in sync with DynAny[] components at all times.
  25. Any[] anys = null;
  26. //
  27. // Constructors
  28. //
  29. private DynAnyCollectionImpl() {
  30. this(null, (Any)null, false);
  31. }
  32. protected DynAnyCollectionImpl(ORB orb, Any any, boolean copyValue) {
  33. super(orb, any, copyValue);
  34. }
  35. protected DynAnyCollectionImpl(ORB orb, TypeCode typeCode) {
  36. super(orb, typeCode);
  37. }
  38. //
  39. // Utility methods
  40. //
  41. protected void createDefaultComponentAt(int i, TypeCode contentType) {
  42. try {
  43. components[i] = DynAnyUtil.createMostDerivedDynAny(contentType, orb);
  44. } catch (InconsistentTypeCode itc) { // impossible
  45. }
  46. // get a hold of the default initialized Any without copying
  47. anys[i] = getAny(components[i]);
  48. }
  49. protected TypeCode getContentType() {
  50. try {
  51. return any.type().content_type();
  52. } catch (BadKind badKind) { // impossible
  53. return null;
  54. }
  55. }
  56. // This method has a different meaning for sequence and array:
  57. // For sequence value of 0 indicates an unbounded sequence,
  58. // values > 0 indicate a bounded sequence.
  59. // For array any value indicates the boundary.
  60. protected int getBound() {
  61. try {
  62. return any.type().length();
  63. } catch (BadKind badKind) { // impossible
  64. return 0;
  65. }
  66. }
  67. //
  68. // DynAny interface methods
  69. //
  70. // _REVISIT_ More efficient copy operation
  71. //
  72. // Collection methods
  73. //
  74. public org.omg.CORBA.Any[] get_elements () {
  75. if (status == STATUS_DESTROYED) {
  76. throw new OBJECT_NOT_EXIST();
  77. }
  78. return (checkInitComponents() ? anys : null);
  79. }
  80. protected abstract void checkValue(Object[] value)
  81. throws org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  82. // Initializes the elements of the ordered collection.
  83. // If value does not contain the same number of elements as the array dimension,
  84. // the operation raises InvalidValue.
  85. // If one or more elements have a type that is inconsistent with the collections TypeCode,
  86. // the operation raises TypeMismatch.
  87. // This operation does not change the current position.
  88. public void set_elements (org.omg.CORBA.Any[] value)
  89. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  90. org.omg.DynamicAny.DynAnyPackage.InvalidValue
  91. {
  92. if (status == STATUS_DESTROYED) {
  93. throw new OBJECT_NOT_EXIST();
  94. }
  95. checkValue(value);
  96. components = new DynAny[value.length];
  97. anys = value;
  98. // We know that this is of kind tk_sequence or tk_array
  99. TypeCode expectedTypeCode = getContentType();
  100. for (int i=0; i<value.length; i++) {
  101. if (value[i] != null) {
  102. if (! value[i].type().equal(expectedTypeCode)) {
  103. clearData();
  104. // _REVISIT_ More info
  105. throw new TypeMismatch();
  106. }
  107. try {
  108. // Creates the appropriate subtype without copying the Any
  109. components[i] = DynAnyUtil.createMostDerivedDynAny(value[i], orb, false);
  110. //System.out.println(this + " created component " + components[i]);
  111. } catch (InconsistentTypeCode itc) {
  112. throw new InvalidValue();
  113. }
  114. } else {
  115. clearData();
  116. // _REVISIT_ More info
  117. throw new InvalidValue();
  118. }
  119. }
  120. index = (value.length == 0 ? NO_INDEX : 0);
  121. // Other representations are invalidated by this operation
  122. representations = REPRESENTATION_COMPONENTS;
  123. }
  124. public org.omg.DynamicAny.DynAny[] get_elements_as_dyn_any () {
  125. if (status == STATUS_DESTROYED) {
  126. throw new OBJECT_NOT_EXIST();
  127. }
  128. return (checkInitComponents() ? components : null);
  129. }
  130. // Same semantics as set_elements(Any[])
  131. public void set_elements_as_dyn_any (org.omg.DynamicAny.DynAny[] value)
  132. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  133. org.omg.DynamicAny.DynAnyPackage.InvalidValue
  134. {
  135. if (status == STATUS_DESTROYED) {
  136. throw new OBJECT_NOT_EXIST();
  137. }
  138. checkValue(value);
  139. components = (value == null ? emptyComponents : value);
  140. anys = new Any[value.length];
  141. // We know that this is of kind tk_sequence or tk_array
  142. TypeCode expectedTypeCode = getContentType();
  143. for (int i=0; i<value.length; i++) {
  144. if (value[i] != null) {
  145. if (! value[i].type().equal(expectedTypeCode)) {
  146. clearData();
  147. // _REVISIT_ More info
  148. throw new TypeMismatch();
  149. }
  150. anys[i] = getAny(value[i]);
  151. } else {
  152. clearData();
  153. // _REVISIT_ More info
  154. throw new InvalidValue();
  155. }
  156. }
  157. index = (value.length == 0 ? NO_INDEX : 0);
  158. // Other representations are invalidated by this operation
  159. representations = REPRESENTATION_COMPONENTS;
  160. }
  161. }