Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / CORBA / DynAn / gnuDynValue.java
blob0c31d4093e2e5797c7f3dd99b0656987b1ae4635
1 /* gnuDynValue.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.Minor;
42 import gnu.CORBA.Unexpected;
44 import org.omg.CORBA.Any;
45 import org.omg.CORBA.BAD_PARAM;
46 import org.omg.CORBA.MARSHAL;
47 import org.omg.CORBA.ORB;
48 import org.omg.CORBA.TCKind;
49 import org.omg.CORBA.TypeCode;
50 import org.omg.CORBA.VM_TRUNCATABLE;
51 import org.omg.CORBA.portable.OutputStream;
52 import org.omg.CORBA.portable.ValueFactory;
53 import org.omg.DynamicAny.DynAny;
54 import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
55 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
56 import org.omg.DynamicAny.DynStruct;
57 import org.omg.DynamicAny.DynValue;
58 import org.omg.DynamicAny.DynValueCommon;
59 import org.omg.DynamicAny.DynValueOperations;
60 import org.omg.DynamicAny.NameDynAnyPair;
61 import org.omg.DynamicAny.NameValuePair;
63 import java.io.Serializable;
65 /**
66 * Implementation of DynValue.
68 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
70 public class gnuDynValue extends RecordAny implements DynValue,
71 Serializable
73 /**
74 * Use serialVersionUID for interoperability.
76 private static final long serialVersionUID = 1;
78 /**
79 * If true, the value of this ValueType is set to null.
81 boolean isNull;
83 /**
84 * Create an instance.
86 public gnuDynValue(TypeCode oType, TypeCode aType,
87 gnuDynAnyFactory aFactory, ORB anOrb
90 super(oType, aType, aFactory, anOrb);
92 // Initialise fields. The array of fields also includes all inherited
93 // fields.
94 try
96 array = new DynAny[ final_type.member_count() ];
97 fNames = new String[ array.length ];
98 for (int i = 0; i < array.length; i++)
100 array [ i ] =
101 factory.create_dyn_any_from_type_code(final_type.member_type(i));
102 fNames [ i ] = final_type.member_name(i);
105 // Search of inherited members.
106 if (final_type.type_modifier() == VM_TRUNCATABLE.value)
108 TypeCode parent = final_type.concrete_base_type();
109 DynAny ancestor = factory.create_dyn_any_from_type_code(parent);
111 if (ancestor instanceof DynValue)
113 // Add members of ancestor in front of the curren members.
114 DynValue anc = (DynValue) ancestor;
115 anc.set_to_value();
117 NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
118 inheritFields(aar);
120 else if (ancestor instanceof DynStruct)
122 // Add members of ancestor in front of the curren members.
123 DynStruct anc = (DynStruct) ancestor;
124 NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
125 inheritFields(aar);
127 else
128 throw new BAD_PARAM("The parent of " + final_type.id() + ", " +
129 parent.id() + ", is not structure nor value."
133 catch (Exception e)
135 throw new Unexpected(e);
138 set_to_null();
142 * Inherit the provided fields.
144 private void inheritFields(NameDynAnyPair[] aar)
146 DynAny[] nArray = new DynAny[ array.length + aar.length ];
147 String[] nNames = new String[ array.length + aar.length ];
148 int p = 0;
149 for (int i = 0; i < aar.length; i++)
151 nArray [ p ] = aar [ i ].value;
152 nNames [ p ] = aar [ i ].id;
153 p++;
156 for (int i = 0; i < array.length; i++)
158 nArray [ p ] = array [ i ];
159 nNames [ p ] = fNames [ i ];
160 p++;
163 array = nArray;
164 fNames = nNames;
167 /** @inheritDoc */
168 public TCKind current_member_kind() throws TypeMismatch, InvalidValue
170 if (isNull)
171 throw new TypeMismatch(ISNULL);
172 else
173 return super.current_member_kind();
177 /** @inheritDoc */
178 public String current_member_name() throws TypeMismatch, InvalidValue
180 if (isNull)
181 throw new TypeMismatch(ISNULL);
182 else
183 return super.current_member_name();
187 /** @inheritDoc */
188 public NameDynAnyPair[] get_members_as_dyn_any() throws InvalidValue
190 if (isNull)
191 throw new InvalidValue(ISNULL);
192 return super.gnu_get_members_as_dyn_any();
196 /** @inheritDoc */
197 public NameValuePair[] get_members() throws InvalidValue
199 if (isNull)
200 throw new InvalidValue(ISNULL);
201 else
202 return super.gnu_get_members();
206 /** @inheritDoc */
207 public void set_members_as_dyn_any(NameDynAnyPair[] value)
208 throws TypeMismatch, InvalidValue
210 super.set_members_as_dyn_any(value);
211 isNull = false;
215 /** @inheritDoc */
216 public void set_members(NameValuePair[] value)
217 throws TypeMismatch, InvalidValue
219 super.set_members(value);
220 isNull = false;
224 /** @inheritDoc */
225 public boolean is_null()
227 return isNull;
230 /** @inheritDoc */
231 public void set_to_null()
233 isNull = true;
234 valueChanged();
237 /** @inheritDoc */
238 public void set_to_value()
240 isNull = false;
241 valueChanged();
245 * Create a new instance.
247 protected RecordAny newInstance(TypeCode oType, TypeCode aType,
248 gnuDynAnyFactory aFactory, ORB anOrb
251 gnuDynValue v = new gnuDynValue(oType, aType, aFactory, anOrb);
252 if (isNull)
253 v.set_to_null();
254 else
255 v.set_to_value();
256 return v;
260 * Compare for equality, minding null values.
262 public boolean equal(DynAny other)
264 if (other instanceof DynValueOperations)
266 DynValueCommon o = (DynValueCommon) other;
267 if (isNull)
268 return o.is_null() && o.type().equal(official_type);
269 else
270 return !o.is_null() && super.equal(other);
272 else
273 return false;
277 * Get the focused component, throwing exception if the current value is null.
279 protected DynAny focused() throws InvalidValue, TypeMismatch
281 if (isNull)
282 throw new TypeMismatch(ISNULL);
283 else
284 return super.focused();
288 * Convert into Any.
290 public Any to_any()
292 if (isNull)
294 Any a0 = createAny();
295 a0.type(orb.get_primitive_tc(TCKind.tk_null));
296 return a0;
298 else
302 ValueFactory factory =
303 ((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory(official_type.id());
304 if (factory == null)
306 MARSHAL m = new MARSHAL("Factory for " + official_type.id() +
307 " not registered.");
308 m.minor = Minor.Factory;
309 throw m;
312 OutputStream out = orb.create_output_stream();
314 for (int i = 0; i < array.length; i++)
315 array [ i ].to_any().write_value(out);
317 org.omg.CORBA_2_3.portable.InputStream in =
318 (org.omg.CORBA_2_3.portable.InputStream) out.create_input_stream();
319 Serializable v = factory.read_value(in);
321 Any g = createAny();
322 g.type(official_type);
323 g.insert_Value(v, official_type);
325 return g;
327 catch (Exception e)
329 throw new Unexpected(e);
334 /** @inheritDoc */
335 public void assign(DynAny from) throws TypeMismatch
337 checkType(official_type, from.type());
339 if (from instanceof DynValue)
341 DynValue other = (DynValue) from;
342 if (other.is_null())
343 set_to_null();
344 else
346 set_to_value();
349 DynValueOperations src = (DynValueOperations) from;
350 set_members_as_dyn_any(src.get_members_as_dyn_any());
352 catch (InvalidValue e)
354 TypeMismatch t = new TypeMismatch("Invalid value");
355 t.initCause(e);
356 throw t;
360 else
361 throw new TypeMismatch("Not a DynValue");
365 * Get the number of components.
367 public int component_count()
369 return isNull ? 0 : super.component_count();
372 /** {@inheritDoc} */
373 public Serializable get_val() throws TypeMismatch, InvalidValue
375 return to_any().extract_Value();
378 /** {@inheritDoc} */
379 public void insert_val(Serializable a_x) throws InvalidValue, TypeMismatch
381 Any a = to_any();
382 a.insert_Value(a_x);
383 from_any(a);
384 valueChanged();