KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.jacorb.orb.*;
27
28 import java.util.*;
29
30 /**
31  * CORBA DynArray
32  *
33  * @author (c) Gerald Brose, FU Berlin 1999
34  * $Id: DynArray.java,v 1.16 2005/03/25 13:15:55 andre.spiegel Exp $
35  */

36
37 public final class DynArray
38     extends DynAny
39     implements org.omg.DynamicAny.DynArray JavaDoc
40 {
41     private org.omg.CORBA.TypeCode JavaDoc elementType;
42     private org.omg.CORBA.Any JavaDoc[] members;
43
44    DynArray( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
45              org.omg.CORBA.TypeCode JavaDoc tc)
46       throws InvalidValue, TypeMismatch
47    {
48       org.omg.CORBA.TypeCode JavaDoc _type =
49          TypeCode.originalType( tc );
50       
51       if( _type.kind() != org.omg.CORBA.TCKind.tk_array )
52          throw new TypeMismatch();
53
54       try
55       {
56          type = _type;
57          this.orb = org.omg.CORBA.ORB.init();
58          this.dynFactory = dynFactory;
59          elementType = TypeCode.originalType( type.content_type() );
60
61          limit = type.length();
62          members = new Any[limit];
63          try
64          {
65             for( int i = limit; i-- > 0;)
66             {
67                members[i] = dynFactory.create_dyn_any_from_type_code( elementType ).to_any();
68             }
69          }
70          catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
71          {
72             // should never happen
73
itc.printStackTrace();
74          }
75       }
76       catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
77       {
78          bk.printStackTrace();
79       }
80    }
81
82    DynArray( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
83              org.omg.CORBA.TypeCode JavaDoc tc, org.omg.CORBA.ORB JavaDoc orb)
84     throws InvalidValue, TypeMismatch
85  {
86     org.omg.CORBA.TypeCode JavaDoc _type =
87        TypeCode.originalType( tc );
88     
89     if( _type.kind() != org.omg.CORBA.TCKind.tk_array )
90        throw new TypeMismatch();
91
92     try
93     {
94        type = _type;
95        this.orb = orb;
96        this.dynFactory = dynFactory;
97        elementType = TypeCode.originalType( type.content_type() );
98
99        limit = type.length();
100        members = new Any[limit];
101        try
102        {
103           for( int i = limit; i-- > 0;)
104           {
105              members[i] = dynFactory.create_dyn_any_from_type_code( elementType ).to_any();
106           }
107        }
108        catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
109        {
110           // should never happen
111
itc.printStackTrace();
112        }
113     }
114     catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
115     {
116        bk.printStackTrace();
117     }
118     }
119    
120    public void from_any(org.omg.CORBA.Any JavaDoc value)
121       throws InvalidValue, TypeMismatch
122    {
123       checkDestroyed ();
124       if( ! type.equivalent( value.type() ))
125          throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc();
126
127       type = TypeCode.originalType( value.type() );
128
129       try
130       {
131          limit = type().length();
132          elementType = TypeCode.originalType( type.content_type() );
133
134          if( limit > 0 )
135             pos = 0;
136
137          org.omg.CORBA.portable.InputStream JavaDoc is = value.create_input_stream();
138          members = new org.omg.CORBA.Any JavaDoc[limit];
139
140          for( int i = 0 ; i < limit; i++ )
141          {
142             members[i] = org.omg.CORBA.ORB.init().create_any();
143             members[i].read_value(is, elementType);
144          }
145       }
146       catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
147       {
148          // should not happen anymore
149
bk.printStackTrace();
150       }
151    }
152
153    public org.omg.CORBA.Any JavaDoc to_any()
154    {
155       checkDestroyed ();
156       org.jacorb.orb.Any out_any =
157          (org.jacorb.orb.Any)org.omg.CORBA.ORB.init().create_any();
158       out_any.type( type());
159
160       CDROutputStream os = new CDROutputStream();
161
162       for( int i = 0; i < limit; i++)
163       {
164          os.write_value( elementType,
165                          (CDRInputStream)members[i].create_input_stream());
166       }
167
168       CDRInputStream is = new CDRInputStream(orb, os.getBufferCopy());
169       out_any.read_value( is, type());
170       return out_any;
171    }
172
173    /**
174      * Overrides equal() in DynAny
175      */

176
177    public boolean equal( org.omg.DynamicAny.DynAny JavaDoc dyn_any )
178    {
179       checkDestroyed ();
180       if( !type().equal( dyn_any.type()) )
181          return false;
182
183       org.omg.DynamicAny.DynArray JavaDoc other = DynArrayHelper.narrow( dyn_any );
184
185       org.omg.CORBA.Any JavaDoc[] elements = get_elements();
186       org.omg.CORBA.Any JavaDoc[] other_elements = other.get_elements();
187
188       for( int i = 0; i < elements.length; i++ )
189       {
190          if( ! (elements[i].equal( other_elements[i] )))
191             return false;
192       }
193
194       return true;
195    }
196
197
198    public org.omg.CORBA.Any JavaDoc[] get_elements()
199    {
200       checkDestroyed ();
201       return members;
202    }
203
204
205    public void set_elements(org.omg.CORBA.Any JavaDoc[] value)
206       throws TypeMismatch, InvalidValue
207    {
208       checkDestroyed ();
209       if( value.length != limit )
210          throw new InvalidValue();
211
212       for( int i = value.length; i-- > 0 ;)
213       {
214          org.omg.CORBA.TypeCode JavaDoc tc =
215             TypeCode.originalType( value[i].type() );
216
217          if( tc.kind() != elementType.kind() )
218             throw new TypeMismatch();
219       }
220
221       /** ok now */
222       members = value;
223    }
224
225    public org.omg.DynamicAny.DynAny JavaDoc[] get_elements_as_dyn_any()
226    {
227       checkDestroyed ();
228       org.omg.DynamicAny.DynAny JavaDoc[] result =
229          new org.omg.DynamicAny.DynAny JavaDoc[ members.length ];
230       try
231       {
232          for( int i = members.length; i-- > 0; )
233             result[i] = dynFactory.create_dyn_any( members[i]);
234          return result;
235       }
236       catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
237       {
238          // should never happen
239
itc.printStackTrace();
240       }
241       return null;
242    }
243
244
245    public void set_elements_as_dyn_any(org.omg.DynamicAny.DynAny JavaDoc[] value)
246       throws TypeMismatch, InvalidValue
247    {
248       checkDestroyed ();
249       org.omg.CORBA.Any JavaDoc [] any_seq =
250          new org.omg.CORBA.Any JavaDoc[value.length];
251       for( int i = value.length; i-- > 0; )
252          any_seq[i] = value[i].to_any();
253
254       set_elements( any_seq );
255    }
256
257    public void destroy()
258    {
259       super.destroy();
260       members = null;
261       elementType = null;
262    }
263
264    /**
265      * Returns the DynAny's internal any representation.
266      * <p>
267      * Overrides getRepresentation() in DynAny
268      */

269
270    protected org.omg.CORBA.Any JavaDoc getRepresentation()
271    {
272       return members[pos];
273    }
274
275    /* iteration interface */
276
277    public org.omg.DynamicAny.DynAny JavaDoc current_component()
278    {
279       checkDestroyed ();
280       if( pos == -1 )
281          return null;
282       try
283       {
284          return dynFactory.create_dyn_any( members[pos] );
285       }
286       catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
287       {
288          // should never happen
289
itc.printStackTrace();
290       }
291       return null;
292    }
293    
294
295 }
296
Popular Tags