OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / CORBA / generalTypeCode.java
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;
40
41 import gnu.CORBA.CDR.cdrBufOutput;
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    * Indicates that the field value has not been previously set.
64    */
65   protected static int UNSET = Integer.MIN_VALUE;
66
67   /**
68    * The kinds for that the length() must return 0 even if it
69    * has not been previously set.
70    */
71   private static final BitSet lengthAllowed = new BitSet();
72
73   static
74   {
75     lengthAllowed.set(TCKind._tk_array);
76     lengthAllowed.set(TCKind._tk_sequence);
77     lengthAllowed.set(TCKind._tk_string);
78     lengthAllowed.set(TCKind._tk_wstring);
79   }
80
81   private String id;
82   private String name;
83   private TypeCode concrete_base_type;
84   private TypeCode content_type;
85   private int len;
86   private int type_modifier = UNSET;
87
88   /**
89    * Create a new instance, setting kind to the given kind.
90    * @param kind
91    */
92   public generalTypeCode(TCKind kind)
93   {
94     super(kind);
95     if (!lengthAllowed.get(kind.value()))
96       len = UNSET;
97   }
98
99   /**
100    * Set this property.
101    */
102   public void setConcreteBase_type(TypeCode concrete_base_type)
103   {
104     this.concrete_base_type = concrete_base_type;
105   }
106
107   /**
108    * Set the component content type.
109    */
110   public void setContentType(TypeCode a_content_type)
111   {
112     this.content_type = a_content_type;
113   }
114
115   /**
116    * Set this property.
117    */
118   public void setId(String id)
119   {
120     this.id = id;
121   }
122
123   /**
124    * Set the length property.
125    * @param l
126    */
127   public void setLength(int l)
128   {
129     len = l;
130   }
131
132   /**
133    * Set this property.
134    */
135   public void setName(String name)
136   {
137     this.name = name;
138   }
139
140   /**
141    * Set the type modifier.
142    */
143   public void setTypeModifier(int a_type_modifier)
144   {
145     this.type_modifier = a_type_modifier;
146   }
147
148   /** {@inheritDoc} */
149   public TypeCode concrete_base_type()
150                               throws BadKind
151   {
152     if (concrete_base_type != null)
153       return concrete_base_type;
154     throw new BadKind("concrete_base_type");
155   }
156
157   /**
158    * Returns the content type that must be explicitly set
159    * for this class.
160    *
161    * @throws BadKind if the content type has not been set.
162    */
163   public TypeCode content_type()
164                         throws BadKind
165   {
166     if (content_type != null)
167       return content_type;
168     throw new BadKind("content_type");
169   }
170
171   /**
172    * Returns true if both typecodes, if written into CDR
173    * stream, would result the same stream content.
174    */
175   public boolean equal(TypeCode other)
176   {
177     if (this == other)
178       return true;
179     if (kind() != other.kind())
180       return false;
181
182     cdrBufOutput a = new cdrBufOutput(16);
183     cdrBufOutput b = new cdrBufOutput(16);
184
185     a.write_TypeCode(this);
186     b.write_TypeCode(other);
187
188     return Arrays.equals(a.buffer.toByteArray(), b.buffer.toByteArray());
189   }
190
191   /**
192    * Delegates functionality to {@link #equal}.
193    */
194   public boolean equivalent(TypeCode other)
195   {
196     return equal(other);
197   }
198
199   /** {@inheritDoc} */
200   public String id()
201             throws BadKind
202   {
203     if (id != null)
204       return id;
205     throw new BadKind("id");
206   }
207
208   /**
209    * Get the length. For sequences, arrays, strings and wstrings
210    * this method returns 0 rather than throwing a BadKind even
211    * if {@link setLength(int)} has not been previously called.
212    *
213    * @return the length of string, array or sequence.
214    *
215    * @throws BadKind if the method cannot be invoked for the
216    * given kind of typecode.
217    */
218   public int length()
219              throws BadKind
220   {
221     if (len != UNSET)
222       return len;
223     throw new BadKind("length");
224   }
225
226   /** {@inheritDoc} */
227   public String name()
228               throws BadKind
229   {
230     if (name != null)
231       return name;
232     throw new BadKind("name");
233   }
234
235   /** {@inheritDoc} */
236   public short type_modifier()
237                       throws BadKind
238   {
239     if (type_modifier != UNSET)
240       return (short) type_modifier;
241     throw new BadKind("type_modifier");
242   }
243 }