KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > pse > offers > AnyValue


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.db.pse.offers;
15
16 import org.omg.CORBA.*;
17
18 public class AnyValue
19 {
20   private TypeCodeValue m_type;
21   private java.lang.Object JavaDoc m_value;
22
23
24   private AnyValue()
25   {
26   }
27
28
29   public AnyValue(Any any)
30   {
31     setValue(any);
32   }
33
34
35   /** Returns true if we can store a value of the given type */
36   public static boolean isTypeSupported(org.omg.CORBA.TypeCode JavaDoc type)
37   {
38     boolean result = false;
39
40     try {
41       TypeCode tc = type;
42
43       while (tc.kind() == TCKind.tk_alias)
44         tc = tc.content_type();
45
46       TCKind kind = tc.kind();
47
48       if (kind == TCKind.tk_null || kind == TCKind.tk_void)
49         result = true;
50       else {
51         if (kind == TCKind.tk_sequence) {
52           TypeCode contentType = tc.content_type();
53           kind = contentType.kind();
54         }
55
56         switch (kind.value()) {
57           case TCKind._tk_short:
58           case TCKind._tk_long:
59           case TCKind._tk_ushort:
60           case TCKind._tk_ulong:
61           case TCKind._tk_float:
62           case TCKind._tk_double:
63           case TCKind._tk_boolean:
64           case TCKind._tk_char:
65           case TCKind._tk_string:
66           case TCKind._tk_octet:
67             result = true;
68             break;
69         }
70       }
71     }
72     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
73       // ignore
74
}
75     catch (BAD_OPERATION e) {
76       // ignore
77
}
78
79     return result;
80   }
81
82
83   public Any getValue()
84   {
85     ORBLayer orbLayer = ORBLayer.instance();
86     ORB orb = orbLayer.getORB();
87     Any result = null;
88
89     TypeCode origType = m_type.getValue();
90
91     boolean isSequence = false;
92
93     try {
94       TypeCode tc = origType;
95       while (tc.kind() == TCKind.tk_alias)
96         tc = tc.content_type();
97
98       TCKind kind = tc.kind();
99
100       if (kind == TCKind.tk_sequence) {
101         TypeCode content = tc.content_type();
102         kind = content.kind();
103         isSequence = true;
104       }
105
106       switch (kind.value()) {
107         case TCKind._tk_null:
108         case TCKind._tk_void:
109           result = orb.create_any();
110           result.type(origType);
111           break;
112
113         case TCKind._tk_short: {
114             if (isSequence) {
115               short[] arr = (short[])m_value;
116               DynSequence ds = orbLayer.create_dyn_sequence(origType);
117               ds.length(arr.length);
118               for (int i = 0; i < arr.length; i++)
119                 ds.insert_short(arr[i]);
120               result = ds.to_any();
121             }
122             else {
123               Short JavaDoc val = (Short JavaDoc)m_value;
124               DynAny da = orbLayer.create_basic_dyn_any(origType);
125               da.insert_short(val.shortValue());
126               result = da.to_any();
127             }
128           }
129           break;
130
131         case TCKind._tk_long: {
132             if (isSequence) {
133               int[] arr = (int[])m_value;
134               DynSequence ds = orbLayer.create_dyn_sequence(origType);
135               ds.length(arr.length);
136               for (int i = 0; i < arr.length; i++)
137                 ds.insert_long(arr[i]);
138               result = ds.to_any();
139             }
140             else {
141               Integer JavaDoc val = (Integer JavaDoc)m_value;
142               DynAny da = orbLayer.create_basic_dyn_any(origType);
143               da.insert_long(val.intValue());
144               result = da.to_any();
145             }
146           }
147           break;
148
149         case TCKind._tk_ushort: {
150             if (isSequence) {
151               short[] arr = (short[])m_value;
152               DynSequence ds = orbLayer.create_dyn_sequence(origType);
153               ds.length(arr.length);
154               for (int i = 0; i < arr.length; i++)
155                 ds.insert_ushort(arr[i]);
156               result = ds.to_any();
157             }
158             else {
159               Short JavaDoc val = (Short JavaDoc)m_value;
160               DynAny da = orbLayer.create_basic_dyn_any(origType);
161               da.insert_ushort(val.shortValue());
162               result = da.to_any();
163             }
164           }
165           break;
166
167         case TCKind._tk_ulong: {
168             if (isSequence) {
169               int[] arr = (int[])m_value;
170               DynSequence ds = orbLayer.create_dyn_sequence(origType);
171               ds.length(arr.length);
172               for (int i = 0; i < arr.length; i++)
173                 ds.insert_ulong(arr[i]);
174               result = ds.to_any();
175             }
176             else {
177               Integer JavaDoc val = (Integer JavaDoc)m_value;
178               DynAny da = orbLayer.create_basic_dyn_any(origType);
179               da.insert_ulong(val.intValue());
180               result = da.to_any();
181             }
182           }
183           break;
184
185         case TCKind._tk_float: {
186             if (isSequence) {
187               float[] arr = (float[])m_value;
188               DynSequence ds = orbLayer.create_dyn_sequence(origType);
189               ds.length(arr.length);
190               for (int i = 0; i < arr.length; i++)
191                 ds.insert_float(arr[i]);
192               result = ds.to_any();
193             }
194             else {
195               Float JavaDoc val = (Float JavaDoc)m_value;
196               DynAny da = orbLayer.create_basic_dyn_any(origType);
197               da.insert_float(val.floatValue());
198               result = da.to_any();
199             }
200           }
201           break;
202
203         case TCKind._tk_double: {
204             if (isSequence) {
205               double[] arr = (double[])m_value;
206               DynSequence ds = orbLayer.create_dyn_sequence(origType);
207               ds.length(arr.length);
208               for (int i = 0; i < arr.length; i++)
209                 ds.insert_double(arr[i]);
210               result = ds.to_any();
211             }
212             else {
213               Double JavaDoc val = (Double JavaDoc)m_value;
214               DynAny da = orbLayer.create_basic_dyn_any(origType);
215               da.insert_double(val.doubleValue());
216               result = da.to_any();
217             }
218           }
219           break;
220
221         case TCKind._tk_boolean: {
222             if (isSequence) {
223               boolean[] arr = (boolean[])m_value;
224               DynSequence ds = orbLayer.create_dyn_sequence(origType);
225               ds.length(arr.length);
226               for (int i = 0; i < arr.length; i++)
227                 ds.insert_boolean(arr[i]);
228               result = ds.to_any();
229             }
230             else {
231               Boolean JavaDoc val = (Boolean JavaDoc)m_value;
232               DynAny da = orbLayer.create_basic_dyn_any(origType);
233               da.insert_boolean(val.booleanValue());
234               result = da.to_any();
235             }
236           }
237           break;
238
239         case TCKind._tk_char: {
240             if (isSequence) {
241               char[] arr = (char[])m_value;
242               DynSequence ds = orbLayer.create_dyn_sequence(origType);
243               ds.length(arr.length);
244               for (int i = 0; i < arr.length; i++)
245                 ds.insert_char(arr[i]);
246               result = ds.to_any();
247             }
248             else {
249               Character JavaDoc val = (Character JavaDoc)m_value;
250               DynAny da = orbLayer.create_basic_dyn_any(origType);
251               da.insert_char(val.charValue());
252               result = da.to_any();
253             }
254           }
255           break;
256
257         case TCKind._tk_string: {
258             if (isSequence) {
259               String JavaDoc[] arr = (String JavaDoc[])m_value;
260               DynSequence ds = orbLayer.create_dyn_sequence(origType);
261               ds.length(arr.length);
262               for (int i = 0; i < arr.length; i++)
263                 ds.insert_string(arr[i]);
264               result = ds.to_any();
265             }
266             else {
267               String JavaDoc val = (String JavaDoc)m_value;
268               DynAny da = orbLayer.create_basic_dyn_any(origType);
269               da.insert_string(val);
270               result = da.to_any();
271             }
272           }
273           break;
274
275         case TCKind._tk_octet: {
276             if (isSequence) {
277               byte[] arr = (byte[])m_value;
278               DynSequence ds = orbLayer.create_dyn_sequence(origType);
279               ds.length(arr.length);
280               for (int i = 0; i < arr.length; i++)
281                 ds.insert_octet(arr[i]);
282               result = ds.to_any();
283             }
284             else {
285               Byte JavaDoc val = (Byte JavaDoc)m_value;
286               DynAny da = orbLayer.create_basic_dyn_any(origType);
287               da.insert_octet(val.byteValue());
288               result = da.to_any();
289             }
290           }
291           break;
292       }
293     }
294     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
295       throw new RuntimeException JavaDoc(e.getMessage());
296     }
297     catch (org.omg.CORBA.DynAnyPackage.InvalidValue JavaDoc e) {
298       throw new RuntimeException JavaDoc(e.getMessage());
299     }
300     catch (org.omg.CORBA.DynAnyPackage.Invalid JavaDoc e) {
301       throw new RuntimeException JavaDoc(e.getMessage());
302     }
303     catch (ORBLayer.InconsistentTypeCode e) {
304       throw new RuntimeException JavaDoc(e.getMessage());
305     }
306
307     return result;
308   }
309
310
311   protected void setValue(Any val)
312   {
313     ORBLayer orbLayer = ORBLayer.instance();
314     TypeCode origType = val.type();
315
316     m_type = new TypeCodeValue(origType);
317
318     boolean isSequence = false;
319
320     try {
321       TypeCode tc = origType;
322       while (tc.kind() == TCKind.tk_alias)
323         tc = tc.content_type();
324
325       TCKind kind = tc.kind();
326
327       if (kind == TCKind.tk_sequence) {
328         TypeCode content = tc.content_type();
329         kind = content.kind();
330         isSequence = true;
331       }
332
333       DynAny da = orbLayer.create_dyn_any(val);
334
335       switch (kind.value()) {
336         case TCKind._tk_null:
337           m_value = null;
338           break;
339
340         case TCKind._tk_void:
341           m_value = null;
342           break;
343
344         case TCKind._tk_short: {
345             if (isSequence) {
346               DynSequence ds = DynSequenceHelper.narrow(da);
347               int len = ds.length();
348               short[] arr = new short[len];
349               for (int i = 0; i < len; i++, ds.next())
350                 arr[i] = ds.get_short();
351               m_value = arr;
352             }
353             else
354               m_value = new Short JavaDoc(da.get_short());
355           }
356           break;
357
358         case TCKind._tk_long: {
359             if (isSequence) {
360               DynSequence ds = DynSequenceHelper.narrow(da);
361               int len = ds.length();
362               int[] arr = new int[len];
363               for (int i = 0; i < len; i++, ds.next())
364                 arr[i] = ds.get_long();
365               m_value = arr;
366             }
367             else
368               m_value = new Integer JavaDoc(da.get_long());
369           }
370           break;
371
372         case TCKind._tk_ushort: {
373             if (isSequence) {
374               DynSequence ds = DynSequenceHelper.narrow(da);
375               int len = ds.length();
376               short[] arr = new short[len];
377               for (int i = 0; i < len; i++, ds.next())
378                 arr[i] = ds.get_ushort();
379               m_value = arr;
380             }
381             else
382               m_value = new Short JavaDoc(da.get_ushort());
383           }
384           break;
385
386         case TCKind._tk_ulong: {
387             if (isSequence) {
388               DynSequence ds = DynSequenceHelper.narrow(da);
389               int len = ds.length();
390               int[] arr = new int[len];
391               for (int i = 0; i < len; i++, ds.next())
392                 arr[i] = ds.get_ulong();
393               m_value = arr;
394             }
395             else
396               m_value = new Integer JavaDoc(da.get_ulong());
397           }
398           break;
399
400         case TCKind._tk_float: {
401             if (isSequence) {
402               DynSequence ds = DynSequenceHelper.narrow(da);
403               int len = ds.length();
404               float[] arr = new float[len];
405               for (int i = 0; i < len; i++, ds.next())
406                 arr[i] = ds.get_float();
407               m_value = arr;
408             }
409             else
410               m_value = new Float JavaDoc(da.get_float());
411           }
412           break;
413
414         case TCKind._tk_double: {
415             if (isSequence) {
416               DynSequence ds = DynSequenceHelper.narrow(da);
417               int len = ds.length();
418               double[] arr = new double[len];
419               for (int i = 0; i < len; i++, ds.next())
420                 arr[i] = ds.get_double();
421               m_value = arr;
422             }
423             else
424               m_value = new Double JavaDoc(da.get_double());
425           }
426           break;
427
428         case TCKind._tk_boolean: {
429             if (isSequence) {
430               DynSequence ds = DynSequenceHelper.narrow(da);
431               int len = ds.length();
432               boolean[] arr = new boolean[len];
433               for (int i = 0; i < len; i++, ds.next())
434                 arr[i] = ds.get_boolean();
435               m_value = arr;
436             }
437             else
438               m_value = new Boolean JavaDoc(da.get_boolean());
439           }
440           break;
441
442         case TCKind._tk_char: {
443             if (isSequence) {
444               DynSequence ds = DynSequenceHelper.narrow(da);
445               int len = ds.length();
446               char[] arr = new char[len];
447               for (int i = 0; i < len; i++, ds.next())
448                 arr[i] = ds.get_char();
449               m_value = arr;
450             }
451             else
452               m_value = new Character JavaDoc(da.get_char());
453           }
454           break;
455
456         case TCKind._tk_string: {
457             if (isSequence) {
458               DynSequence ds = DynSequenceHelper.narrow(da);
459               int len = ds.length();
460               String JavaDoc[] arr = new String JavaDoc[len];
461               for (int i = 0; i < len; i++, ds.next())
462                 arr[i] = ds.get_string();
463               m_value = arr;
464             }
465             else
466               m_value = da.get_string();
467           }
468           break;
469
470         case TCKind._tk_octet: {
471             if (isSequence) {
472               DynSequence ds = DynSequenceHelper.narrow(da);
473               int len = ds.length();
474               byte[] arr = new byte[len];
475               for (int i = 0; i < len; i++, ds.next())
476                 arr[i] = ds.get_octet();
477               m_value = arr;
478             }
479             else
480               m_value = new Byte JavaDoc(da.get_octet());
481           }
482           break;
483
484         default:
485           throw new RuntimeException JavaDoc("Unsupported type");
486       }
487
488       da.destroy();
489     }
490     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
491       throw new RuntimeException JavaDoc(e.getMessage());
492     }
493     catch (org.omg.CORBA.DynAnyPackage.TypeMismatch JavaDoc e) {
494       throw new RuntimeException JavaDoc(e.getMessage());
495     }
496     catch (BAD_OPERATION e) {
497       throw new RuntimeException JavaDoc();
498     }
499   }
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
Popular Tags