1 package org.jacorb.orb.dynany; 2 3 22 23 import org.omg.DynamicAny.DynAnyPackage.*; 24 import org.omg.DynamicAny.*; 25 26 import org.omg.CORBA.INTERNAL ; 27 import org.jacorb.orb.*; 28 29 import java.util.*; 30 31 38 39 public final class DynSequence 40 extends DynAny 41 implements org.omg.DynamicAny.DynSequence 42 { 43 private Vector members; 44 private int length; 45 private org.omg.CORBA.TypeCode elementType; 46 47 DynSequence( org.omg.DynamicAny.DynAnyFactory dynFactory, 48 org.omg.CORBA.TypeCode tc ) 49 throws InvalidValue, TypeMismatch 50 { 51 org.omg.CORBA.TypeCode _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 bk ) 69 { 70 bk.printStackTrace(); 71 } 72 if (elementType == null) 73 { 74 throw new INTERNAL ("DynSequence.set_length, elementType null"); 75 } 76 } 77 78 DynSequence( org.omg.DynamicAny.DynAnyFactory dynFactory, 79 org.omg.CORBA.TypeCode tc, org.omg.CORBA.ORB orb ) 80 throws InvalidValue, TypeMismatch 81 { 82 org.omg.CORBA.TypeCode _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 bk ) 100 { 101 bk.printStackTrace(); 102 } 103 if (elementType == null) 104 { 105 throw new INTERNAL ("DynSequence.set_length, elementType null"); 106 } 107 } 108 109 public void from_any( org.omg.CORBA.Any value ) 110 throws InvalidValue, TypeMismatch 111 { 112 checkDestroyed (); 113 if( ! type().equivalent( value.type() )) 114 { 115 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch (); 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 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 bk ) 146 { 147 bk.printStackTrace(); 149 } 150 if (elementType == null) 151 { 152 throw new INTERNAL ("DynSequence.set_length, elementType null"); 153 } 154 } 155 156 157 public org.omg.CORBA.Any 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 180 181 public boolean equal( org.omg.DynamicAny.DynAny dyn_any ) 182 { 183 checkDestroyed (); 184 if( !type().equal( dyn_any.type()) ) 185 return false; 186 187 org.omg.DynamicAny.DynSequence other = DynSequenceHelper.narrow( dyn_any ); 188 189 if( other.get_length() != get_length()) 190 return false; 191 192 org.omg.CORBA.Any [] elements = get_elements(); 193 org.omg.CORBA.Any [] 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 ("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 members.addElement( dynFactory.create_dyn_any_from_type_code( elementType ).to_any() ); 237 } 238 } 239 catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode itc ) 240 { 241 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 [] 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 [] 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 tc = TypeCode.originalType( value[i].type() ); 279 280 if( tc.kind() != elementType.kind() ) 281 throw new TypeMismatch(); 282 } 283 284 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 [] get_elements_as_dyn_any() 301 { 302 checkDestroyed (); 303 org.omg.DynamicAny.DynAny [] result = 304 new org.omg.DynamicAny.DynAny [ 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 itc ) 312 { 313 itc.printStackTrace(); 315 } 316 return null; 317 } 318 319 320 public void set_elements_as_dyn_any(org.omg.DynamicAny.DynAny [] value) 321 throws TypeMismatch, InvalidValue 322 { 323 checkDestroyed (); 324 org.omg.CORBA.Any [] any_seq = new org.omg.CORBA.Any [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 345 346 347 protected org.omg.CORBA.Any 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 383 384 public org.omg.DynamicAny.DynAny 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 itc ) 396 { 397 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 |