Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / gnu / CORBA / DynAn / gnuDynEnum.java
blob6eb7fe2bcaceb295dd7a0f98d4321582d46933d7
1 /* gnuDynEnum.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.CORBA.DynAn;
41 import gnu.CORBA.Unexpected;
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;
54 import java.io.IOException;
56 import java.util.Arrays;
58 /**
59 * Our implementation of dynamic enumeration.
61 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
63 public class gnuDynEnum extends UndivideableAny implements DynEnum
65 /**
66 * Use serialVersionUID for interoperability.
68 private static final long serialVersionUID = 1;
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.
75 final String[] values;
77 /**
78 * The current value of enum.
80 int current;
82 /**
83 * Create a new dyn enum from the given typecode.
85 public gnuDynEnum(TypeCode oType, TypeCode aType, gnuDynAnyFactory aFactory,
86 ORB anOrb
89 super(oType, aType, aFactory, anOrb);
90 try
92 values = new String[ final_type.member_count() ];
94 for (int i = 0; i < values.length; i++)
96 values [ i ] = final_type.member_name(i);
99 catch (Exception e)
101 throw new BAD_PARAM("Not enum");
106 * Create a clone of the given enum, sharing values and final_type.
108 public gnuDynEnum(gnuDynEnum from)
110 super(from.official_type, from.final_type, from.factory, from.orb);
111 values = from.values;
115 * Assign the Enum from the passed value. The passed DynAny must hold the
116 * enumeration of exactly the same final_type.
118 public void assign(DynAny from) throws TypeMismatch
120 checkType(official_type, from.type());
121 if (!(from instanceof DynEnum))
122 throw new TypeMismatch("Not a DynEnum");
125 set_as_ulong(((DynEnum) from).get_as_ulong());
127 catch (InvalidValue e)
129 TypeMismatch t = new TypeMismatch();
130 t.initCause(e);
131 throw t;
136 * Copy this DynEnum.
138 public DynAny copy()
140 gnuDynEnum other = new gnuDynEnum(this);
141 other.current = current;
142 return other;
146 * Compares for equality.
148 public boolean equal(DynAny other)
150 if (other instanceof gnuDynEnum)
152 gnuDynEnum oe = (gnuDynEnum) other;
153 return current == oe.current &&
154 (oe.values == values || Arrays.equals(values, oe.values));
156 else if (other instanceof DynEnum)
158 DynEnum oe = (DynEnum) other;
159 return current == oe.get_as_ulong() && official_type.equal(oe.type());
161 else
162 return false;
166 * Set value from any that must contain enumeration.
168 public void from_any(Any an_any) throws TypeMismatch, InvalidValue
170 checkType(official_type, an_any.type());
173 InputStream in = an_any.create_input_stream();
174 set_as_ulong(in.read_long());
175 in.close();
177 catch (MARSHAL eof)
179 throw new InvalidValue();
181 catch (IOException ex)
183 throw new Unexpected(ex);
188 * Get the value of this enumeration as string.
190 public String get_as_string()
192 return values [ current ];
196 * Get the value of this enumeration as int.
198 public int get_as_ulong()
200 return current;
204 * Set the value of this enumeration as string.
206 public void set_as_string(String value) throws InvalidValue
208 for (int i = 0; i < values.length; i++)
210 if (values [ i ].equals(value))
212 current = i;
213 valueChanged();
214 return;
217 throw new InvalidValue(value);
221 * Set the value of this enumeration as int.
223 public void set_as_ulong(int value) throws InvalidValue
225 if (value < 0 || value >= values.length)
226 throw new InvalidValue(value + " not in [0.." + values.length);
227 else
229 current = value;
230 valueChanged();
235 * Wrap the enumeration value into any.
237 public Any to_any()
239 Any a = createAny();
240 a.insert_long(current);
241 a.type(official_type);
242 return a;