KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > dynany > DynEnum


1 package org.jacorb.orb.dynany;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.DynamicAny.DynAnyPackage.*;
24 import org.omg.DynamicAny.*;
25 import org.jacorb.orb.*;
26
27 /**
28  * CORBA DynEnum
29  *
30  * @author (c) Gerald Brose, FU Berlin 1999
31  * $Id: DynEnum.java,v 1.15 2005/03/25 13:15:55 andre.spiegel Exp $
32  */

33
34 public final class DynEnum
35    extends DynAny
36    implements org.omg.DynamicAny.DynEnum JavaDoc
37 {
38    private int enum_value;
39    private int max;
40    private String JavaDoc [] member_names;
41
42     DynEnum( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
43            org.omg.CORBA.TypeCode JavaDoc tc,org.omg.CORBA.ORB JavaDoc orb)
44      throws InvalidValue, TypeMismatch
45   {
46      org.omg.CORBA.TypeCode JavaDoc _type = TypeCode.originalType( tc );
47
48      if( _type.kind().value() != org.omg.CORBA.TCKind._tk_enum )
49         throw new TypeMismatch();
50
51      type = _type;
52
53      this.orb = orb;
54      this.dynFactory = dynFactory;
55      pos = -1;
56      enum_value = 0;
57
58      try
59      {
60         member_names = new String JavaDoc[ type().member_count()];
61         max = member_names.length;
62         for( int i = 0; i < member_names.length; i++ )
63            member_names[i] = type().member_name(i);
64      }
65      catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
66      {
67         // should not happen
68
bk.printStackTrace();
69      }
70      catch( org.omg.CORBA.TypeCodePackage.Bounds JavaDoc b )
71      {
72         // should not happen
73
b.printStackTrace();
74      }
75   }
76      
77    DynEnum( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
78             org.omg.CORBA.TypeCode JavaDoc tc)
79       throws InvalidValue, TypeMismatch
80    {
81       org.omg.CORBA.TypeCode JavaDoc _type = TypeCode.originalType( tc );
82
83       if( _type.kind().value() != org.omg.CORBA.TCKind._tk_enum )
84          throw new TypeMismatch();
85
86       type = _type;
87
88       this.orb = org.omg.CORBA.ORB.init();
89       this.dynFactory = dynFactory;
90       pos = -1;
91       enum_value = 0;
92
93       try
94       {
95          member_names = new String JavaDoc[ type().member_count()];
96          max = member_names.length;
97          for( int i = 0; i < member_names.length; i++ )
98             member_names[i] = type().member_name(i);
99       }
100       catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
101       {
102          // should not happen
103
bk.printStackTrace();
104       }
105       catch( org.omg.CORBA.TypeCodePackage.Bounds JavaDoc b )
106       {
107          // should not happen
108
b.printStackTrace();
109       }
110    }
111
112
113    /**
114     * Overrides from_any() in DynAny
115     */

116
117    public void from_any( org.omg.CORBA.Any JavaDoc value )
118       throws InvalidValue, TypeMismatch
119    {
120       checkDestroyed ();
121       if( ! value.type().equivalent( type()) )
122          throw new TypeMismatch();
123
124       type = TypeCode.originalType( value.type() );
125
126       try
127       {
128          enum_value = value.create_input_stream().read_long();
129          member_names = new String JavaDoc[ type().member_count()];
130          max = member_names.length;
131          for( int i = 0; i < member_names.length; i++ )
132          {
133             member_names[i] = type().member_name(i);
134          }
135       }
136       catch( org.omg.CORBA.TypeCodePackage.Bounds JavaDoc b )
137       {
138          // should not happen
139
b.printStackTrace();
140       }
141       catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
142       {
143          // should not happen anymore
144
bk.printStackTrace();
145       }
146    }
147
148    /**
149     * Overrides equal() in DynAny
150     */

151
152    public boolean equal( org.omg.DynamicAny.DynAny JavaDoc dyn_any )
153    {
154       checkDestroyed ();
155       if( !type().equal( dyn_any.type()))
156          return false;
157
158       return DynEnumHelper.narrow( dyn_any).get_as_ulong() == get_as_ulong();
159    }
160
161
162    public org.omg.CORBA.Any JavaDoc to_any()
163    {
164       checkDestroyed ();
165       CDROutputStream os = new CDROutputStream();
166       os.write_long( enum_value );
167
168       org.jacorb.orb.Any out_any =
169          (org.jacorb.orb.Any)org.omg.CORBA.ORB.init().create_any();
170       out_any.type(type());
171       out_any.read_value( new CDRInputStream(orb, os.getBufferCopy()), type());
172       return out_any;
173    }
174
175    public java.lang.String JavaDoc get_as_string()
176    {
177       checkDestroyed ();
178       return member_names[ enum_value ];
179    }
180     
181    public void set_as_string( java.lang.String JavaDoc arg )
182       throws InvalidValue
183    {
184       checkDestroyed ();
185       int i = 0;
186       while( i < member_names.length && !(arg.equals(member_names[i])) )
187          i++;
188
189       if( i < member_names.length )
190          set_as_ulong(i);
191       else
192          throw new InvalidValue();
193    }
194
195    public int get_as_ulong()
196    {
197       checkDestroyed ();
198       return enum_value;
199    }
200     
201    public void set_as_ulong(int arg)
202       throws InvalidValue
203    {
204       checkDestroyed ();
205       if( arg < 0 || arg > max )
206          throw new InvalidValue();
207
208       enum_value = arg;
209    }
210
211
212 }
213
Popular Tags