Source for gnu.CORBA.DynAn.DivideableAny

   1: /* DivideableAny.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA.DynAn;
  40: 
  41: import gnu.CORBA.TypeKindNamer;
  42: 
  43: import org.omg.CORBA.Any;
  44: import org.omg.CORBA.CompletionStatus;
  45: import org.omg.CORBA.ORB;
  46: import org.omg.CORBA.Object;
  47: import org.omg.CORBA.TypeCode;
  48: import org.omg.CORBA.UNKNOWN;
  49: import org.omg.DynamicAny.DynAny;
  50: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  51: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  52: import org.omg.DynamicAny.DynValueCommon;
  53: 
  54: import java.io.Serializable;
  55: 
  56: /**
  57:  * Provides a base for DynAnys, having multiple components.
  58:  *
  59:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  60:  */
  61: public abstract class DivideableAny
  62:   extends AbstractAny
  63:   implements Serializable
  64: {
  65:   /**
  66:    * Use serialVersionUID for interoperability.
  67:    */
  68:   private static final long serialVersionUID = 1;
  69: 
  70:   /**
  71:    * The array of the components that in general case may have different
  72:    * final_type.
  73:    */
  74:   protected DynAny[] array;
  75: 
  76:   /**
  77:    * The internal pointer.
  78:    */
  79:   protected int pos = 0;
  80: 
  81:   public DivideableAny(TypeCode oType, TypeCode aType,
  82:                        gnuDynAnyFactory aFactory, ORB anOrb
  83:                       )
  84:   {
  85:     super(oType, aType, aFactory, anOrb);
  86:   }
  87: 
  88:   /**
  89:    * Advance forward.
  90:    */
  91:   public boolean next()
  92:   {
  93:     pos++;
  94:     return array.length > pos;
  95:   }
  96: 
  97:   /**
  98:    * Set zero position.
  99:    */
 100:   public void rewind()
 101:   {
 102:     pos = 0;
 103:   }
 104: 
 105:   /**
 106:    * Set a position.
 107:    */
 108:   public boolean seek(int p)
 109:   {
 110:     pos = p;
 111:     return pos >= 0 && array.length > pos;
 112:   }
 113: 
 114:   /**
 115:    * Get the insertion point as DynAny. This method may throw exceptions if the
 116:    * current insertion point does not support reading or insertion of the
 117:    * primitive types.
 118:    *
 119:    * @return the focused component, from where the primitve value can be read or
 120:    * where it can be inserted.
 121:    * @throws InvalidValue if the primitive value cannot be inserted at the given
 122:    * point.
 123:    */
 124:   protected DynAny focused()
 125:                     throws InvalidValue, TypeMismatch
 126:   {
 127:     if (pos >= 0 && pos < array.length)
 128:       {
 129:         if (array [ pos ].component_count() == 0)
 130:           return array [ pos ];
 131:         else
 132:           throw new TypeMismatch("Multiple coponents at " + pos);
 133:       }
 134:     else
 135:       throw new InvalidValue("Out of bounds at " + pos + " valid 0.." +
 136:                              (array.length - 1)
 137:                             );
 138:   }
 139: 
 140:   /** {@inheritDoc} */
 141:   public int component_count()
 142:   {
 143:     return array.length;
 144:   }
 145: 
 146:   /**
 147:    * Return the second (enclosed any) that is stored in the wrapped Any.
 148:    */
 149:   public Any get_any()
 150:               throws TypeMismatch, InvalidValue
 151:   {
 152:     return focused().get_any();
 153:   }
 154: 
 155:   /** {@inheritDoc} */
 156:   public boolean get_boolean()
 157:                       throws TypeMismatch, InvalidValue
 158:   {
 159:     return focused().get_boolean();
 160:   }
 161: 
 162:   /** {@inheritDoc} */
 163:   public char get_char()
 164:                 throws TypeMismatch, InvalidValue
 165:   {
 166:     return focused().get_char();
 167:   }
 168: 
 169:   /** {@inheritDoc} */
 170:   public double get_double()
 171:                     throws TypeMismatch, InvalidValue
 172:   {
 173:     return focused().get_double();
 174:   }
 175: 
 176:   /** {@inheritDoc} */
 177:   public float get_float()
 178:                   throws TypeMismatch, InvalidValue
 179:   {
 180:     return focused().get_float();
 181:   }
 182: 
 183:   /** {@inheritDoc} */
 184:   public int get_long()
 185:                throws TypeMismatch, InvalidValue
 186:   {
 187:     return focused().get_long();
 188:   }
 189: 
 190:   /** {@inheritDoc} */
 191:   public long get_longlong()
 192:                     throws TypeMismatch, InvalidValue
 193:   {
 194:     return focused().get_longlong();
 195:   }
 196: 
 197:   /** {@inheritDoc} */
 198:   public byte get_octet()
 199:                  throws TypeMismatch, InvalidValue
 200:   {
 201:     return focused().get_octet();
 202:   }
 203: 
 204:   /** {@inheritDoc} */
 205:   public Object get_reference()
 206:                        throws TypeMismatch, InvalidValue
 207:   {
 208:     return focused().get_reference();
 209:   }
 210: 
 211:   /** {@inheritDoc} */
 212:   public short get_short()
 213:                   throws TypeMismatch, InvalidValue
 214:   {
 215:     return focused().get_short();
 216:   }
 217: 
 218:   /** {@inheritDoc} */
 219:   public String get_string()
 220:                     throws TypeMismatch, InvalidValue
 221:   {
 222:     return focused().get_string();
 223:   }
 224: 
 225:   /** {@inheritDoc} */
 226:   public TypeCode get_typecode()
 227:                         throws TypeMismatch, InvalidValue
 228:   {
 229:     return focused().get_typecode();
 230:   }
 231: 
 232:   /** {@inheritDoc} */
 233:   public int get_ulong()
 234:                 throws TypeMismatch, InvalidValue
 235:   {
 236:     return focused().get_ulong();
 237:   }
 238: 
 239:   /** {@inheritDoc} */
 240:   public long get_ulonglong()
 241:                      throws TypeMismatch, InvalidValue
 242:   {
 243:     return focused().get_ulonglong();
 244:   }
 245: 
 246:   /** {@inheritDoc} */
 247:   public short get_ushort()
 248:                    throws TypeMismatch, InvalidValue
 249:   {
 250:     return focused().get_ushort();
 251:   }
 252: 
 253:   /** {@inheritDoc} */
 254:   public Serializable get_val()
 255:                        throws TypeMismatch, InvalidValue
 256:   {
 257:     if (pos >= 0 && pos < array.length)
 258:       {
 259:         if (array [ pos ] instanceof DynValueCommon)
 260:           return array [ pos ].get_val();
 261:         else
 262:           throw new TypeMismatch();
 263:       }
 264:     else
 265:       throw new InvalidValue("Out of bounds at " + pos + " valid 0.." +
 266:                              (array.length - 1)
 267:                             );
 268:   }
 269: 
 270:   /** {@inheritDoc} */
 271:   public char get_wchar()
 272:                  throws TypeMismatch, InvalidValue
 273:   {
 274:     return focused().get_wchar();
 275:   }
 276: 
 277:   /** {@inheritDoc} */
 278:   public String get_wstring()
 279:                      throws TypeMismatch, InvalidValue
 280:   {
 281:     return focused().get_wstring();
 282:   }
 283: 
 284:   /** {@inheritDoc} */
 285:   public void insert_any(Any a_x)
 286:                   throws TypeMismatch, InvalidValue
 287:   {
 288:     focused().insert_any(a_x);
 289:     valueChanged();
 290:   }
 291: 
 292:   /** {@inheritDoc} */
 293:   public void insert_boolean(boolean a_x)
 294:                       throws InvalidValue, TypeMismatch
 295:   {
 296:     focused().insert_boolean(a_x);
 297:     valueChanged();
 298:   }
 299: 
 300:   /** {@inheritDoc} */
 301:   public void insert_char(char a_x)
 302:                    throws InvalidValue, TypeMismatch
 303:   {
 304:     focused().insert_char(a_x);
 305:     valueChanged();
 306:   }
 307: 
 308:   /** {@inheritDoc} */
 309:   public void insert_double(double a_x)
 310:                      throws InvalidValue, TypeMismatch
 311:   {
 312:     focused().insert_double(a_x);
 313:     valueChanged();
 314:   }
 315: 
 316:   /** {@inheritDoc} */
 317:   public void insert_float(float a_x)
 318:                     throws InvalidValue, TypeMismatch
 319:   {
 320:     focused().insert_float(a_x);
 321:     valueChanged();
 322:   }
 323: 
 324:   /** {@inheritDoc} */
 325:   public void insert_long(int a_x)
 326:                    throws InvalidValue, TypeMismatch
 327:   {
 328:     focused().insert_long(a_x);
 329:     valueChanged();
 330:   }
 331: 
 332:   /** {@inheritDoc} */
 333:   public void insert_longlong(long a_x)
 334:                        throws InvalidValue, TypeMismatch
 335:   {
 336:     focused().insert_longlong(a_x);
 337:     valueChanged();
 338:   }
 339: 
 340:   /** {@inheritDoc} */
 341:   public void insert_octet(byte a_x)
 342:                     throws InvalidValue, TypeMismatch
 343:   {
 344:     focused().insert_octet(a_x);
 345:     valueChanged();
 346:   }
 347: 
 348:   /** {@inheritDoc} */
 349:   public void insert_reference(Object a_x)
 350:                         throws InvalidValue, TypeMismatch
 351:   {
 352:     focused().insert_reference(a_x);
 353:     valueChanged();
 354:   }
 355: 
 356:   /** {@inheritDoc} */
 357:   public void insert_short(short a_x)
 358:                     throws InvalidValue, TypeMismatch
 359:   {
 360:     focused().insert_short(a_x);
 361:     valueChanged();
 362:   }
 363: 
 364:   /** {@inheritDoc} */
 365:   public void insert_string(String a_x)
 366:                      throws InvalidValue, TypeMismatch
 367:   {
 368:     focused().insert_string(a_x);
 369:     valueChanged();
 370:   }
 371: 
 372:   /** {@inheritDoc} */
 373:   public void insert_typecode(TypeCode a_x)
 374:                        throws InvalidValue, TypeMismatch
 375:   {
 376:     focused().insert_typecode(a_x);
 377:     valueChanged();
 378:   }
 379: 
 380:   /** {@inheritDoc} */
 381:   public void insert_ulong(int a_x)
 382:                     throws InvalidValue, TypeMismatch
 383:   {
 384:     focused().insert_ulong(a_x);
 385:     valueChanged();
 386:   }
 387: 
 388:   /** {@inheritDoc} */
 389:   public void insert_ulonglong(long a_x)
 390:                         throws InvalidValue, TypeMismatch
 391:   {
 392:     focused().insert_ulonglong(a_x);
 393:     valueChanged();
 394:   }
 395: 
 396:   /** {@inheritDoc} */
 397:   public void insert_ushort(short a_x)
 398:                      throws InvalidValue, TypeMismatch
 399:   {
 400:     focused().insert_ushort(a_x);
 401:     valueChanged();
 402:   }
 403: 
 404:   /** {@inheritDoc} */
 405:   public void insert_val(Serializable a_x)
 406:                   throws InvalidValue, TypeMismatch
 407:   {
 408:     if (pos >= 0 && pos < array.length)
 409:       {
 410:         if (array [ pos ] instanceof DynValueCommon)
 411:           array [ pos ].insert_val(a_x);
 412:         else
 413:           throw new TypeMismatch();
 414:       }
 415:     else
 416:       throw new InvalidValue("Out of bounds at " + pos + " valid 0.." +
 417:                              (array.length - 1)
 418:                             );
 419:     valueChanged();
 420:   }
 421: 
 422:   /** {@inheritDoc} */
 423:   public void insert_wchar(char a_x)
 424:                     throws InvalidValue, TypeMismatch
 425:   {
 426:     focused().insert_wchar(a_x);
 427:     valueChanged();
 428:   }
 429: 
 430:   /** {@inheritDoc} */
 431:   public void insert_wstring(String a_x)
 432:                       throws InvalidValue, TypeMismatch
 433:   {
 434:     focused().insert_wstring(a_x);
 435:     valueChanged();
 436:   }
 437: 
 438:   /** {@inheritDoc} */
 439:   public DynAny get_dyn_any()
 440:                      throws TypeMismatch, InvalidValue
 441:   {
 442:     return focused().get_dyn_any();
 443:   }
 444: 
 445:   /** {@inheritDoc} */
 446:   public void insert_dyn_any(DynAny insert_it)
 447:                       throws TypeMismatch, InvalidValue
 448:   {
 449:     focused().insert_dyn_any(insert_it);
 450:   }
 451: 
 452:   /**
 453:    * Get current component.
 454:    *
 455:    * @return current component or <code>null</code> if the pointer is out of
 456:    * bounds.
 457:    */
 458:   public DynAny current_component()
 459:                            throws TypeMismatch
 460:   {
 461:     if (array.length == 0)
 462:       throw new TypeMismatch("empty");
 463:     return (pos >= 0 && pos < array.length) ? array [ pos ] : null;
 464:   }
 465: 
 466:   /**
 467:    * No action, cleanup is done by garbage collector in java.
 468:    */
 469:   public void destroy()
 470:   {
 471:   }
 472: 
 473:   /**
 474:    * Involved in equal(DynAny).
 475:    */
 476:   public abstract Any to_any()
 477:                       throws TypeMismatch;
 478: 
 479:   /**
 480:    * Compares with other DynAny for equality. The final_type, array size and
 481:    * array members must match.
 482:    */
 483:   public boolean equal(DynAny other)
 484:   {
 485:     try
 486:       {
 487:         if (!official_type.equal(other.type()))
 488:           return false;
 489:         else if (other instanceof DivideableAny)
 490:           {
 491:             DivideableAny x = (DivideableAny) other;
 492:             if (x.array.length != array.length)
 493:               return false;
 494: 
 495:             for (int i = 0; i < array.length; i++)
 496:               {
 497:                 if (!array [ i ].equal(x.array [ i ]))
 498:                   return false;
 499:               }
 500:             return true;
 501:           }
 502:         else if (other == null || other instanceof AbstractAny)
 503:           return false;
 504:         else
 505:           return other.to_any().equal(to_any());
 506:       }
 507:     catch (TypeMismatch e)
 508:       {
 509:         UNKNOWN u = new UNKNOWN(MINOR, CompletionStatus.COMPLETED_NO);
 510:         u.initCause(e);
 511:         throw u;
 512:       }
 513:   }