KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > DatatypeDescriptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.storagemodel;
20
21 import java.util.*;
22
23 import org.netbeans.mdr.util.*;
24 import org.netbeans.mdr.handlers.BaseObjectHandler;
25
26 /**
27  *
28  * @author Petr Hrebejk, Martin Matula
29  */

30
31 public class DatatypeDescriptor extends Object JavaDoc {
32     private final int memberIDs[];
33     private final int memberTypeIDs[];
34     private final int ifcIndex;
35     private final int typeNameIndex;
36
37     private final transient MdrStorage storage;
38
39     // name of the interface that the datatype maps to
40
private transient String JavaDoc ifcName = null;
41     // list of datatype members (enum. literals/structure fields)
42
private transient List members = null;
43     // list of datatypes of members (applicable for structure fields only)
44
private transient List memberTypes = null;
45     // fully qualified name of datatype
46
// (stored only for structs - FQNs for enums are hardcoded in their impls)
47
private transient List typeName = null;
48     // storage id
49
private transient String JavaDoc storageId = null;
50
51     // used for constructing structure descriptor
52
public DatatypeDescriptor(MdrStorage storage, List typeName, List members, List memberTypes, String JavaDoc ifcName, String JavaDoc storageId) {
53         this.storage = storage;
54         this.ifcName = ifcName;
55         this.storageId = storageId;
56         this.ifcIndex = storage.storageValues(this.storageId).store(ifcName);
57         this.members = Collections.unmodifiableList(members);
58         this.memberIDs = new int[members.size()];
59         for (int i = 0; i < memberIDs.length; i++) {
60             memberIDs[i] = storage.storageValues(this.storageId).store(members.get(i));
61         }
62         if (typeName == null) {
63             this.typeNameIndex = 0;
64         } else {
65             this.typeName = Collections.unmodifiableList(typeName);
66             this.typeNameIndex = storage.storageValues (this.storageId).store(typeName);
67         }
68         if (memberTypes == null) {
69             this.memberTypeIDs = null;
70         } else {
71             this.memberTypes = Collections.unmodifiableList(memberTypes);
72             memberTypeIDs = new int[memberTypes.size()];
73             for (int i = 0; i < memberTypeIDs.length; i++) {
74                 memberTypeIDs[i] = storage.storageValues(this.storageId).store(((Class JavaDoc) memberTypes.get(i)).getName());
75             }
76         }
77     }
78     
79     // used for creating enumeration descriptor
80
public DatatypeDescriptor(MdrStorage storage, List members, String JavaDoc ifcName, String JavaDoc storageId) {
81         this(storage, null, members, null, ifcName, storageId);
82     }
83     
84     private DatatypeDescriptor(MdrStorage storage, int typeNameIndex, int[] memberIDs, int[] memberTypeIDs, int ifcIndex, String JavaDoc storageId) {
85         this.storage = storage;
86         this.typeNameIndex = typeNameIndex;
87         this.memberIDs = memberIDs;
88         this.memberTypeIDs = memberTypeIDs;
89         this.ifcIndex = ifcIndex;
90         this.storageId = storageId;
91     }
92     
93     public synchronized List getMembers() {
94         if (members == null) {
95             members = new ArrayList(memberIDs.length);
96             for (int i = 0; i < memberIDs.length; i++) {
97                 members.add(storage.storageValues(this.storageId).resolve(memberIDs[i]));
98             }
99             members = Collections.unmodifiableList(members);
100         }
101         return members;
102     }
103
104     public synchronized List getMemberTypes() {
105         if (memberTypeIDs == null) return null;
106         if (memberTypes == null) {
107             memberTypes = new ArrayList(memberTypeIDs.length);
108             for (int i = 0; i < memberTypeIDs.length; i++) {
109                 try {
110                     memberTypes.add(BaseObjectHandler.resolveInterface((String JavaDoc) storage.storageValues(this.storageId).resolve(memberTypeIDs[i])));
111                 } catch (ClassNotFoundException JavaDoc e) {
112                     throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
113                 }
114             }
115             memberTypes = Collections.unmodifiableList(memberTypes);
116         }
117         return memberTypes;
118     }
119
120     public synchronized List getTypeName() {
121         if (typeName == null) {
122             typeName = (List) storage.storageValues(this.storageId).resolve(typeNameIndex);
123         }
124         return typeName;
125     }
126
127     public synchronized String JavaDoc getIfcName() {
128         if (ifcName == null) {
129             ifcName = (String JavaDoc) storage.storageValues(this.storageId).resolve(ifcIndex);
130         }
131         
132         return ifcName;
133     }
134     
135     static DatatypeDescriptor readResolve(java.io.InputStream JavaDoc inputStream, StorableBaseObject storable) throws java.io.IOException JavaDoc {
136         int typeNameIndex = IOUtils.readInt(inputStream);
137         int[] memberIDs = new int[IOUtils.readInt(inputStream)];
138         for (int i = 0; i < memberIDs.length; i++) {
139             memberIDs[i] = IOUtils.readInt(inputStream);
140         }
141         int size = IOUtils.readInt(inputStream);
142         int[] memberTypeIDs = null;
143         if (size > 0) {
144             memberTypeIDs = new int[size - 1];
145             for (int i = 0; i < size - 1; i++) {
146                 memberTypeIDs[i] = IOUtils.readInt(inputStream);
147             }
148         }
149         
150         return new DatatypeDescriptor(storable.getMdrStorage(), typeNameIndex, memberIDs, memberTypeIDs, IOUtils.readInt(inputStream), MdrStorage.getStorageIdFromMofId(storable.getMofId()));
151     }
152     
153     void write(java.io.OutputStream JavaDoc outputStream) throws java.io.IOException JavaDoc {
154         IOUtils.writeInt(outputStream, typeNameIndex);
155         IOUtils.writeInt(outputStream, memberIDs.length);
156         for (int i = 0; i < memberIDs.length; i++) {
157             IOUtils.writeInt(outputStream, memberIDs[i]);
158         }
159         if (memberTypeIDs == null) {
160             IOUtils.writeInt(outputStream, 0);
161         } else {
162             IOUtils.writeInt(outputStream, memberTypeIDs.length + 1);
163             for (int i = 0; i < memberTypeIDs.length; i++) {
164                 IOUtils.writeInt(outputStream, memberTypeIDs[i]);
165             }
166         }
167         IOUtils.writeInt(outputStream, ifcIndex);
168     }
169 }
170
Popular Tags