1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59:
60: import ;
61:
62:
67: public class gnuDynValueBox
68: extends DivideableAny
69: implements DynValueBox, Serializable
70: {
71:
74: private static final long serialVersionUID = 1;
75:
76:
79: final TypeCode content;
80:
81:
84: String CONTENT = "Box content final_type mismatch";
85:
86:
89: public gnuDynValueBox(TypeCode oType, TypeCode aType,
90: gnuDynAnyFactory aFactory, ORB anOrb
91: )
92: {
93: super(oType, aType, aFactory, anOrb);
94: try
95: {
96: content = final_type.content_type();
97: array = new DynAny[] { factory.create_dyn_any_from_type_code(content) };
98: set_to_null();
99: }
100: catch (Exception e)
101: {
102: throw new Unexpected(e);
103: }
104: }
105:
106:
107: public void assign(DynAny from)
108: throws TypeMismatch
109: {
110: checkType(official_type, from.type());
111: if (from instanceof DynValueBoxOperations)
112: {
113: DynValueBoxOperations other = (DynValueBoxOperations) from;
114: if (other.is_null())
115: set_to_null();
116: else
117: {
118: DynAny inBox;
119: try
120: {
121: inBox = other.get_boxed_value_as_dyn_any();
122: }
123: catch (InvalidValue e)
124: {
125: TypeMismatch t = new TypeMismatch("Invalid value");
126: t.initCause(e);
127: throw t;
128: }
129: if (!content.equal(inBox.type()))
130: throw new TypeMismatch(CONTENT);
131: array = new DynAny[] { inBox.copy() };
132: }
133: }
134: valueChanged();
135: }
136:
137:
138: public DynAny copy()
139: {
140: gnuDynValueBox other =
141: new gnuDynValueBox(official_type, final_type, factory, orb);
142: if (is_null())
143: other.set_to_null();
144: else
145: {
146: try
147: {
148: other.array = new DynAny[] { array [ 0 ].copy() };
149: }
150: catch (Exception e)
151: {
152: throw new Unexpected(e);
153: }
154: }
155: return other;
156: }
157:
158:
161: public DynAny current_component()
162: throws TypeMismatch
163: {
164: if (is_null())
165: return null;
166: else
167: return super.current_component();
168: }
169:
170:
173: public boolean equal(DynAny other)
174: {
175: if (other instanceof DynValueCommon)
176: {
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);
182: }
183: else
184: return false;
185: }
186:
187:
188: public void from_any(Any an_any)
189: throws TypeMismatch, InvalidValue
190: {
191: checkType(official_type, an_any.type());
192: try
193: {
194: if (!an_any.type().content_type().equal(content))
195: throw new InvalidValue(CONTENT);
196: }
197: catch (BadKind e)
198: {
199: TypeMismatch t = new TypeMismatch("Not a box");
200: t.initCause(e);
201: throw t;
202: }
203:
204: Serializable s = an_any.extract_Value();
205: if (s == null)
206: set_to_null();
207: else
208: {
209: try
210: {
211: Streamable holder = HolderLocator.createHolder(content);
212: Field v = holder.getClass().getField("value");
213: v.set(holder, s);
214:
215: Any cont = createAny();
216: cont.insert_Streamable(holder);
217:
218: array = new DynAny[] { factory.create_dyn_any(cont) };
219: }
220: catch (Exception ex)
221: {
222: throw new Unexpected(ex);
223: }
224: }
225: valueChanged();
226: }
227:
228:
229: public Any get_boxed_value()
230: throws InvalidValue
231: {
232: try
233: {
234: if (is_null())
235: throw new InvalidValue(ISNULL);
236: else
237: return array [ 0 ].to_any();
238: }
239: catch (Exception e)
240: {
241: InvalidValue t = new InvalidValue();
242: t.initCause(e);
243: throw t;
244: }
245: }
246:
247:
248: public DynAny get_boxed_value_as_dyn_any()
249: throws InvalidValue
250: {
251: if (is_null())
252: throw new InvalidValue(ISNULL);
253: else
254: return array [ 0 ].copy();
255: }
256:
257:
258: public Serializable get_val()
259: throws TypeMismatch, InvalidValue
260: {
261: return to_any().extract_Value();
262: }
263:
264:
265: public void insert_val(Serializable a_x)
266: throws InvalidValue, TypeMismatch
267: {
268: Any a = to_any();
269: a.insert_Value(a_x);
270: from_any(a);
271: valueChanged();
272: }
273:
274:
275: public boolean is_null()
276: {
277: return array.length == 0;
278: }
279:
280:
281: public void set_boxed_value(Any boxIt)
282: throws TypeMismatch
283: {
284: if (!content.equal(boxIt.type()))
285: throw new TypeMismatch(CONTENT);
286: try
287: {
288: if (is_null())
289: {
290: array = new DynAny[] { factory.create_dyn_any(boxIt) };
291: }
292: else
293: {
294: array [ 0 ].from_any(boxIt);
295: }
296: }
297: catch (Exception e)
298: {
299: TypeMismatch t = new TypeMismatch();
300: t.initCause(e);
301: throw t;
302: }
303: valueChanged();
304: }
305:
306:
307: public void set_boxed_value_as_dyn_any(DynAny boxIt)
308: throws TypeMismatch
309: {
310: if (!content.equal(boxIt.type()))
311: throw new TypeMismatch(CONTENT);
312: try
313: {
314: if (is_null())
315: {
316: array = new DynAny[] { boxIt.copy() };
317: }
318: else
319: {
320: array [ 0 ].assign(boxIt);
321: }
322: }
323: catch (Exception e)
324: {
325: TypeMismatch t = new TypeMismatch();
326: t.initCause(e);
327: throw t;
328: }
329: valueChanged();
330: }
331:
332:
333: public void set_to_null()
334: {
335: array = new DynAny[ 0 ];
336: valueChanged();
337: }
338:
339:
340: public void set_to_value()
341: {
342: try
343: {
344: if (array.length == 0)
345: {
346: array =
347: new DynAny[] { factory.create_dyn_any_from_type_code(content) };
348: }
349: }
350: catch (InconsistentTypeCode e)
351: {
352: throw new Unexpected(e);
353: }
354: valueChanged();
355: }
356:
357:
358: public Any to_any()
359: {
360: Any a = createAny();
361:
362: if (!is_null())
363: {
364: try
365: {
366: Streamable holder;
367: if (array [ 0 ] instanceof gnuDynAny)
368: holder = ((gnuDynAny) array [ 0 ]).holder;
369: else
370: {
371: Any uan = array [ 0 ].to_any();
372: holder = uan.extract_Streamable();
373: }
374:
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);
379: }
380: catch (Exception ex)
381: {
382: throw new Unexpected(ex);
383: }
384: }
385: else
386: a.type(orb.get_primitive_tc(TCKind.tk_null));
387: return a;
388: }
389: }