KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DynAnyCollectionImpl.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.NO_IMPLEMENT JavaDoc;
13 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
14 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
15 import org.omg.DynamicAny.*;
16 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
17 import org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
18 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc;
19
20 import com.sun.corba.se.spi.orb.ORB ;
21 import com.sun.corba.se.spi.logging.CORBALogDomains ;
22 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
23
24 abstract class DynAnyCollectionImpl extends DynAnyConstructedImpl
25 {
26     //
27
// Instance variables
28
//
29

30     // Keep in sync with DynAny[] components at all times.
31
Any JavaDoc[] anys = null;
32
33     //
34
// Constructors
35
//
36

37     private DynAnyCollectionImpl() {
38         this(null, (Any JavaDoc)null, false);
39     }
40
41     protected DynAnyCollectionImpl(ORB orb, Any JavaDoc any, boolean copyValue) {
42         super(orb, any, copyValue);
43     }
44
45     protected DynAnyCollectionImpl(ORB orb, TypeCode JavaDoc typeCode) {
46         super(orb, typeCode);
47     }
48
49     //
50
// Utility methods
51
//
52

53     protected void createDefaultComponentAt(int i, TypeCode JavaDoc contentType) {
54         try {
55             components[i] = DynAnyUtil.createMostDerivedDynAny(contentType, orb);
56         } catch (InconsistentTypeCode JavaDoc itc) { // impossible
57
}
58         // get a hold of the default initialized Any without copying
59
anys[i] = getAny(components[i]);
60     }
61
62     protected TypeCode JavaDoc getContentType() {
63         try {
64             return any.type().content_type();
65         } catch (BadKind JavaDoc badKind) { // impossible
66
return null;
67         }
68     }
69
70     // This method has a different meaning for sequence and array:
71
// For sequence value of 0 indicates an unbounded sequence,
72
// values > 0 indicate a bounded sequence.
73
// For array any value indicates the boundary.
74
protected int getBound() {
75         try {
76             return any.type().length();
77         } catch (BadKind JavaDoc badKind) { // impossible
78
return 0;
79         }
80     }
81
82     //
83
// DynAny interface methods
84
//
85

86     // _REVISIT_ More efficient copy operation
87

88     //
89
// Collection methods
90
//
91

92     public org.omg.CORBA.Any JavaDoc[] get_elements () {
93         if (status == STATUS_DESTROYED) {
94         throw wrapper.dynAnyDestroyed() ;
95         }
96         return (checkInitComponents() ? anys : null);
97     }
98
99     protected abstract void checkValue(Object JavaDoc[] value)
100         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
101
102     // Initializes the elements of the ordered collection.
103
// If value does not contain the same number of elements as the array dimension,
104
// the operation raises InvalidValue.
105
// If one or more elements have a type that is inconsistent with the collections TypeCode,
106
// the operation raises TypeMismatch.
107
// This operation does not change the current position.
108
public void set_elements (org.omg.CORBA.Any JavaDoc[] value)
109         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
110                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
111     {
112         if (status == STATUS_DESTROYED) {
113         throw wrapper.dynAnyDestroyed() ;
114         }
115         checkValue(value);
116
117         components = new DynAny[value.length];
118         anys = value;
119
120         // We know that this is of kind tk_sequence or tk_array
121
TypeCode JavaDoc expectedTypeCode = getContentType();
122         for (int i=0; i<value.length; i++) {
123             if (value[i] != null) {
124                 if (! value[i].type().equal(expectedTypeCode)) {
125                     clearData();
126                     // _REVISIT_ More info
127
throw new TypeMismatch JavaDoc();
128                 }
129                 try {
130                     // Creates the appropriate subtype without copying the Any
131
components[i] = DynAnyUtil.createMostDerivedDynAny(value[i], orb, false);
132                     //System.out.println(this + " created component " + components[i]);
133
} catch (InconsistentTypeCode JavaDoc itc) {
134                     throw new InvalidValue JavaDoc();
135                 }
136             } else {
137                 clearData();
138                 // _REVISIT_ More info
139
throw new InvalidValue JavaDoc();
140             }
141         }
142         index = (value.length == 0 ? NO_INDEX : 0);
143         // Other representations are invalidated by this operation
144
representations = REPRESENTATION_COMPONENTS;
145     }
146
147     public org.omg.DynamicAny.DynAny JavaDoc[] get_elements_as_dyn_any () {
148         if (status == STATUS_DESTROYED) {
149         throw wrapper.dynAnyDestroyed() ;
150         }
151         return (checkInitComponents() ? components : null);
152     }
153
154     // Same semantics as set_elements(Any[])
155
public void set_elements_as_dyn_any (org.omg.DynamicAny.DynAny JavaDoc[] value)
156         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
157                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
158     {
159         if (status == STATUS_DESTROYED) {
160         throw wrapper.dynAnyDestroyed() ;
161         }
162         checkValue(value);
163
164         components = (value == null ? emptyComponents : value);
165         anys = new Any JavaDoc[value.length];
166
167         // We know that this is of kind tk_sequence or tk_array
168
TypeCode JavaDoc expectedTypeCode = getContentType();
169         for (int i=0; i<value.length; i++) {
170             if (value[i] != null) {
171                 if (! value[i].type().equal(expectedTypeCode)) {
172                     clearData();
173                     // _REVISIT_ More info
174
throw new TypeMismatch JavaDoc();
175                 }
176                 anys[i] = getAny(value[i]);
177             } else {
178                 clearData();
179                 // _REVISIT_ More info
180
throw new InvalidValue JavaDoc();
181             }
182         }
183         index = (value.length == 0 ? NO_INDEX : 0);
184         // Other representations are invalidated by this operation
185
representations = REPRESENTATION_COMPONENTS;
186     }
187 }
188
Popular Tags