1 19 20 package org.netbeans.mdr.storagemodel.transientimpl; 21 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.ListIterator ; 27 import java.util.Iterator ; 28 import org.netbeans.mdr.persistence.MultivaluedOrderedIndex; 29 import org.netbeans.mdr.persistence.StorageException; 30 import org.netbeans.mdr.persistence.StorageBadRequestException; 31 import org.netbeans.mdr.persistence.Storage; 32 import org.netbeans.mdr.persistence.SinglevaluedIndex; 33 import org.netbeans.mdr.storagemodel.MdrStorage; 34 import org.netbeans.mdr.util.DebugException; 35 36 37 38 42 public class TransientMultivaluedOrderedIndex extends TransientMultivaluedIndex implements MultivaluedOrderedIndex { 43 44 public class OrderedSlotCollection extends SlotCollection implements List { 45 46 47 public OrderedSlotCollection(Object key, Collection st) { 48 this(key, st, null); 49 } 50 51 public OrderedSlotCollection(Object key, Collection st, int index, int toIndex) { 52 this(key, st, null, index, toIndex); 53 } 54 55 public OrderedSlotCollection(Object key, Collection st, SinglevaluedIndex repos) { 56 super(key, st, repos); 57 } 58 59 public OrderedSlotCollection(Object key, Collection st, SinglevaluedIndex repos, int fromIndex, int toIndex) { 60 super(key, ((List )st).subList(fromIndex,toIndex), repos); 61 } 62 63 public void add(int index, Object element) { 64 if (this.repos != null) 65 throw new UnsupportedOperationException (); 66 try { 67 addToList((List )this.st, index, this.key, element); 68 txlog.push( new CompensatingTransaction.AddOrderedCTx(this.key, element, index)); 69 } catch (StorageException se) { 70 throw new DebugException(se.toString()); 71 } 72 } 73 74 public boolean addAll(int index, Collection c) { 75 boolean result = false; 76 for (Iterator it = c.iterator(); it.hasNext(); index++) { 77 this.add(index, it.next()); 78 result = true; 79 } 80 return result; 81 } 82 83 public Object get(int index) { 84 Entry e = (Entry) ((List )this.st).get(index); 85 if (e == null) 86 return null; 87 if (!e.isValid()) { 88 ((List )this.st).remove(index); 89 e.dispose(); 90 return null; 91 } 92 try { 93 return (this.repos == null) ? e.getValue() : map(this.key, e.getValue(), this.repos); 94 } catch (StorageException se) { 95 throw new DebugException(se.toString()); 96 } 97 } 98 99 public int indexOf(Object o) { 100 if (this.repos != null) 101 throw new UnsupportedOperationException (); 102 List l = (List ) this.st; 103 for (int i= 0; i < l.size(); i++) { 104 Entry e = (Entry) l.get(i); 105 if (!e.isValid()) { 106 l.remove(i); 107 e.dispose(); 108 i--; 109 } 110 else if (o.equals(e.getValue())) { 111 return i; 112 } 113 } 114 return -1; 115 } 116 117 public int lastIndexOf(Object o) { 118 if (this.repos != null) 119 throw new UnsupportedOperationException (); 120 List l = (List ) this.st; 121 for (int i = l.size()-1; i>=0; i--) { 122 Entry e = (Entry) l.get(i); 123 if (!e.isValid()) { 124 l.remove(i); 125 e.dispose(); 126 } 127 else if (o.equals(e.getValue())) { 128 return i; 129 } 130 } 131 return -1; 132 } 133 134 public ListIterator listIterator() { 135 return new OrderedSlotIterator(this.key, (List )this.st, this.repos); 136 } 137 138 139 public ListIterator listIterator(int index) { 140 return new OrderedSlotIterator(this.key, (List )this.st, this.repos, index); 141 } 142 143 public Object remove(int index) { 144 if (this.repos != null) 145 throw new UnsupportedOperationException (); 146 if (index < 0 || index >= this.st.size()) 147 throw new IndexOutOfBoundsException (); 148 try { 149 Object result = removeFromList((List )this.st, index); 150 txlog.push( new CompensatingTransaction.RemoveOrderedCTx(this.key, result, index)); 151 return result; 152 } catch (StorageException se) { 153 throw new DebugException(se.toString()); 154 } 155 } 156 157 158 public Object set(int index, Object element) { 159 if (this.repos != null) 160 throw new UnsupportedOperationException (); 161 try { 162 Object result = setInList((List )this.st, index, this.key, element); 163 if (result != null) { 164 txlog.push(new CompensatingTransaction.RemoveOrderedCTx(key, result, index)); 165 } 166 txlog.push(new CompensatingTransaction.AddOrderedCTx(key, result, index)); 167 return result; 168 } catch (StorageException se) { 169 throw new DebugException(se.toString()); 170 } 171 } 172 173 public List subList(int fromIndex, int toIndex) { 174 return new OrderedSlotCollection(this.key, this.st, this.repos, fromIndex, toIndex); 175 } 176 177 } 178 179 180 protected class OrderedSlotIterator extends SlotIterator implements ListIterator { 181 182 private Entry prev; 183 private int position; 184 185 public OrderedSlotIterator(List l) { 186 this(null, l, null, 0); 187 } 188 189 public OrderedSlotIterator(List l, int index) { 190 this(null, l, null, index); 191 } 192 193 public OrderedSlotIterator(Object key, List l, SinglevaluedIndex repos) { 194 this(null, l, repos, 0); 195 } 196 197 public OrderedSlotIterator(Object key, List l, SinglevaluedIndex repos, int index) { 198 super(key, l, repos, (index == 0) ? l.listIterator() : l.listIterator(index)); 199 } 200 201 public Object next() { 202 while (this.top == null) { 203 this.top = (Entry) this.innerIt.next(); 204 if (! this.top.isValid()) { 205 this.innerIt.remove(); 206 this.top.dispose(); 207 this.top = null; 208 } 209 } 210 this.last = this.top; 211 this.top = prev = null; 212 try { 213 Object result = map(this.key, this.last.getValue(), this.repos); 214 this.position++; 215 return result; 216 }catch (StorageException se) { 217 throw new DebugException(se.toString()); 218 } 219 } 220 221 public boolean hasPrevious() { 222 while (this.prev == null) { 223 if (!((ListIterator )this.innerIt).hasPrevious()) 224 return false; 225 this.prev = (Entry) ((ListIterator )this.innerIt).previous(); 226 if (! this.prev.isValid()) { 227 this.innerIt.remove(); 228 this.prev.dispose(); 229 this.prev = null; 230 } 231 } 232 return true; 233 } 234 235 public Object previous() { 236 while (this.prev == null) { 237 this.prev = (Entry) ((ListIterator )this.innerIt).previous(); 238 if (! this.prev.isValid()) { 239 this.innerIt.remove(); 240 this.prev.dispose(); 241 this.prev = null; 242 } 243 } 244 this.last = this.prev; 245 this.top = prev = null; 246 try { 247 Object result = map(this.key, this.last.getValue(), this.repos); 248 this.position--; 249 return result; 250 }catch (StorageException se) { 251 throw new DebugException(se.toString()); 252 } 253 } 254 255 public int nextIndex() { 256 return ((ListIterator )this.innerIt).nextIndex(); 257 } 258 259 public int previousIndex() { 260 return ((ListIterator )this.innerIt).previousIndex(); 261 } 262 263 public void add(Object o) { 264 if (this.repos != null) 265 throw new UnsupportedOperationException (); 266 if (this.last == null) 267 throw new IllegalStateException (); 268 if (unique && this.collection.contains(o)) 269 throw new IllegalStateException ("Object already contained in unique index."); 270 try { 271 Entry e = new Entry(this.key, o); 272 ((ListIterator )innerIt).add(e); 273 txlog.push(new CompensatingTransaction.AddOrderedCTx(this.key, o, this.position)); 274 }catch (StorageException se) { 275 throw new DebugException(); 276 } 277 } 278 279 public void set(Object o) { 280 if (this.repos != null) 281 throw new UnsupportedOperationException (); 282 if (this.last == null) 283 throw new IllegalStateException (); 284 if (unique && this.collection.contains(o)) 285 throw new IllegalStateException ("Object already contained in unique index."); 286 try { 287 Entry e = new Entry(this.key, o); 288 ((ListIterator )innerIt).set(e); 289 txlog.push( new CompensatingTransaction.RemoveOrderedCTx(this.key,this.last.getValue(),this.position)); 290 txlog.push( new CompensatingTransaction.AddOrderedCTx(this.key, o, this.position)); 291 } catch (StorageException se) { 292 throw new DebugException(); 293 } 294 } 295 296 } 297 298 299 public TransientMultivaluedOrderedIndex(MdrStorage storage, String name, Storage.EntryType keyType, Storage.EntryType valueType, boolean unique) { 300 super(storage, name, keyType, valueType, unique); 301 } 302 303 310 public void add(Object key, int index, Object value) throws StorageException { 311 if (this.map == null) { 312 this.map = new HashMap (); 313 } 314 List c = (List ) this.map.get(key); 316 if (c == null) { 317 c = new ArrayList (); 318 this.map.put(key, c); 319 } 320 this.addToList(c, index, key, value); 321 this.txlog.push(new CompensatingTransaction.AddOrderedCTx(key, value, index)); 322 } 323 324 protected SlotCollection createSlotCollection (Object key, Collection c) { 325 return new OrderedSlotCollection (key, c); 326 } 327 328 protected SlotCollection createSlotCollection (Object key, Collection c, SinglevaluedIndex repos) { 329 return new OrderedSlotCollection (key, c, repos); 330 } 331 332 338 public java.util.List getItemsOrdered(Object key) throws StorageException { 339 return (List ) this.getItems(key); 340 } 341 342 348 public Collection getObjectsOrdered(Object key, SinglevaluedIndex repos) throws StorageException { 349 return this.getObjects(key, repos); 350 } 351 352 358 public boolean remove(Object key, int index) throws StorageException { 359 if (this.map == null) 360 return false; 361 List list = (List ) this.map.get(key); 363 Object result = this.removeFromList(list, index); 364 if (result != null) 365 this.txlog.push(new CompensatingTransaction.RemoveOrderedCTx(key, result, index)); 366 return result != null; 367 } 368 369 377 public void replace(Object key, int index, Object element) throws StorageException { 378 if (this.map == null) 379 throw new StorageBadRequestException(); 380 List list = (List ) this.map.get(key); 382 Object result = this.setInList(list, index, key, element); 383 if (result != null) { 384 this.txlog.push(new CompensatingTransaction.RemoveOrderedCTx(key, result, index)); 385 } 386 this.txlog.push(new CompensatingTransaction.AddOrderedCTx(key, result, index)); 387 } 388 389 protected void addNoTx (Object key, Object value, int index) throws StorageException { 390 if (map == null) 391 return; List l = (List ) this.map.get (key); 393 this.addToList (l, index, key, value); 394 } 395 396 protected Object removeNoTx (Object key, Object value, int index) throws StorageException { 397 if (map == null) 398 return null; List l = (List ) this.map.get (key); 400 return this.removeFromList (l, index); 401 } 402 403 private void addToList(List c, int index, Object key, Object value) throws StorageException { 404 if (this.unique) { 405 for (Iterator it = c.iterator(); it.hasNext();) { 406 Entry e = (Entry) it.next(); 407 if (value.equals(e.getValue())) 408 throw new StorageBadRequestException("Value: "+value+" is already contained."); 409 } 410 } 411 Entry e = new Entry(key, value); 412 c.add(index, e); 413 } 414 415 private Object removeFromList(List list, int index) throws StorageException { 416 if (list == null) 417 return null; 418 Entry e = (Entry) list.remove(index); 419 Object value = e.getValue(); 420 e.dispose(); 421 return value; 422 } 423 424 private Object setInList(List list, int index,Object key, Object value) throws StorageException { 425 if (list == null) 426 throw new StorageBadRequestException(); 427 Entry newEntry = new Entry(key, value); 428 Entry old = (Entry) list.set(index, newEntry); 429 if (old != null) { 430 old.dispose(); 431 return old.getValue(); 432 } 433 else 434 return null; 435 } 436 437 } 438 | Popular Tags |