KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > dynamicany > DynSequenceImpl


1 /*
2  * @(#)DynSequenceImpl.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.dynamicany;
9
10 import org.omg.CORBA.TypeCode JavaDoc;
11 import org.omg.CORBA.Any JavaDoc;
12 import org.omg.CORBA.BAD_OPERATION JavaDoc;
13 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
14 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
15 import org.omg.CORBA.portable.InputStream JavaDoc;
16 import org.omg.CORBA.portable.OutputStream JavaDoc;
17 import org.omg.DynamicAny.*;
18 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
19 import org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
20 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc;
21
22 import com.sun.corba.se.spi.orb.ORB ;
23 import com.sun.corba.se.spi.logging.CORBALogDomains ;
24 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
25
26 // _REVIST_ Could make this a subclass of DynArrayImpl
27
// But that would mean that an object that implements DynSequence also implements DynArray
28
// which the spec doesn't mention (it also doesn't forbid it).
29
public class DynSequenceImpl extends DynAnyCollectionImpl implements DynSequence
30 {
31     //
32
// Constructors
33
//
34

35     private DynSequenceImpl() {
36         this(null, (Any JavaDoc)null, false);
37     }
38
39     protected DynSequenceImpl(ORB orb, Any JavaDoc any, boolean copyValue) {
40         super(orb, any, copyValue);
41     }
42
43     // Sets the current position to -1 and creates an empty sequence.
44
protected DynSequenceImpl(ORB orb, TypeCode JavaDoc typeCode) {
45         super(orb, typeCode);
46     }
47
48     // Initializes components and anys representation
49
// from the Any representation
50
protected boolean initializeComponentsFromAny() {
51         // This typeCode is of kind tk_sequence.
52
TypeCode JavaDoc typeCode = any.type();
53         int length;
54         TypeCode JavaDoc contentType = getContentType();
55         InputStream JavaDoc input;
56
57         try {
58             input = any.create_input_stream();
59         } catch (BAD_OPERATION JavaDoc e) {
60             return false;
61         }
62
63         length = input.read_long();
64         components = new DynAny[length];
65         anys = new Any JavaDoc[length];
66
67         for (int i=0; i<length; i++) {
68             // _REVISIT_ Could use read_xxx_array() methods on InputStream for efficiency
69
// but only for primitive types
70
anys[i] = DynAnyUtil.extractAnyFromStream(contentType, input, orb);
71             try {
72                 // Creates the appropriate subtype without copying the Any
73
components[i] = DynAnyUtil.createMostDerivedDynAny(anys[i], orb, false);
74             } catch (InconsistentTypeCode JavaDoc itc) { // impossible
75
}
76         }
77         return true;
78     }
79
80     // Sets the current position to -1 and creates an empty sequence.
81
protected boolean initializeComponentsFromTypeCode() {
82         // already done in the type code constructor
83
components = new DynAny[0];
84         anys = new Any JavaDoc[0];
85         return true;
86     }
87
88     // Collapses the whole DynAny hierarchys values into one single streamed Any
89
protected boolean initializeAnyFromComponents() {
90         OutputStream JavaDoc out = any.create_output_stream();
91         // Writing the length first is the only difference to supers implementation
92
out.write_long(components.length);
93         for (int i=0; i<components.length; i++) {
94             if (components[i] instanceof DynAnyImpl) {
95                 ((DynAnyImpl)components[i]).writeAny(out);
96             } else {
97                 // Not our implementation. Nothing we can do to prevent copying.
98
components[i].to_any().write_value(out);
99             }
100         }
101         any.read_value(out.create_input_stream(), any.type());
102         return true;
103     }
104
105
106     //
107
// DynSequence interface methods
108
//
109

110     // Returns the current length of the sequence
111
public int get_length() {
112         if (status == STATUS_DESTROYED) {
113         throw wrapper.dynAnyDestroyed() ;
114         }
115         return (checkInitComponents() ? components.length : 0);
116     }
117
118     // Sets the length of the sequence. Increasing the length of a sequence
119
// adds new elements at the tail without affecting the values of already
120
// existing elements. Newly added elements are default-initialized.
121
//
122
// Increasing the length of a sequence sets the current position to the first
123
// newly-added element if the previous current position was -1.
124
// Otherwise, if the previous current position was not -1,
125
// the current position is not affected.
126
//
127
// Increasing the length of a bounded sequence to a value larger than the bound
128
// raises InvalidValue.
129
//
130
// Decreasing the length of a sequence removes elements from the tail
131
// without affecting the value of those elements that remain.
132
// The new current position after decreasing the length of a sequence is determined
133
// as follows:
134
// ?f the length of the sequence is set to zero, the current position is set to -1.
135
// ?f the current position is -1 before decreasing the length, it remains at -1.
136
// ?f the current position indicates a valid element and that element is not removed
137
// when the length is decreased, the current position remains unaffected.
138
// ?f the current position indicates a valid element and that element is removed, the
139
// current position is set to -1.
140
public void set_length(int len)
141         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
142     {
143         if (status == STATUS_DESTROYED) {
144         throw wrapper.dynAnyDestroyed() ;
145         }
146         int bound = getBound();
147         if (bound > 0 && len > bound) {
148             throw new InvalidValue JavaDoc();
149         }
150
151         checkInitComponents();
152
153         int oldLength = components.length;
154         if (len > oldLength) {
155             // Increase length
156
DynAny[] newComponents = new DynAny[len];
157             Any JavaDoc[] newAnys = new Any JavaDoc[len];
158             System.arraycopy(components, 0, newComponents, 0, oldLength);
159             System.arraycopy(anys, 0, newAnys, 0, oldLength);
160             components = newComponents;
161             anys = newAnys;
162
163             // Newly added elements are default-initialized
164
TypeCode JavaDoc contentType = getContentType();
165             for (int i=oldLength; i<len; i++) {
166                 createDefaultComponentAt(i, contentType);
167             }
168
169             // Increasing the length of a sequence sets the current position to the first
170
// newly-added element if the previous current position was -1.
171
if (index == NO_INDEX)
172                 index = oldLength;
173         } else if (len < oldLength) {
174             // Decrease length
175
DynAny[] newComponents = new DynAny[len];
176             Any JavaDoc[] newAnys = new Any JavaDoc[len];
177             System.arraycopy(components, 0, newComponents, 0, len);
178             System.arraycopy(anys, 0, newAnys, 0, len);
179             // It is probably right not to destroy the released component DynAnys.
180
// Some other DynAny or a user variable might still hold onto them
181
// and if not then the garbage collector will take care of it.
182
//for (int i=len; i<oldLength; i++) {
183
// components[i].destroy();
184
//}
185
components = newComponents;
186             anys = newAnys;
187
188             // ?f the length of the sequence is set to zero, the current position is set to -1.
189
// ?f the current position is -1 before decreasing the length, it remains at -1.
190
// ?f the current position indicates a valid element and that element is not removed
191
// when the length is decreased, the current position remains unaffected.
192
// ?f the current position indicates a valid element and that element is removed,
193
// the current position is set to -1.
194
if (len == 0 || index >= len) {
195                 index = NO_INDEX;
196             }
197         } else {
198             // Length unchanged
199
// Maybe components is now default initialized from type code
200
if (index == NO_INDEX && len > 0) {
201                 index = 0;
202             }
203         }
204     }
205
206     // Initializes the elements of the sequence.
207
// The length of the DynSequence is set to the length of value.
208
// The current position is set to zero if value has non-zero length
209
// and to -1 if value is a zero-length sequence.
210
// If the length of value exceeds the bound of a bounded sequence,
211
// the operation raises InvalidValue.
212
// If value contains one or more elements whose TypeCode is not equivalent
213
// to the element TypeCode of the DynSequence, the operation raises TypeMismatch.
214
/*
215     public void set_elements(org.omg.CORBA.Any[] value)
216         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
217                org.omg.DynamicAny.DynAnyPackage.InvalidValue;
218 */

219
220     //
221
// Utility methods
222
//
223

224     protected void checkValue(Object JavaDoc[] value)
225         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
226     {
227         if (value == null || value.length == 0) {
228             clearData();
229             index = NO_INDEX;
230             return;
231         } else {
232             index = 0;
233         }
234         int bound = getBound();
235         if (bound > 0 && value.length > bound) {
236             throw new InvalidValue JavaDoc();
237         }
238     }
239 }
240
Popular Tags