KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

180
181    public boolean equal( org.omg.DynamicAny.DynAny JavaDoc dyn_any )
182    {
183       checkDestroyed ();
184       if( !type().equal( dyn_any.type()) )
185          return false;
186
187       org.omg.DynamicAny.DynSequence JavaDoc other = DynSequenceHelper.narrow( dyn_any );
188
189       if( other.get_length() != get_length())
190          return false;
191
192       org.omg.CORBA.Any JavaDoc[] elements = get_elements();
193       org.omg.CORBA.Any JavaDoc[] other_elements = other.get_elements();
194
195       for( int i = 0; i < elements.length; i++ )
196       {
197          if( ! (elements[i].equal( other_elements[i] )))
198             return false;
199       }
200
201       return true;
202    }
203
204    public int get_length()
205    {
206       checkDestroyed ();
207       return length;
208    }
209
210
211    public void set_length(int len)
212       throws InvalidValue
213    {
214       checkDestroyed ();
215       if( limit > 0 && len > limit )
216          throw new InvalidValue();
217
218       if( len == 0 )
219       {
220          members = new Vector();
221          pos = -1;
222       }
223
224       if (elementType == null)
225       {
226           throw new INTERNAL JavaDoc ("DynSequence.set_length, elementType null");
227       }
228
229       if( len > length )
230       {
231          try
232          {
233             for( int i = length; i < len; i++ )
234             {
235                // create correctly initialized anys
236
members.addElement( dynFactory.create_dyn_any_from_type_code( elementType ).to_any() );
237             }
238          }
239          catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
240          {
241             // should neever happen
242
itc.printStackTrace();
243          }
244
245          if( pos == -1 )
246             pos = len - length - 1;
247       }
248       else if( len < length )
249       {
250          members.setSize(len);
251
252          if( pos > len )
253             pos = -1;
254       }
255       length = len;
256    }
257
258
259    public org.omg.CORBA.Any JavaDoc[] get_elements()
260    {
261       checkDestroyed ();
262       Any[] result = new Any[ members.size()];
263       for( int i = members.size(); i-- > 0; )
264          result[i] = (Any)members.elementAt(i);
265       return result;
266    }
267
268
269    public void set_elements( org.omg.CORBA.Any JavaDoc[] value )
270       throws TypeMismatch, InvalidValue
271    {
272       checkDestroyed ();
273       if( limit > 0 && value.length > limit )
274          throw new InvalidValue();
275
276       for( int i = value.length; i-- > 0 ;)
277       {
278          org.omg.CORBA.TypeCode JavaDoc tc = TypeCode.originalType( value[i].type() );
279
280          if( tc.kind() != elementType.kind() )
281             throw new TypeMismatch();
282       }
283
284       /** ok now */
285       length = value.length;
286
287       members = new Vector();
288       for( int i = 0; i < length; i++)
289       {
290          members.addElement( value[i] );
291       }
292
293       if( length > 0 )
294          pos = 0;
295       else
296          pos = -1;
297
298    }
299
300    public org.omg.DynamicAny.DynAny JavaDoc[] get_elements_as_dyn_any()
301    {
302       checkDestroyed ();
303       org.omg.DynamicAny.DynAny JavaDoc[] result =
304          new org.omg.DynamicAny.DynAny JavaDoc[ members.size()];
305       try
306       {
307          for( int i = members.size(); i-- > 0; )
308             result[i] = dynFactory.create_dyn_any( (Any)members.elementAt(i));
309          return result;
310       }
311       catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
312       {
313          // should never happen
314
itc.printStackTrace();
315       }
316       return null;
317    }
318
319
320    public void set_elements_as_dyn_any(org.omg.DynamicAny.DynAny JavaDoc[] value)
321       throws TypeMismatch, InvalidValue
322    {
323       checkDestroyed ();
324       org.omg.CORBA.Any JavaDoc [] any_seq = new org.omg.CORBA.Any JavaDoc[value.length];
325       for( int i = value.length; i-- > 0; )
326          any_seq[i] = value[i].to_any();
327
328       set_elements( any_seq );
329    }
330
331
332    public void destroy()
333    {
334       super.destroy();
335       members.removeAllElements();
336       members = null;
337       elementType = null;
338    }
339
340
341    /**
342     * returns the DynAny's internal any representation,
343     * overwrites
344     */

345
346
347    protected org.omg.CORBA.Any JavaDoc getRepresentation()
348    {
349       return (Any)members.elementAt(pos);
350    }
351
352
353    public boolean next()
354    {
355       checkDestroyed ();
356       if( pos < length-1 )
357       {
358          pos++;
359          return true;
360       }
361       pos = -1;
362       return false;
363    }
364
365    public boolean seek(int index)
366    {
367       checkDestroyed ();
368       if( index < 0 )
369       {
370          pos = -1;
371          return false;
372       }
373       if( index < length )
374       {
375          pos = index;
376          return true;
377       }
378       pos = -1;
379       return false;
380    }
381
382    /* iteration interface */
383
384    public org.omg.DynamicAny.DynAny JavaDoc current_component()
385    {
386       checkDestroyed ();
387       if( pos == -1 )
388       {
389          return null;
390       }
391       try
392       {
393          return dynFactory.create_dyn_any( (Any)members.elementAt(pos) );
394       }
395       catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
396       {
397          // should never happen
398
itc.printStackTrace();
399       }
400       return null;
401    }
402
403    public int component_count()
404    {
405       checkDestroyed ();
406       return get_length();
407    }
408
409 }
410
Popular Tags