1 /* gnuDynSequence.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
.DynAn
;
41 import gnu
.CORBA
.Unexpected
;
43 import org
.omg
.CORBA
.Any
;
44 import org
.omg
.CORBA
.BAD_PARAM
;
45 import org
.omg
.CORBA
.ORB
;
46 import org
.omg
.CORBA
.TypeCode
;
47 import org
.omg
.CORBA
.TypeCodePackage
.BadKind
;
48 import org
.omg
.DynamicAny
.DynAny
;
49 import org
.omg
.DynamicAny
.DynAnyFactoryPackage
.InconsistentTypeCode
;
50 import org
.omg
.DynamicAny
.DynAnyPackage
.InvalidValue
;
51 import org
.omg
.DynamicAny
.DynAnyPackage
.TypeMismatch
;
52 import org
.omg
.DynamicAny
.DynSequence
;
54 import java
.io
.Serializable
;
56 import java
.lang
.reflect
.*;
58 public class gnuDynSequence
60 implements DynSequence
, Serializable
63 * Use serialVersionUID for interoperability.
65 private static final long serialVersionUID
= 1;
68 * The bound of the sequence, as defined in typecode.
73 * Create a new gnuDynSequence with the given typecode.
75 * @throws BAD_PARAM if the passed typecode is probably not a sequence
78 public gnuDynSequence(TypeCode oType
, TypeCode aType
,
79 gnuDynAnyFactory aFactory
, ORB anOrb
83 super(oType
, aType
, aFactory
, anOrb
, false);
84 array
= new DynAny
[ 0 ];
87 bound
= final_type
.length();
91 throw new Unexpected(ex
);
96 * Get the length of the sequence.
98 public int get_length()
104 * Resize the sequence, preserving components.
106 public void set_length(int length
)
110 if (length
== array
.length
)
111 return; // Nothing to do.
112 else if (length
< array
.length
)
115 DynAny
[] d
= new DynAny
[ length
];
116 for (int i
= 0; i
< d
.length
; i
++)
117 d
[ i
] = array
[ i
];
123 DynAny
[] d
= new DynAny
[ length
];
124 for (int i
= 0; i
< array
.length
; i
++)
125 d
[ i
] = array
[ i
];
127 for (int i
= array
.length
; i
< d
.length
; i
++)
132 factory
.create_dyn_any_from_type_code(official_components
);
134 catch (InconsistentTypeCode e
)
136 throw new Unexpected(e
);
145 * Copy one DynAny into another.
147 public void assign(DynAny from
)
150 checkType(official_type
, from
.type());
151 if (from
instanceof DynSequence
)
153 DynSequence dyn
= (DynSequence
) from
;
154 array
= dyn
.get_elements_as_dyn_any();
157 throw new TypeMismatch();
161 * Set the contenst of the sequence, resizing if required.
163 public void set_elements_as_dyn_any(DynAny
[] value
)
164 throws InvalidValue
, TypeMismatch
166 checkBound(value
.length
);
167 if (array
.length
!= value
.length
)
168 set_length(value
.length
);
170 for (int i
= 0; i
< value
.length
; i
++)
172 checkType(official_components
, value
[ i
].type());
173 array
[ i
].assign(value
[ i
]);
179 * Set the elements from array of Any's.
181 public void set_elements(Any
[] value
)
182 throws InvalidValue
, TypeMismatch
184 checkBound(value
.length
);
186 DynAny
[] prev
= array
;
188 array
= new DynAny
[ value
.length
];
191 super.set_elements(value
);
193 // valueChanged() is called in super.set_elements(value).
196 // On the problem, value does not change.
197 catch (TypeMismatch ex
)
202 catch (InvalidValue ex
)
207 catch (RuntimeException rex
)
219 DynAny
[] c
= new DynAny
[ array
.length
];
220 for (int i
= 0; i
< c
.length
; i
++)
222 c
[ i
] = array
[ i
].copy();
226 new gnuDynSequence(official_type
, final_type
, factory
, orb
);
234 * @param x the value to check.
236 void checkBound(int x
)
240 if (x
< 0 || x
> bound
)
241 throw new InvalidValue(x
+ " out of bounds, valid [0.." + bound
+ "]");
245 * Check if array size is valid. Called from from_any.
247 protected void checkArrayValid(Object members
)
248 throws TypeMismatch
, InvalidValue
250 checkBound(Array
.getLength(members
));
251 if (get_length() != Array
.getLength(members
))
252 set_length(Array
.getLength(members
));