DynAnyCollectionImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.dynamicany;
27
28import org.omg.CORBA.TypeCode;
29import org.omg.CORBA.Any;
30import org.omg.CORBA.NO_IMPLEMENT;
31import org.omg.CORBA.TypeCodePackage.BadKind;
32import org.omg.CORBA.TypeCodePackage.Bounds;
33import org.omg.DynamicAny.*;
34import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
35import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
36import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
37
38import com.sun.corba.se.spi.orb.ORB ;
39import com.sun.corba.se.spi.logging.CORBALogDomains ;
40import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
41
42abstract class DynAnyCollectionImpl extends DynAnyConstructedImpl
43{
44    //
45    // Instance variables
46    //
47
48    // Keep in sync with DynAny[] components at all times.
49    Any[] anys = null;
50
51    //
52    // Constructors
53    //
54
55    private DynAnyCollectionImpl() {
56        this(null, (Any)null, false);
57    }
58
59    protected DynAnyCollectionImpl(ORB orb, Any any, boolean copyValue) {
60        super(orb, any, copyValue);
61    }
62
63    protected DynAnyCollectionImpl(ORB orb, TypeCode typeCode) {
64        super(orb, typeCode);
65    }
66
67    //
68    // Utility methods
69    //
70
71    protected void createDefaultComponentAt(int i, TypeCode contentType) {
72        try {
73            components[i] = DynAnyUtil.createMostDerivedDynAny(contentType, orb);
74        } catch (InconsistentTypeCode itc) { // impossible
75        }
76        // get a hold of the default initialized Any without copying
77        anys[i] = getAny(components[i]);
78    }
79
80    protected TypeCode getContentType() {
81        try {
82            return any.type().content_type();
83        } catch (BadKind badKind) { // impossible
84            return null;
85        }
86    }
87
88    // This method has a different meaning for sequence and array:
89    // For sequence value of 0 indicates an unbounded sequence,
90    // values > 0 indicate a bounded sequence.
91    // For array any value indicates the boundary.
92    protected int getBound() {
93        try {
94            return any.type().length();
95        } catch (BadKind badKind) { // impossible
96            return 0;
97        }
98    }
99
100    //
101    // DynAny interface methods
102    //
103
104    // _REVISIT_ More efficient copy operation
105
106    //
107    // Collection methods
108    //
109
110    public org.omg.CORBA.Any[] get_elements () {
111        if (status == STATUS_DESTROYED) {
112            throw wrapper.dynAnyDestroyed() ;
113        }
114        return (checkInitComponents() ? anys : null);
115    }
116
117    protected abstract void checkValue(Object[] value)
118        throws org.omg.DynamicAny.DynAnyPackage.InvalidValue;
119
120    // Initializes the elements of the ordered collection.
121    // If value does not contain the same number of elements as the array dimension,
122    // the operation raises InvalidValue.
123    // If one or more elements have a type that is inconsistent with the collections TypeCode,
124    // the operation raises TypeMismatch.
125    // This operation does not change the current position.
126    public void set_elements (org.omg.CORBA.Any[] value)
127        throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
128               org.omg.DynamicAny.DynAnyPackage.InvalidValue
129    {
130        if (status == STATUS_DESTROYED) {
131            throw wrapper.dynAnyDestroyed() ;
132        }
133        checkValue(value);
134
135        components = new DynAny[value.length];
136        anys = value;
137
138        // We know that this is of kind tk_sequence or tk_array
139        TypeCode expectedTypeCode = getContentType();
140        for (int i=0; i<value.length; i++) {
141            if (value[i] != null) {
142                if (! value[i].type().equal(expectedTypeCode)) {
143                    clearData();
144                    // _REVISIT_ More info
145                    throw new TypeMismatch();
146                }
147                try {
148                    // Creates the appropriate subtype without copying the Any
149                    components[i] = DynAnyUtil.createMostDerivedDynAny(value[i], orb, false);
150                    //System.out.println(this + " created component " + components[i]);
151                } catch (InconsistentTypeCode itc) {
152                    throw new InvalidValue();
153                }
154            } else {
155                clearData();
156                // _REVISIT_ More info
157                throw new InvalidValue();
158            }
159        }
160        index = (value.length == 0 ? NO_INDEX : 0);
161        // Other representations are invalidated by this operation
162        representations = REPRESENTATION_COMPONENTS;
163    }
164
165    public org.omg.DynamicAny.DynAny[] get_elements_as_dyn_any () {
166        if (status == STATUS_DESTROYED) {
167            throw wrapper.dynAnyDestroyed() ;
168        }
169        return (checkInitComponents() ? components : null);
170    }
171
172    // Same semantics as set_elements(Any[])
173    public void set_elements_as_dyn_any (org.omg.DynamicAny.DynAny[] value)
174        throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
175               org.omg.DynamicAny.DynAnyPackage.InvalidValue
176    {
177        if (status == STATUS_DESTROYED) {
178            throw wrapper.dynAnyDestroyed() ;
179        }
180        checkValue(value);
181
182        components = (value == null ? emptyComponents : value);
183        anys = new Any[value.length];
184
185        // We know that this is of kind tk_sequence or tk_array
186        TypeCode expectedTypeCode = getContentType();
187        for (int i=0; i<value.length; i++) {
188            if (value[i] != null) {
189                if (! value[i].type().equal(expectedTypeCode)) {
190                    clearData();
191                    // _REVISIT_ More info
192                    throw new TypeMismatch();
193                }
194                anys[i] = getAny(value[i]);
195            } else {
196                clearData();
197                // _REVISIT_ More info
198                throw new InvalidValue();
199            }
200        }
201        index = (value.length == 0 ? NO_INDEX : 0);
202        // Other representations are invalidated by this operation
203        representations = REPRESENTATION_COMPONENTS;
204    }
205}
206