1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 21 import java.io.*; 22 import java.util.*; 23 import org.netbeans.mdr.persistence.*; 24 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.*; 25 26 42 public class VirtualList extends AbstractList { 43 private ArrayList keys = new ArrayList(); 44 private BtreeDatabase store; 45 private String name; 46 private BtreeStorage storage; 47 48 51 public VirtualList (String fileName, ObjectResolver resolver) { 52 this (defaultMap (fileName), resolver); 53 } 54 55 public VirtualList(Map properties, ObjectResolver resolver) { 56 try { 57 this.storage = (BtreeStorage) (new BtreeFactory()).createStorage(properties); 58 this.storage.create(true, resolver); 59 this.store = (BtreeDatabase)this.storage.getPrimaryIndex(); 60 } 61 catch (StorageException ex) { 62 throw new RuntimeStorageException(ex); 63 } 64 } 65 66 68 public void delete() { 69 try { 70 store.close(); 71 store.delete(name); 72 } 73 catch (StorageException ex) { 74 throw new RuntimeStorageException(ex); 75 } 76 } 77 78 82 public synchronized Object get(int index) { 83 try { 84 MOFID key = (MOFID)keys.get(index); 85 return store.get(key); 86 } 87 catch (StorageException ex) { 88 throw new RuntimeStorageException(ex); 89 } 90 } 91 92 94 95 public int size() { 96 return keys.size(); 97 } 98 99 104 public synchronized Object set(int index, Object element) { 105 try { 106 MOFID key = (MOFID)keys.get(index); 107 Object retval = store.getIfExists(key); 108 store.put(key, element); 109 return retval; 110 } 111 catch (StorageException ex) { 112 throw new RuntimeStorageException(ex); 113 } 114 } 115 116 120 public synchronized void add(int index, Object element) { 121 try { 122 MOFID key = new MOFID(this.storage); 123 keys.add(index, key); 124 store.add(key, element); 125 } 126 catch (StorageException ex) { 127 keys.remove(index); 128 throw new RuntimeStorageException(ex); 129 } 130 } 131 132 136 public synchronized Object remove(int index) { 137 try { 138 MOFID key = (MOFID)keys.get(index); 139 Object o = store.get(key); 140 store.remove(key); 141 keys.remove(index); 142 return o; 143 } 144 catch (StorageException ex) { 145 throw new RuntimeStorageException(ex); 146 } 147 } 148 149 152 public synchronized void changed(int index) { 153 try { 154 store.objectStateChanged((MOFID)keys.get(index)); 155 } 156 catch (StorageException ex) { 157 throw new RuntimeStorageException(ex); 158 } 159 } 160 161 162 private static Map defaultMap (String fileName) { 163 java.util.Hashtable map = new java.util.Hashtable (); 164 map.put (org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory.STORAGE_FILE_NAME,fileName); 165 return map; 166 } 167 } 168 | Popular Tags |