gnuDynEnum.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* gnuDynEnum.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.MARSHAL;
  36. import org.omg.CORBA.ORB;
  37. import org.omg.CORBA.TypeCode;
  38. import org.omg.CORBA.portable.InputStream;
  39. import org.omg.DynamicAny.DynAny;
  40. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  41. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  42. import org.omg.DynamicAny.DynEnum;
  43. import java.io.IOException;
  44. import java.util.Arrays;
  45. /**
  46. * Our implementation of dynamic enumeration.
  47. *
  48. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  49. */
  50. public class gnuDynEnum extends UndivideableAny implements DynEnum
  51. {
  52. /**
  53. * Use serialVersionUID for interoperability.
  54. */
  55. private static final long serialVersionUID = 1;
  56. /**
  57. * The valid string values of the enumeration. Most of enumerations are short,
  58. * counting 2-5 memebers. With so small number of memebers, it seems not
  59. * reasonable to use hashtables.
  60. */
  61. final String[] values;
  62. /**
  63. * The current value of enum.
  64. */
  65. int current;
  66. /**
  67. * Create a new dyn enum from the given typecode.
  68. */
  69. public gnuDynEnum(TypeCode oType, TypeCode aType, gnuDynAnyFactory aFactory,
  70. ORB anOrb
  71. )
  72. {
  73. super(oType, aType, aFactory, anOrb);
  74. try
  75. {
  76. values = new String[ final_type.member_count() ];
  77. for (int i = 0; i < values.length; i++)
  78. {
  79. values [ i ] = final_type.member_name(i);
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. throw new BAD_PARAM("Not enum");
  85. }
  86. }
  87. /**
  88. * Create a clone of the given enum, sharing values and final_type.
  89. */
  90. public gnuDynEnum(gnuDynEnum from)
  91. {
  92. super(from.official_type, from.final_type, from.factory, from.orb);
  93. values = from.values;
  94. }
  95. /**
  96. * Assign the Enum from the passed value. The passed DynAny must hold the
  97. * enumeration of exactly the same final_type.
  98. */
  99. public void assign(DynAny from) throws TypeMismatch
  100. {
  101. checkType(official_type, from.type());
  102. if (!(from instanceof DynEnum))
  103. throw new TypeMismatch("Not a DynEnum");
  104. try
  105. {
  106. set_as_ulong(((DynEnum) from).get_as_ulong());
  107. }
  108. catch (InvalidValue e)
  109. {
  110. TypeMismatch t = new TypeMismatch();
  111. t.initCause(e);
  112. throw t;
  113. }
  114. }
  115. /**
  116. * Copy this DynEnum.
  117. */
  118. public DynAny copy()
  119. {
  120. gnuDynEnum other = new gnuDynEnum(this);
  121. other.current = current;
  122. return other;
  123. }
  124. /**
  125. * Compares for equality.
  126. */
  127. public boolean equal(DynAny other)
  128. {
  129. if (other instanceof gnuDynEnum)
  130. {
  131. gnuDynEnum oe = (gnuDynEnum) other;
  132. return current == oe.current &&
  133. (oe.values == values || Arrays.equals(values, oe.values));
  134. }
  135. else if (other instanceof DynEnum)
  136. {
  137. DynEnum oe = (DynEnum) other;
  138. return current == oe.get_as_ulong() && official_type.equal(oe.type());
  139. }
  140. else
  141. return false;
  142. }
  143. /**
  144. * Set value from any that must contain enumeration.
  145. */
  146. public void from_any(Any an_any) throws TypeMismatch, InvalidValue
  147. {
  148. checkType(official_type, an_any.type());
  149. try
  150. {
  151. InputStream in = an_any.create_input_stream();
  152. set_as_ulong(in.read_long());
  153. in.close();
  154. }
  155. catch (MARSHAL eof)
  156. {
  157. throw new InvalidValue();
  158. }
  159. catch (IOException ex)
  160. {
  161. throw new Unexpected(ex);
  162. }
  163. }
  164. /**
  165. * Get the value of this enumeration as string.
  166. */
  167. public String get_as_string()
  168. {
  169. return values [ current ];
  170. }
  171. /**
  172. * Get the value of this enumeration as int.
  173. */
  174. public int get_as_ulong()
  175. {
  176. return current;
  177. }
  178. /**
  179. * Set the value of this enumeration as string.
  180. */
  181. public void set_as_string(String value) throws InvalidValue
  182. {
  183. for (int i = 0; i < values.length; i++)
  184. {
  185. if (values [ i ].equals(value))
  186. {
  187. current = i;
  188. valueChanged();
  189. return;
  190. }
  191. }
  192. throw new InvalidValue(value);
  193. }
  194. /**
  195. * Set the value of this enumeration as int.
  196. */
  197. public void set_as_ulong(int value) throws InvalidValue
  198. {
  199. if (value < 0 || value >= values.length)
  200. throw new InvalidValue(value + " not in [0.." + values.length);
  201. else
  202. {
  203. current = value;
  204. valueChanged();
  205. }
  206. }
  207. /**
  208. * Wrap the enumeration value into any.
  209. */
  210. public Any to_any()
  211. {
  212. Any a = createAny();
  213. a.insert_long(current);
  214. a.type(official_type);
  215. return a;
  216. }
  217. }