1 2 12 package com.versant.core.jdo; 13 14 import java.util.Iterator ; 15 import java.util.ConcurrentModificationException ; 16 import java.util.NoSuchElementException ; 17 18 22 public class TransactionalList { 23 private final PMCacheEntry header = new PMCacheEntry(); 24 private int modCount; 25 26 public TransactionalList() { 27 header.processListNext = header.processListPrev = header; 28 } 29 30 private PMCacheEntry addBefore(PMCacheEntry o, PMCacheEntry e) { 31 modCount++; 32 o.processListNext = e; 33 o.processListPrev = e.processListPrev; 34 35 o.processListPrev.processListNext = o; 36 o.processListNext.processListPrev = o; 37 return o; 38 } 39 40 public boolean contains(PMCacheEntry ce) { 41 if (header == ce) return true; 42 return (ce.processListPrev != null || ce.processListNext != null); 43 } 44 45 public void clear() { 46 modCount++; 47 PMCacheEntry ce = header.processListNext; 48 for (;ce != null && ce != header; ce = ce.processListNext) { 49 ce.processListPrev.processListNext = null; 50 ce.processListPrev = null; 51 } 52 header.processListPrev.processListNext = null; 53 header.processListNext = header.processListPrev = header; 54 } 55 56 public void add(PCStateMan sm) { 57 if (!contains(sm.cacheEntry)) { 58 addBefore(sm.cacheEntry, header); 59 } 60 } 61 62 public void remove(PCStateMan sm) { 63 if (contains(sm.cacheEntry)) { 64 removeImp(sm.cacheEntry); 65 } 66 } 67 68 private void removeImp(PMCacheEntry ce) { 69 if (contains(ce)) { 70 modCount++; 71 ce.processListPrev.processListNext = ce.processListNext; 72 ce.processListNext.processListPrev = ce.processListPrev; 73 ce.processListPrev = null; 74 ce.processListNext = null; 75 } 76 } 77 78 public Iterator iterator() { 79 return new Iter(header, modCount); 80 } 81 82 class Iter implements Iterator { 83 PMCacheEntry current; 84 private int iterModCount; 85 86 public Iter(PMCacheEntry current, int iterModCount) { 87 this.current = current; 88 this.iterModCount = iterModCount; 89 } 90 91 public void remove() { 92 } 94 95 public boolean hasNext() { 96 checkConcurrent(); 97 return current.processListNext != header; 98 } 99 100 private void checkConcurrent() { 101 if (iterModCount != modCount) { 102 throw new ConcurrentModificationException (); 103 } 104 } 105 106 public Object next() { 107 if (hasNext()) { 108 current = current.processListNext; 109 } else { 110 throw new NoSuchElementException (); 111 } 112 return current; 113 } 114 } 115 } 116 | Popular Tags |