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)
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
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
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
;
63 * Implementation of the DynValueBox.
65 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
67 public class gnuDynValueBox
69 implements DynValueBox
, Serializable
72 * Use serialVersionUID for interoperability.
74 private static final long serialVersionUID
= 1;
77 * The final_type of contents of this value box.
79 final TypeCode content
;
82 * The string for some TypeMismatch exceptions.
84 String CONTENT
= "Box content final_type mismatch";
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
);
96 content
= final_type
.content_type();
97 array
= new DynAny
[] { factory
.create_dyn_any_from_type_code(content
) };
102 throw new Unexpected(e
);
107 public void assign(DynAny from
)
110 checkType(official_type
, from
.type());
111 if (from
instanceof DynValueBoxOperations
)
113 DynValueBoxOperations other
= (DynValueBoxOperations
) from
;
121 inBox
= other
.get_boxed_value_as_dyn_any();
123 catch (InvalidValue e
)
125 TypeMismatch t
= new TypeMismatch("Invalid value");
129 if (!content
.equal(inBox
.type()))
130 throw new TypeMismatch(CONTENT
);
131 array
= new DynAny
[] { inBox
.copy() };
140 gnuDynValueBox other
=
141 new gnuDynValueBox(official_type
, final_type
, factory
, orb
);
148 other
.array
= new DynAny
[] { array
[ 0 ].copy() };
152 throw new Unexpected(e
);
159 * Returns null for null value, delegates to super. otherwise.
161 public DynAny
current_component()
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
;
179 return o
.is_null() && o
.type().equal(official_type
);
181 return !o
.is_null() && super.equal(other
);
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
);
199 TypeMismatch t
= new TypeMismatch("Not a box");
204 Serializable s
= an_any
.extract_Value();
211 Streamable holder
= HolderLocator
.createHolder(content
);
212 Field v
= holder
.getClass().getField("value");
215 Any cont
= createAny();
216 cont
.insert_Streamable(holder
);
218 array
= new DynAny
[] { factory
.create_dyn_any(cont
) };
222 throw new Unexpected(ex
);
229 public Any
get_boxed_value()
235 throw new InvalidValue(ISNULL
);
237 return array
[ 0 ].to_any();
241 InvalidValue t
= new InvalidValue();
248 public DynAny
get_boxed_value_as_dyn_any()
252 throw new InvalidValue(ISNULL
);
254 return array
[ 0 ].copy();
258 public Serializable
get_val()
259 throws TypeMismatch
, InvalidValue
261 return to_any().extract_Value();
265 public void insert_val(Serializable a_x
)
266 throws InvalidValue
, TypeMismatch
275 public boolean is_null()
277 return array
.length
== 0;
281 public void set_boxed_value(Any boxIt
)
284 if (!content
.equal(boxIt
.type()))
285 throw new TypeMismatch(CONTENT
);
290 array
= new DynAny
[] { factory
.create_dyn_any(boxIt
) };
294 array
[ 0 ].from_any(boxIt
);
299 TypeMismatch t
= new TypeMismatch();
307 public void set_boxed_value_as_dyn_any(DynAny boxIt
)
310 if (!content
.equal(boxIt
.type()))
311 throw new TypeMismatch(CONTENT
);
316 array
= new DynAny
[] { boxIt
.copy() };
320 array
[ 0 ].assign(boxIt
);
325 TypeMismatch t
= new TypeMismatch();
333 public void set_to_null()
335 array
= new DynAny
[ 0 ];
340 public void set_to_value()
344 if (array
.length
== 0)
347 new DynAny
[] { factory
.create_dyn_any_from_type_code(content
) };
350 catch (InconsistentTypeCode e
)
352 throw new Unexpected(e
);
367 if (array
[ 0 ] instanceof gnuDynAny
)
368 holder
= ((gnuDynAny
) array
[ 0 ]).holder
;
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
);
382 throw new Unexpected(ex
);
386 a
.type(orb
.get_primitive_tc(TCKind
.tk_null
));