gnuDynSequence.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* gnuDynSequence.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.DynAn;
  32. import gnu.CORBA.Unexpected;
  33. import org.omg.CORBA.Any;
  34. import org.omg.CORBA.BAD_PARAM;
  35. import org.omg.CORBA.ORB;
  36. import org.omg.CORBA.TypeCode;
  37. import org.omg.CORBA.TypeCodePackage.BadKind;
  38. import org.omg.DynamicAny.DynAny;
  39. import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  40. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  41. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  42. import org.omg.DynamicAny.DynSequence;
  43. import java.io.Serializable;
  44. import java.lang.reflect.*;
  45. public class gnuDynSequence
  46. extends gnuDynArray
  47. implements DynSequence, Serializable
  48. {
  49. /**
  50. * Use serialVersionUID for interoperability.
  51. */
  52. private static final long serialVersionUID = 1;
  53. /**
  54. * The bound of the sequence, as defined in typecode.
  55. */
  56. final int bound;
  57. /**
  58. * Create a new gnuDynSequence with the given typecode.
  59. *
  60. * @throws BAD_PARAM if the passed typecode is probably not a sequence
  61. * typecode.
  62. */
  63. public gnuDynSequence(TypeCode oType, TypeCode aType,
  64. gnuDynAnyFactory aFactory, ORB anOrb
  65. )
  66. throws BAD_PARAM
  67. {
  68. super(oType, aType, aFactory, anOrb, false);
  69. array = new DynAny[ 0 ];
  70. try
  71. {
  72. bound = final_type.length();
  73. }
  74. catch (BadKind ex)
  75. {
  76. throw new Unexpected(ex);
  77. }
  78. }
  79. /**
  80. * Get the length of the sequence.
  81. */
  82. public int get_length()
  83. {
  84. return array.length;
  85. }
  86. /**
  87. * Resize the sequence, preserving components.
  88. */
  89. public void set_length(int length)
  90. throws InvalidValue
  91. {
  92. checkBound(length);
  93. if (length == array.length)
  94. return; // Nothing to do.
  95. else if (length < array.length)
  96. {
  97. // Truncate.
  98. DynAny[] d = new DynAny[ length ];
  99. for (int i = 0; i < d.length; i++)
  100. d [ i ] = array [ i ];
  101. array = d;
  102. }
  103. else
  104. {
  105. // Expand.
  106. DynAny[] d = new DynAny[ length ];
  107. for (int i = 0; i < array.length; i++)
  108. d [ i ] = array [ i ];
  109. for (int i = array.length; i < d.length; i++)
  110. {
  111. try
  112. {
  113. d [ i ] =
  114. factory.create_dyn_any_from_type_code(official_components);
  115. }
  116. catch (InconsistentTypeCode e)
  117. {
  118. throw new Unexpected(e);
  119. }
  120. }
  121. array = d;
  122. }
  123. valueChanged();
  124. }
  125. /**
  126. * Copy one DynAny into another.
  127. */
  128. public void assign(DynAny from)
  129. throws TypeMismatch
  130. {
  131. checkType(official_type, from.type());
  132. if (from instanceof DynSequence)
  133. {
  134. DynSequence dyn = (DynSequence) from;
  135. array = dyn.get_elements_as_dyn_any();
  136. }
  137. else
  138. throw new TypeMismatch();
  139. }
  140. /*
  141. * Set the contenst of the sequence, resizing if required.
  142. */
  143. public void set_elements_as_dyn_any(DynAny[] value)
  144. throws InvalidValue, TypeMismatch
  145. {
  146. checkBound(value.length);
  147. if (array.length != value.length)
  148. set_length(value.length);
  149. for (int i = 0; i < value.length; i++)
  150. {
  151. checkType(official_components, value [ i ].type());
  152. array [ i ].assign(value [ i ]);
  153. }
  154. valueChanged();
  155. }
  156. /**
  157. * Set the elements from array of Any's.
  158. */
  159. public void set_elements(Any[] value)
  160. throws InvalidValue, TypeMismatch
  161. {
  162. checkBound(value.length);
  163. DynAny[] prev = array;
  164. array = new DynAny[ value.length ];
  165. try
  166. {
  167. super.set_elements(value);
  168. // valueChanged() is called in super.set_elements(value).
  169. }
  170. // On the problem, value does not change.
  171. catch (TypeMismatch ex)
  172. {
  173. array = prev;
  174. throw ex;
  175. }
  176. catch (InvalidValue ex)
  177. {
  178. array = prev;
  179. throw ex;
  180. }
  181. catch (RuntimeException rex)
  182. {
  183. array = prev;
  184. throw rex;
  185. }
  186. }
  187. /**
  188. * Create a copy.
  189. */
  190. public DynAny copy()
  191. {
  192. DynAny[] c = new DynAny[ array.length ];
  193. for (int i = 0; i < c.length; i++)
  194. {
  195. c [ i ] = array [ i ].copy();
  196. }
  197. gnuDynSequence d =
  198. new gnuDynSequence(official_type, final_type, factory, orb);
  199. d.array = c;
  200. return d;
  201. }
  202. /**
  203. * Check the bound.
  204. *
  205. * @param x the value to check.
  206. */
  207. void checkBound(int x)
  208. throws InvalidValue
  209. {
  210. if (bound != 0)
  211. if (x < 0 || x > bound)
  212. throw new InvalidValue(x + " out of bounds, valid [0.." + bound + "]");
  213. }
  214. /**
  215. * Check if array size is valid. Called from from_any.
  216. */
  217. protected void checkArrayValid(Object members)
  218. throws TypeMismatch, InvalidValue
  219. {
  220. checkBound(Array.getLength(members));
  221. if (get_length() != Array.getLength(members))
  222. set_length(Array.getLength(members));
  223. }
  224. }