OSDN Git Service

Normalise whitespace in GNU Classpath.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / CORBA / DynAn / gnuDynEnum.java
1 /* gnuDynEnum.java --
2    Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.CORBA.DynAn;
40
41 import gnu.CORBA.Unexpected;
42
43 import org.omg.CORBA.Any;
44 import org.omg.CORBA.BAD_PARAM;
45 import org.omg.CORBA.MARSHAL;
46 import org.omg.CORBA.ORB;
47 import org.omg.CORBA.TypeCode;
48 import org.omg.CORBA.portable.InputStream;
49 import org.omg.DynamicAny.DynAny;
50 import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
51 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
52 import org.omg.DynamicAny.DynEnum;
53
54 import java.io.IOException;
55
56 import java.util.Arrays;
57
58 /**
59  * Our implementation of dynamic enumeration.
60  *
61  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
62  */
63 public class gnuDynEnum extends UndivideableAny implements DynEnum
64 {
65   /**
66    * Use serialVersionUID for interoperability.
67    */
68   private static final long serialVersionUID = 1;
69
70   /**
71    * The valid string values of the enumeration. Most of enumerations are short,
72    * counting 2-5 memebers. With so small number of memebers, it seems not
73    * reasonable to use hashtables.
74    */
75   final String[] values;
76
77   /**
78    * The current value of enum.
79    */
80   int current;
81
82   /**
83    * Create a new dyn enum from the given typecode.
84    */
85   public gnuDynEnum(TypeCode oType, TypeCode aType, gnuDynAnyFactory aFactory,
86     ORB anOrb
87   )
88   {
89     super(oType, aType, aFactory, anOrb);
90     try
91       {
92         values = new String[ final_type.member_count() ];
93
94         for (int i = 0; i < values.length; i++)
95           {
96             values [ i ] = final_type.member_name(i);
97           }
98       }
99     catch (Exception e)
100       {
101         throw new BAD_PARAM("Not enum");
102       }
103   }
104
105   /**
106    * Create a clone of the given enum, sharing values and final_type.
107    */
108   public gnuDynEnum(gnuDynEnum from)
109   {
110     super(from.official_type, from.final_type, from.factory, from.orb);
111     values = from.values;
112   }
113
114   /**
115    * Assign the Enum from the passed value. The passed DynAny must hold the
116    * enumeration of exactly the same final_type.
117    */
118   public void assign(DynAny from) throws TypeMismatch
119   {
120     checkType(official_type, from.type());
121     if (!(from instanceof DynEnum))
122       throw new TypeMismatch("Not a DynEnum");
123     try
124       {
125         set_as_ulong(((DynEnum) from).get_as_ulong());
126       }
127     catch (InvalidValue e)
128       {
129         TypeMismatch t = new TypeMismatch();
130         t.initCause(e);
131         throw t;
132       }
133   }
134
135   /**
136    * Copy this DynEnum.
137    */
138   public DynAny copy()
139   {
140     gnuDynEnum other = new gnuDynEnum(this);
141     other.current = current;
142     return other;
143   }
144
145   /**
146    * Compares for equality.
147    */
148   public boolean equal(DynAny other)
149   {
150     if (other instanceof gnuDynEnum)
151       {
152         gnuDynEnum oe = (gnuDynEnum) other;
153         return current == oe.current &&
154         (oe.values == values || Arrays.equals(values, oe.values));
155       }
156     else if (other instanceof DynEnum)
157       {
158         DynEnum oe = (DynEnum) other;
159         return current == oe.get_as_ulong() && official_type.equal(oe.type());
160       }
161     else
162       return false;
163   }
164
165   /**
166    * Set value from any that must contain enumeration.
167    */
168   public void from_any(Any an_any) throws TypeMismatch, InvalidValue
169   {
170     checkType(official_type, an_any.type());
171     try
172       {
173         InputStream in = an_any.create_input_stream();
174         set_as_ulong(in.read_long());
175         in.close();
176       }
177     catch (MARSHAL eof)
178       {
179         throw new InvalidValue();
180       }
181     catch (IOException ex)
182       {
183         throw new Unexpected(ex);
184       }
185   }
186
187   /**
188    * Get the value of this enumeration as string.
189    */
190   public String get_as_string()
191   {
192     return values [ current ];
193   }
194
195   /**
196    * Get the value of this enumeration as int.
197    */
198   public int get_as_ulong()
199   {
200     return current;
201   }
202
203   /**
204    * Set the value of this enumeration as string.
205    */
206   public void set_as_string(String value) throws InvalidValue
207   {
208     for (int i = 0; i < values.length; i++)
209       {
210         if (values [ i ].equals(value))
211           {
212             current = i;
213             valueChanged();
214             return;
215           }
216       }
217     throw new InvalidValue(value);
218   }
219
220   /**
221    * Set the value of this enumeration as int.
222    */
223   public void set_as_ulong(int value) throws InvalidValue
224   {
225     if (value < 0 || value >= values.length)
226       throw new InvalidValue(value + " not in [0.." + values.length);
227     else
228       {
229         current = value;
230         valueChanged();
231       }
232   }
233
234   /**
235    * Wrap the enumeration value into any.
236    */
237   public Any to_any()
238   {
239     Any a = createAny();
240     a.insert_long(current);
241     a.type(official_type);
242     return a;
243   }
244 }