KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > IDL3 > AnyValue


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Mathieu Vadet.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.IDL3;
28
29 /**
30  * This class manages the constant or union case value.
31  *
32  * @author <a=href="Philippe.Merle@lifl.fr">Philippe Merle</a>
33  * <a=href="Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
34  *
35  * @version 0.3
36  */

37
38 public class AnyValue
39 {
40     // ==================================================================
41
//
42
// Internal state.
43
//
44
// ==================================================================
45

46     /**
47      ** The type of the constant.
48      **/

49     private int type_;
50
51     /**
52      ** The value of the constant.
53      **/

54     private long integer_;
55     private String JavaDoc string_;
56     private char char_;
57     private String JavaDoc fixed_;
58     private double floating_;
59     private boolean boolean_;
60     private org.omg.CORBA.TypeCode JavaDoc tc_;
61     private int enumerator_;
62
63     // ==================================================================
64
//
65
// Kind of type.
66
//
67
// ==================================================================
68

69     /**
70      ** Represent unknown type for the Any value.
71      **/

72     final public int NONE = 0;
73
74     /**
75      ** Represent integer type for the Any value.
76      **/

77     final public int INTEGER = 1;
78
79     /**
80      ** Represent string type for the Any value.
81      **/

82     final public int STRING = 2;
83
84     /**
85      ** Represent wide string type for the Any value.
86      **/

87     final public int WSTRING = 3;
88
89     /**
90      ** Represent character type for the Any value.
91      **/

92     final public int CHAR = 4;
93
94     /**
95      ** Represent wide character type for the Any value.
96      **/

97     final public int WCHAR = 5;
98
99     /**
100      ** Represent fixed type for the Any value.
101      **/

102     final public int FIXED = 6;
103
104     /**
105      ** Represent float type for the Any value.
106      **/

107     final public int FLOATING = 7;
108
109     /**
110      ** Represent boolean type for the Any value.
111      **/

112     final public int BOOLEAN = 8;
113
114     /**
115      ** Represent enumerate type for the Any value.
116      **/

117     final public int ENUM = 9;
118
119     // ==================================================================
120
//
121
// Constructor.
122
//
123
// ==================================================================
124

125     /**
126      ** The default constructor.
127      **/

128     public
129     AnyValue()
130     {
131         type_ = NONE;
132     }
133
134     // ==================================================================
135
//
136
// Internal methods.
137
//
138
// ==================================================================
139

140     /**
141      ** Obtain the any value.
142      **
143      ** @param tc The TypeCode of the desired any value.
144      ** Note that one of the set* method should have been used
145      ** to fix the value before using this method.
146      ** If the TypeCode and the set* method are not consistent,
147      ** an Error is thrown.
148      **
149      ** @return An Any created from the TypeCode.
150      **/

151     protected org.omg.CORBA.Any JavaDoc
152     computeAny(org.omg.CORBA.TypeCode JavaDoc tc)
153     {
154         if(type_ == NONE)
155         {
156             org.omg.CORBA.Any JavaDoc any =
157                 org.objectweb.openccm.corba.TheORB.create_any();
158             any.insert_octet((byte)0);
159             return any;
160         }
161
162         org.omg.CORBA.TypeCode JavaDoc origTC =
163             org.objectweb.openccm.corba.TypeCodeUtils.getOrigType(tc);
164         org.omg.DynamicAny.DynAny JavaDoc dynAny =
165             org.objectweb.openccm.corba.TheDynamicAnyFactory.
166             create_dyn_any_from_type_code(origTC);
167
168         try
169         {
170             switch(origTC.kind().value())
171             {
172             case org.omg.CORBA.TCKind._tk_short:
173                 if(type_ != INTEGER)
174                 throw new Error JavaDoc("Invalid constant value!");
175                 dynAny.insert_short((short)integer_);
176                 break;
177
178             case org.omg.CORBA.TCKind._tk_long:
179                 if(type_ != INTEGER)
180                 throw new Error JavaDoc("Invalid constant value!");
181                 dynAny.insert_long((int)integer_);
182                 break;
183
184             case org.omg.CORBA.TCKind._tk_ushort:
185                 if(type_ != INTEGER)
186                 throw new Error JavaDoc("Invalid constant value!");
187                 dynAny.insert_ushort((short)integer_);
188                 break;
189
190             case org.omg.CORBA.TCKind._tk_ulong:
191                 if(type_ != INTEGER)
192                 throw new Error JavaDoc("Invalid constant value!");
193                 dynAny.insert_ulong((int)integer_);
194                 break;
195
196             case org.omg.CORBA.TCKind._tk_float:
197                 if(type_ != FLOATING)
198                 throw new Error JavaDoc("Invalid constant value!");
199                 dynAny.insert_float((float)floating_);
200                 break;
201
202             case org.omg.CORBA.TCKind._tk_double:
203                 if(type_ != FLOATING)
204                 throw new Error JavaDoc("Invalid constant value!");
205                 dynAny.insert_double(floating_);
206                 break;
207
208             case org.omg.CORBA.TCKind._tk_boolean:
209                 if(type_ != BOOLEAN)
210                 throw new Error JavaDoc("Invalid constant value!");
211                 dynAny.insert_boolean(boolean_);
212                 break;
213
214             case org.omg.CORBA.TCKind._tk_char:
215                 if(type_ != CHAR)
216                 throw new Error JavaDoc("Invalid constant value!");
217                 dynAny.insert_char(char_);
218                 break;
219
220             case org.omg.CORBA.TCKind._tk_string:
221                 if(type_ != STRING)
222                 throw new Error JavaDoc("Invalid constant value!");
223                 dynAny.insert_string(string_);
224                 break;
225
226             case org.omg.CORBA.TCKind._tk_longlong:
227                 if(type_ != INTEGER)
228                 throw new Error JavaDoc("Invalid constant value!");
229                 dynAny.insert_longlong(integer_);
230                 break;
231
232             case org.omg.CORBA.TCKind._tk_ulonglong:
233                 if(type_ != INTEGER)
234                 throw new Error JavaDoc("Invalid constant value!");
235                 dynAny.insert_ulonglong(integer_);
236                 break;
237
238             case org.omg.CORBA.TCKind._tk_wchar:
239                 if(type_ != WCHAR)
240                 throw new Error JavaDoc("Invalid constant value!");
241                 dynAny.insert_wchar(char_);
242                 break;
243
244             case org.omg.CORBA.TCKind._tk_wstring:
245                 if(type_ != WSTRING)
246                 throw new Error JavaDoc("Invalid constant value!");
247                 dynAny.insert_wstring(string_);
248                 break;
249
250             case org.omg.CORBA.TCKind._tk_fixed:
251                 if(type_ != FIXED)
252                 throw new Error JavaDoc("Invalid constant value!");
253                 // TODO: Must be wrapped for JavaORB.
254
org.omg.DynamicAny.DynFixedHelper.narrow(dynAny).set_value(fixed_);
255                 break;
256
257             case org.omg.CORBA.TCKind._tk_enum:
258                 if(type_ != ENUM)
259                 throw new Error JavaDoc("Invalid constant value!");
260                 if(!tc_.equal(origTC))
261                 throw new Error JavaDoc("Invalid constant type!");
262                 org.omg.DynamicAny.DynEnumHelper.narrow(dynAny).set_as_ulong(enumerator_);
263                 break;
264
265             default:
266             throw new Error JavaDoc("Invalid constant type!");
267             }
268             return dynAny.to_any();
269         }
270         catch(org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc exc)
271         {
272             // Wrap the DynamicAny::InvalidValue exception.
273
throw new org.objectweb.openccm.corba.UserExceptionWrapper(exc);
274         }
275         catch(org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc exc)
276         {
277             // Wrap the DynamicAny::TypeMismatch exception.
278
throw new org.objectweb.openccm.corba.UserExceptionWrapper(exc);
279         }
280     }
281
282     /**
283      **
284      **/

285     protected void
286     loadAny(org.omg.CORBA.Any JavaDoc any)
287     {
288         org.omg.CORBA.TypeCode JavaDoc tc = any.type();
289         try
290         {
291             switch(tc.kind().value())
292             {
293             case org.omg.CORBA.TCKind._tk_short:
294                 setAsInteger(any.extract_short());
295                 break;
296             case org.omg.CORBA.TCKind._tk_long:
297                 setAsInteger(any.extract_long());
298                 break;
299             case org.omg.CORBA.TCKind._tk_ushort:
300                 setAsInteger(any.extract_ushort());
301                 break;
302             case org.omg.CORBA.TCKind._tk_ulong:
303                 setAsInteger(any.extract_ulong());
304                 break;
305             case org.omg.CORBA.TCKind._tk_float:
306                 setAsFloating(any.extract_float());
307                 break;
308             case org.omg.CORBA.TCKind._tk_double:
309                 setAsFloating(any.extract_double());
310                 break;
311             case org.omg.CORBA.TCKind._tk_boolean:
312                 setAsBoolean(any.extract_boolean());
313                 break;
314             case org.omg.CORBA.TCKind._tk_char:
315                 setAsChar(any.extract_char());
316                 break;
317             case org.omg.CORBA.TCKind._tk_wchar:
318                 setAsWChar(any.extract_wchar());
319                 break;
320             case org.omg.CORBA.TCKind._tk_string:
321                 setAsString(any.extract_string());
322                 break;
323             case org.omg.CORBA.TCKind._tk_wstring:
324                 setAsWString(any.extract_wstring());
325                 break;
326             case org.omg.CORBA.TCKind._tk_longlong:
327                 setAsInteger(any.extract_longlong());
328                 break;
329             case org.omg.CORBA.TCKind._tk_ulonglong:
330                 setAsInteger(any.extract_ulonglong());
331                 break;
332             case org.omg.CORBA.TCKind._tk_fixed:
333                 org.omg.DynamicAny.DynFixed JavaDoc fixed =
334                     org.objectweb.openccm.corba.TheDynamicAnyFactory.create_dyn_fixed(any);
335                 setAsFixed(fixed.get_value());
336                 break;
337             case org.omg.CORBA.TCKind._tk_enum:
338                 org.omg.DynamicAny.DynEnum JavaDoc dynEnum =
339                     org.objectweb.openccm.corba.TheDynamicAnyFactory.create_dyn_enum(any);
340                 setAsEnum(tc, dynEnum.get_as_ulong(), dynEnum.get_as_string());
341                 break;
342             default:
343                 break;
344             }
345         }
346         catch(org.omg.CORBA.BAD_OPERATION JavaDoc ex)
347         {}
348     }
349
350     // ==================================================================
351
//
352
// Methods for the AnyValue interface.
353
//
354
// ==================================================================
355

356     /**
357      ** Is it an integer?
358      **
359      ** @return true if it's a long or false otherwise.
360      **/

361     public boolean
362     isInteger()
363     {
364         return type_ == INTEGER;
365     }
366
367     /**
368      ** Set with an integer.
369      **
370      ** @param value The long to set as value.
371      **/

372     public void
373     setAsInteger(long value)
374     {
375         type_ = INTEGER;
376         integer_ = value;
377     }
378
379     /**
380      ** Get as an integer.
381      **
382      ** @return The long that must have been previously set.
383      **/

384     public long
385     getAsInteger()
386     {
387         return integer_;
388     }
389
390     /**
391      ** Is it an string?
392      **
393      ** @return true if it's a string or false otherwise.
394      **/

395     public boolean
396     isString()
397     {
398         return type_ == STRING;
399     }
400
401     /**
402      ** Set with a string.
403      **
404      ** @param value The String to set as value.
405      **/

406     public void
407     setAsString(String JavaDoc value)
408     {
409         type_ = STRING;
410         string_ = value;
411     }
412
413     /**
414      ** Get as a string.
415      **
416      ** @return The string that must have been previously set.
417      **/

418     public String JavaDoc
419     getAsString()
420     {
421         return string_;
422     }
423
424     /**
425      ** Is it an wstring?
426      **
427      ** @return true if it's a wstring or false otherwise.
428      **/

429     public boolean
430     isWString()
431     {
432         return type_ == WSTRING;
433     }
434
435     /**
436      ** Set with a wstring.
437      **
438      ** @param value The String to set as value.
439      **/

440     public void
441     setAsWString(String JavaDoc value)
442     {
443         type_ = WSTRING;
444         string_ = value;
445     }
446
447     /**
448      ** Get as a wstring.
449      **
450      ** @return The wstring that must have been previously set.
451      **/

452     public String JavaDoc
453     getAsWString()
454     {
455         return string_;
456     }
457
458     /**
459      ** Is it an char?
460      **
461      ** @return true if it's a char or false otherwise.
462      **/

463     public boolean
464     isChar()
465     {
466         return type_ == CHAR;
467     }
468
469     /**
470      ** Set with a char.
471      **
472      ** @param value The char to set as value.
473      **/

474     public void
475     setAsChar(char value)
476     {
477         type_ = CHAR;
478         char_ = value;
479     }
480
481     /**
482      ** Get as a char.
483      **
484      ** @return The char that must have been previously set.
485      **/

486     public char
487     getAsChar()
488     {
489         return char_;
490     }
491
492     /**
493      ** Is it an wchar?
494      **
495      ** @return true if it's a wchar or false otherwise.
496      **/

497     public boolean
498     isWChar()
499     {
500         return type_ == WCHAR;
501     }
502
503     /**
504      ** Set with a wchar.
505      **
506      ** @param value The char to set as value.
507      **/

508     public void
509     setAsWChar(char value)
510     {
511         type_ = WCHAR;
512         char_ = value;
513     }
514
515     /**
516      ** Get as a wchar.
517      **
518      ** @return The wchar that must have been previously set.
519      **/

520     public char
521     getAsWChar()
522     {
523         return char_;
524     }
525
526     /**
527      ** Is it an fixed?
528      **
529      ** @return true if it's a fixed or false otherwise.
530      **/

531     public boolean
532     isFixed()
533     {
534         return type_ == FIXED;
535     }
536
537     /**
538      ** Set with a fixed.
539      **
540      ** @param value The String to set as value.
541      **/

542     public void
543     setAsFixed(String JavaDoc value)
544     {
545         type_ = FIXED;
546         fixed_ = value;
547     }
548
549     /**
550      ** Get as a fixed.
551      **
552      ** @return The string that must have been previously set.
553      **/

554     public String JavaDoc
555     getAsFixed()
556     {
557         return fixed_;
558     }
559
560     /**
561      ** Is it a floating?
562      **
563      ** @return true if it's a float or false otherwise.
564      **/

565     public boolean
566     isFloating()
567     {
568         return type_ == FLOATING;
569     }
570
571     /**
572      ** Set with a double.
573      **
574      ** @param value The double to set as value.
575      **/

576     public void
577     setAsFloating(double value)
578     {
579         type_ = FLOATING;
580         floating_ = value;
581     }
582
583     /**
584      ** Get as a double.
585      **
586      ** @return The double that must have been previously set.
587      **/

588     public double
589     getAsFloating()
590     {
591         return floating_;
592     }
593
594     /**
595      ** Is it a boolean?
596      **
597      ** @return true if it's a boolean or false otherwise.
598      **/

599     public boolean
600     isBoolean()
601     {
602         return type_ == BOOLEAN;
603     }
604
605     /**
606      ** Set with a boolean.
607      **
608      ** @param value The boolean to set as value.
609      **/

610     public void
611     setAsBoolean(boolean value)
612     {
613         type_ = BOOLEAN;
614         boolean_ = value;
615     }
616
617     /**
618      ** Get as a boolean.
619      **
620      ** @return The boolean that must have been previously set.
621      **/

622     public boolean
623     getAsBoolean()
624     {
625         return boolean_;
626     }
627
628     /**
629      ** Is it a enum?
630      **
631      ** @return true if it's a enum or false otherwise.
632      **/

633     public boolean
634     isEnum()
635     {
636         return type_ == ENUM;
637     }
638
639     /**
640      ** Set with an enum.
641      **
642      ** @param value The long to set as value.
643      **/

644     public void
645     setAsEnum(org.omg.CORBA.TypeCode JavaDoc tc,
646               int value,
647               String JavaDoc name)
648     {
649         type_ = ENUM;
650         tc_ = tc;
651         enumerator_ = value;
652         string_ = name;
653     }
654
655     /**
656      ** Get as a enum.
657      **
658      ** @return The string that must have been previously set.
659      **/

660     public String JavaDoc
661     getAsEnum()
662     {
663         return string_;
664     }
665
666 }
667
Popular Tags