1 /* GeneralTypeCode.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package gnu
.CORBA
.typecodes
;
41 import gnu
.CORBA
.CDR
.BufferedCdrOutput
;
43 import java
.util
.Arrays
;
44 import java
.util
.BitSet
;
46 import org
.omg
.CORBA
.TCKind
;
47 import org
.omg
.CORBA
.TypeCode
;
48 import org
.omg
.CORBA
.TypeCodePackage
.BadKind
;
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.
57 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
59 public class GeneralTypeCode
60 extends PrimitiveTypeCode
63 * Use serialVersionUID for interoperability.
65 private static final long serialVersionUID
= 1;
69 * Indicates that the field value has not been previously set.
71 protected static int UNSET
= Integer
.MIN_VALUE
;
74 * The kinds for that the length() must return 0 even if it
75 * has not been previously set.
77 private static final BitSet lengthAllowed
= new BitSet();
81 lengthAllowed
.set(TCKind
._tk_array
);
82 lengthAllowed
.set(TCKind
._tk_sequence
);
83 lengthAllowed
.set(TCKind
._tk_string
);
84 lengthAllowed
.set(TCKind
._tk_wstring
);
89 private TypeCode concrete_base_type
;
90 private TypeCode content_type
;
92 private int type_modifier
= UNSET
;
95 * Create a new instance, setting kind to the given kind.
96 * @param a_kind the kind of the typecode being created.
98 public GeneralTypeCode(TCKind a_kind
)
101 if (!lengthAllowed
.get(a_kind
.value()))
108 public void setConcreteBase_type(TypeCode a_concrete_base_type
)
110 this.concrete_base_type
= a_concrete_base_type
;
114 * Set the component content type.
116 public void setContentType(TypeCode a_content_type
)
118 this.content_type
= a_content_type
;
124 public void setId(String an_id
)
130 * Set the length property.
133 public void setLength(int l
)
141 public void setName(String a_name
)
147 * Set the type modifier.
149 public void setTypeModifier(int a_type_modifier
)
151 this.type_modifier
= a_type_modifier
;
155 public TypeCode
concrete_base_type()
158 if (concrete_base_type
!= null)
159 return concrete_base_type
;
160 throw new BadKind("concrete_base_type");
164 * Returns the content type that must be explicitly set
167 * @throws BadKind if the content type has not been set.
169 public TypeCode
content_type()
172 if (content_type
!= null)
174 throw new BadKind("content_type");
178 * Returns true if both typecodes, if written into CDR
179 * stream, would result the same stream content.
181 public boolean equal(TypeCode other
)
185 if (kind() != other
.kind())
188 BufferedCdrOutput a
= new BufferedCdrOutput(16);
189 BufferedCdrOutput b
= new BufferedCdrOutput(16);
191 a
.write_TypeCode(this);
192 b
.write_TypeCode(other
);
194 return Arrays
.equals(a
.buffer
.toByteArray(), b
.buffer
.toByteArray());
198 * Delegates functionality to {@link #equal}.
200 public boolean equivalent(TypeCode other
)
211 throw new BadKind("id");
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.
219 * @return the length of string, array or sequence.
221 * @throws BadKind if the method cannot be invoked for the
222 * given kind of typecode.
229 throw new BadKind("length");
238 throw new BadKind("name");
242 public short type_modifier()
245 if (type_modifier
!= UNSET
)
246 return (short) type_modifier
;
247 throw new BadKind("type_modifier");