1 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 34 public class AttrImmutList extends AbstractList { 35 36 protected Object [] data; 37 private String 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 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 [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 (); 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 e) { 92 return null; 93 } 94 } 95 96 public Object 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 ) storable.getMdrStorage().values(storable.getMofId()).resolve(IOUtils.readInt(stream)); 106 int size = IOUtils.readInt(stream); 107 data = new Object [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 |