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