Create embedded-5_0-branch branch for development on ARM embedded cores.
[official-gcc.git] / embedded-5_0-branch / libjava / classpath / gnu / CORBA / DynAn / gnuDynValueBox.java
blob9c50534ed0990a9780cc22f7e2b94c45b5410161
1 /* gnuDynValueBox.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;
42 import gnu.CORBA.HolderLocator;
44 import org.omg.CORBA.Any;
45 import org.omg.CORBA.ORB;
46 import org.omg.CORBA.TCKind;
47 import org.omg.CORBA.TypeCode;
48 import org.omg.CORBA.TypeCodePackage.BadKind;
49 import org.omg.CORBA.portable.Streamable;
50 import org.omg.DynamicAny.DynAny;
51 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
52 import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
53 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
54 import org.omg.DynamicAny.DynValueBox;
55 import org.omg.DynamicAny.DynValueBoxOperations;
56 import org.omg.DynamicAny.DynValueCommon;
58 import java.io.Serializable;
60 import java.lang.reflect.Field;
62 /**
63 * Implementation of the DynValueBox.
65 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
67 public class gnuDynValueBox
68 extends DivideableAny
69 implements DynValueBox, Serializable
71 /**
72 * Use serialVersionUID for interoperability.
74 private static final long serialVersionUID = 1;
76 /**
77 * The final_type of contents of this value box.
79 final TypeCode content;
81 /**
82 * The string for some TypeMismatch exceptions.
84 String CONTENT = "Box content final_type mismatch";
86 /**
87 * Create a new instance of gnuDynValueBox.
89 public gnuDynValueBox(TypeCode oType, TypeCode aType,
90 gnuDynAnyFactory aFactory, ORB anOrb
93 super(oType, aType, aFactory, anOrb);
94 try
96 content = final_type.content_type();
97 array = new DynAny[] { factory.create_dyn_any_from_type_code(content) };
98 set_to_null();
100 catch (Exception e)
102 throw new Unexpected(e);
106 /** @inheritDoc */
107 public void assign(DynAny from)
108 throws TypeMismatch
110 checkType(official_type, from.type());
111 if (from instanceof DynValueBoxOperations)
113 DynValueBoxOperations other = (DynValueBoxOperations) from;
114 if (other.is_null())
115 set_to_null();
116 else
118 DynAny inBox;
121 inBox = other.get_boxed_value_as_dyn_any();
123 catch (InvalidValue e)
125 TypeMismatch t = new TypeMismatch("Invalid value");
126 t.initCause(e);
127 throw t;
129 if (!content.equal(inBox.type()))
130 throw new TypeMismatch(CONTENT);
131 array = new DynAny[] { inBox.copy() };
134 valueChanged();
137 /** @inheritDoc */
138 public DynAny copy()
140 gnuDynValueBox other =
141 new gnuDynValueBox(official_type, final_type, factory, orb);
142 if (is_null())
143 other.set_to_null();
144 else
148 other.array = new DynAny[] { array [ 0 ].copy() };
150 catch (Exception e)
152 throw new Unexpected(e);
155 return other;
159 * Returns null for null value, delegates to super. otherwise.
161 public DynAny current_component()
162 throws TypeMismatch
164 if (is_null())
165 return null;
166 else
167 return super.current_component();
171 * Compare for equality, minding null values.
173 public boolean equal(DynAny other)
175 if (other instanceof DynValueCommon)
177 DynValueCommon o = (DynValueCommon) other;
178 if (is_null())
179 return o.is_null() && o.type().equal(official_type);
180 else
181 return !o.is_null() && super.equal(other);
183 else
184 return false;
187 /** @inheritDoc */
188 public void from_any(Any an_any)
189 throws TypeMismatch, InvalidValue
191 checkType(official_type, an_any.type());
194 if (!an_any.type().content_type().equal(content))
195 throw new InvalidValue(CONTENT);
197 catch (BadKind e)
199 TypeMismatch t = new TypeMismatch("Not a box");
200 t.initCause(e);
201 throw t;
204 Serializable s = an_any.extract_Value();
205 if (s == null)
206 set_to_null();
207 else
211 Streamable holder = HolderLocator.createHolder(content);
212 Field v = holder.getClass().getField("value");
213 v.set(holder, s);
215 Any cont = createAny();
216 cont.insert_Streamable(holder);
218 array = new DynAny[] { factory.create_dyn_any(cont) };
220 catch (Exception ex)
222 throw new Unexpected(ex);
225 valueChanged();
228 /** @inheritDoc */
229 public Any get_boxed_value()
230 throws InvalidValue
234 if (is_null())
235 throw new InvalidValue(ISNULL);
236 else
237 return array [ 0 ].to_any();
239 catch (Exception e)
241 InvalidValue t = new InvalidValue();
242 t.initCause(e);
243 throw t;
247 /** @inheritDoc */
248 public DynAny get_boxed_value_as_dyn_any()
249 throws InvalidValue
251 if (is_null())
252 throw new InvalidValue(ISNULL);
253 else
254 return array [ 0 ].copy();
257 /** {@inheritDoc} */
258 public Serializable get_val()
259 throws TypeMismatch, InvalidValue
261 return to_any().extract_Value();
264 /** {@inheritDoc} */
265 public void insert_val(Serializable a_x)
266 throws InvalidValue, TypeMismatch
268 Any a = to_any();
269 a.insert_Value(a_x);
270 from_any(a);
271 valueChanged();
274 /** @inheritDoc */
275 public boolean is_null()
277 return array.length == 0;
280 /** @inheritDoc */
281 public void set_boxed_value(Any boxIt)
282 throws TypeMismatch
284 if (!content.equal(boxIt.type()))
285 throw new TypeMismatch(CONTENT);
288 if (is_null())
290 array = new DynAny[] { factory.create_dyn_any(boxIt) };
292 else
294 array [ 0 ].from_any(boxIt);
297 catch (Exception e)
299 TypeMismatch t = new TypeMismatch();
300 t.initCause(e);
301 throw t;
303 valueChanged();
306 /** @inheritDoc */
307 public void set_boxed_value_as_dyn_any(DynAny boxIt)
308 throws TypeMismatch
310 if (!content.equal(boxIt.type()))
311 throw new TypeMismatch(CONTENT);
314 if (is_null())
316 array = new DynAny[] { boxIt.copy() };
318 else
320 array [ 0 ].assign(boxIt);
323 catch (Exception e)
325 TypeMismatch t = new TypeMismatch();
326 t.initCause(e);
327 throw t;
329 valueChanged();
332 /** @inheritDoc */
333 public void set_to_null()
335 array = new DynAny[ 0 ];
336 valueChanged();
339 /** @inheritDoc */
340 public void set_to_value()
344 if (array.length == 0)
346 array =
347 new DynAny[] { factory.create_dyn_any_from_type_code(content) };
350 catch (InconsistentTypeCode e)
352 throw new Unexpected(e);
354 valueChanged();
357 /** @inheritDoc */
358 public Any to_any()
360 Any a = createAny();
362 if (!is_null())
366 Streamable holder;
367 if (array [ 0 ] instanceof gnuDynAny)
368 holder = ((gnuDynAny) array [ 0 ]).holder;
369 else
371 Any uan = array [ 0 ].to_any();
372 holder = uan.extract_Streamable();
375 Field v = holder.getClass().getField("value");
376 Serializable value = (Serializable) v.get(holder);
377 a.type(official_type);
378 a.insert_Value(value, content);
380 catch (Exception ex)
382 throw new Unexpected(ex);
385 else
386 a.type(orb.get_primitive_tc(TCKind.tk_null));
387 return a;