KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > TransactionalList


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.ConcurrentModificationException JavaDoc;
16 import java.util.NoSuchElementException JavaDoc;
17
18 /**
19  * This is a Link List implementation of {@link PMCacheEntry} instances that must
20  * be processed at the end of the transaction.
21  */

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 JavaDoc iterator() {
79         return new Iter(header, modCount);
80     }
81
82     class Iter implements Iterator JavaDoc {
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             //To change body of implemented methods use File | Settings | File Templates.
93
}
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 JavaDoc();
103             }
104         }
105
106         public Object JavaDoc next() {
107             if (hasNext()) {
108                 current = current.processListNext;
109             } else {
110                 throw new NoSuchElementException JavaDoc();
111             }
112             return current;
113         }
114     }
115 }
116
Popular Tags