Source for gnu.CORBA.typecodes.GeneralTypeCode

   1: /* GeneralTypeCode.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.typecodes;
  40: 
  41: import gnu.CORBA.CDR.BufferedCdrOutput;
  42: 
  43: import java.util.Arrays;
  44: import java.util.BitSet;
  45: 
  46: import org.omg.CORBA.TCKind;
  47: import org.omg.CORBA.TypeCode;
  48: import org.omg.CORBA.TypeCodePackage.BadKind;
  49: 
  50: /**
  51:  * A typecode for types, requiring to provide various additional
  52:  * properties but still not requiring to store the
  53:  * members of the structure. The property can be retrieved
  54:  * by the corresponding method if it has been previously assigned.
  55:  * Otherwise, a {@link BadKind} is thrown.
  56:  *
  57:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  58:  */
  59: public class GeneralTypeCode
  60:   extends PrimitiveTypeCode
  61: {
  62:   /**
  63:    * Use serialVersionUID for interoperability.
  64:    */
  65:   private static final long serialVersionUID = 1;
  66: 
  67: 
  68:   /**
  69:    * Indicates that the field value has not been previously set.
  70:    */
  71:   protected static int UNSET = Integer.MIN_VALUE;
  72: 
  73:   /**
  74:    * The kinds for that the length() must return 0 even if it
  75:    * has not been previously set.
  76:    */
  77:   private static final BitSet lengthAllowed = new BitSet();
  78: 
  79:   static
  80:   {
  81:     lengthAllowed.set(TCKind._tk_array);
  82:     lengthAllowed.set(TCKind._tk_sequence);
  83:     lengthAllowed.set(TCKind._tk_string);
  84:     lengthAllowed.set(TCKind._tk_wstring);
  85:   }
  86: 
  87:   private String id;
  88:   private String name;
  89:   private TypeCode concrete_base_type;
  90:   private TypeCode content_type;
  91:   private int len;
  92:   private int type_modifier = UNSET;
  93: 
  94:   /**
  95:    * Create a new instance, setting kind to the given kind.
  96:    * @param a_kind the kind of the typecode being created.
  97:    */
  98:   public GeneralTypeCode(TCKind a_kind)
  99:   {
 100:     super(a_kind);
 101:     if (!lengthAllowed.get(a_kind.value()))
 102:       len = UNSET;
 103:   }
 104: 
 105:   /**
 106:    * Set this property.
 107:    */
 108:   public void setConcreteBase_type(TypeCode a_concrete_base_type)
 109:   {
 110:     this.concrete_base_type = a_concrete_base_type;
 111:   }
 112: 
 113:   /**
 114:    * Set the component content type.
 115:    */
 116:   public void setContentType(TypeCode a_content_type)
 117:   {
 118:     this.content_type = a_content_type;
 119:   }
 120: 
 121:   /**
 122:    * Set this property.
 123:    */
 124:   public void setId(String an_id)
 125:   {
 126:     this.id = an_id;
 127:   }
 128: 
 129:   /**
 130:    * Set the length property.
 131:    * @param l
 132:    */
 133:   public void setLength(int l)
 134:   {
 135:     len = l;
 136:   }
 137: 
 138:   /**
 139:    * Set this property.
 140:    */
 141:   public void setName(String a_name)
 142:   {
 143:     this.name = a_name;
 144:   }
 145: 
 146:   /**
 147:    * Set the type modifier.
 148:    */
 149:   public void setTypeModifier(int a_type_modifier)
 150:   {
 151:     this.type_modifier = a_type_modifier;
 152:   }
 153: 
 154:   /** {@inheritDoc} */
 155:   public TypeCode concrete_base_type()
 156:                               throws BadKind
 157:   {
 158:     if (concrete_base_type != null)
 159:       return concrete_base_type;
 160:     throw new BadKind("concrete_base_type");
 161:   }
 162: 
 163:   /**
 164:    * Returns the content type that must be explicitly set
 165:    * for this class.
 166:    *
 167:    * @throws BadKind if the content type has not been set.
 168:    */
 169:   public TypeCode content_type()
 170:                         throws BadKind
 171:   {
 172:     if (content_type != null)
 173:       return content_type;
 174:     throw new BadKind("content_type");
 175:   }
 176: 
 177:   /**
 178:    * Returns true if both typecodes, if written into CDR
 179:    * stream, would result the same stream content.
 180:    */
 181:   public boolean equal(TypeCode other)
 182:   {
 183:     if (this == other)
 184:       return true;
 185:     if (kind() != other.kind())
 186:       return false;
 187: 
 188:     BufferedCdrOutput a = new BufferedCdrOutput(16);
 189:     BufferedCdrOutput b = new BufferedCdrOutput(16);
 190: 
 191:     a.write_TypeCode(this);
 192:     b.write_TypeCode(other);
 193: 
 194:     return Arrays.equals(a.buffer.toByteArray(), b.buffer.toByteArray());
 195:   }
 196: 
 197:   /**
 198:    * Delegates functionality to {@link #equal}.
 199:    */
 200:   public boolean equivalent(TypeCode other)
 201:   {
 202:     return equal(other);
 203:   }
 204: 
 205:   /** {@inheritDoc} */
 206:   public String id()
 207:             throws BadKind
 208:   {
 209:     if (id != null)
 210:       return id;
 211:     throw new BadKind("id");
 212:   }
 213: 
 214:   /**
 215:    * Get the length. For sequences, arrays, strings and wstrings
 216:    * this method returns 0 rather than throwing a BadKind even
 217:    * if {@link setLength(int)} has not been previously called.
 218:    *
 219:    * @return the length of string, array or sequence.
 220:    *
 221:    * @throws BadKind if the method cannot be invoked for the
 222:    * given kind of typecode.
 223:    */
 224:   public int length()
 225:              throws BadKind
 226:   {
 227:     if (len != UNSET)
 228:       return len;
 229:     throw new BadKind("length");
 230:   }
 231: 
 232:   /** {@inheritDoc} */
 233:   public String name()
 234:               throws BadKind
 235:   {
 236:     if (name != null)
 237:       return name;
 238:     throw new BadKind("name");
 239:   }
 240: 
 241:   /** {@inheritDoc} */
 242:   public short type_modifier()
 243:                       throws BadKind
 244:   {
 245:     if (type_modifier != UNSET)
 246:       return (short) type_modifier;
 247:     throw new BadKind("type_modifier");
 248:   }
 249: }