KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.*;
23
24 import javax.jmi.reflect.*;
25
26 import org.netbeans.mdr.handlers.BaseObjectHandler;
27 import org.netbeans.mdr.persistence.StorageException;
28 import org.netbeans.mdr.persistence.MOFID;
29 import org.netbeans.mdr.util.IOUtils;
30
31 /**
32  * @author Martin Matula
33  */

34 public class AttrImmutList extends AbstractList {
35
36     protected Object JavaDoc[] data;
37     private String JavaDoc typeName;
38
39     public AttrImmutList() {
40     }
41
42     AttrImmutList(StorableFeatured mdrObject, StorableClass.AttributeDescriptor desc) throws StorageException {
43         this(mdrObject, desc, null);
44     }
45     
46     AttrImmutList(StorableFeatured mdrObject, StorableClass.AttributeDescriptor desc, Collection values) throws StorageException {
47         Class JavaDoc type = desc.getType();
48         if (RefEnum.class.isAssignableFrom(type) || RefStruct.class.isAssignableFrom(type)) {
49             typeName = type.getName();
50         } else {
51             typeName = null;
52         }
53         
54         if (values == null) {
55             data = new Object JavaDoc[0];
56         } else {
57             data = values.toArray();
58             for (int i = 0; i < data.length; i++) {
59                 if (data[i] == null) {
60                     throw new NullPointerException JavaDoc();
61                 }
62                 if (!type.isInstance(data[i])) {
63                     throw new TypeMismatchException(type, data[i], getMetaElement(mdrObject.getMdrStorage(), desc.getMofId()));
64                 }
65             }
66         }
67
68         if ((data.length < desc.getMinSize()) || ((desc.getMaxSize() > -1) && (data.length > desc.getMaxSize()))) {
69             throw new WrongSizeException(getMetaElement(mdrObject.getMdrStorage(), desc.getMofId()));
70         }
71
72         if (type.isInstance(RefObject.class)) {
73             setAttribComposite(mdrObject.getMofId(), values, desc.getMofId());
74         }
75     }
76     
77     protected void setAttribComposite(MOFID compositeId, RefObject object, MOFID metaMofId) throws StorageException {
78         StorableObject storable = (StorableObject) ((BaseObjectHandler) object)._getDelegate();
79         storable.setComposite(compositeId, storable.getMofId(), metaMofId);
80     }
81     
82     protected void setAttribComposite(MOFID compositeId, Collection list, MOFID metaMofId) throws StorageException {
83         for (Iterator it = list.iterator(); it.hasNext();) {
84             setAttribComposite(compositeId, (RefObject) it.next(), metaMofId);
85         }
86     }
87
88     protected RefObject getMetaElement(MdrStorage mdrStorage, MOFID mofId) {
89         try {
90             return (RefObject) mdrStorage.getRepository().getHandler(mdrStorage.getObject(mofId));
91         } catch (Exception JavaDoc e) {
92             return null;
93         }
94     }
95     
96     public Object JavaDoc get(int index) {
97         return data[index];
98     }
99     
100     public int size() {
101         return data.length;
102     }
103     
104     public void read(InputStream stream, StorableBaseObject storable) throws IOException {
105         typeName = (String JavaDoc) storable.getMdrStorage().values(storable.getMofId()).resolve(IOUtils.readInt(stream));
106         int size = IOUtils.readInt(stream);
107         data = new Object JavaDoc[size];
108         for (int i = 0; i < size; i++) {
109             data[i] = IOUtils.read(stream, storable);
110         }
111     }
112     
113     public void write(OutputStream stream, StorableBaseObject storable) throws IOException {
114         IOUtils.writeInt(stream, storable.getMdrStorage().values(storable.getMofId()).indexOf(typeName));
115         IOUtils.writeInt(stream, data.length);
116         for (int i = 0; i < data.length; i++) {
117             IOUtils.write(stream, data[i], storable);
118         }
119     }
120 }
121
Popular Tags