001: /*
002: File: SyncList.java
003:
004: Originally written by Doug Lea and released into the public domain.
005: This may be used for any purposes whatsoever without acknowledgment.
006: Thanks for the assistance and support of Sun Microsystems Labs,
007: and everyone contributing, testing, and using this code.
008:
009: History:
010: Date Who What
011: 1Aug1998 dl Create public version
012: */
013:
014: package EDU.oswego.cs.dl.util.concurrent;
015:
016: import java.util.*;
017:
018: /**
019: * SyncLists wrap Sync-based control around java.util.Lists.
020: * They support the following additional reader operations over
021: * SyncCollection: hashCode, equals, get, indexOf, lastIndexOf,
022: * subList. They support additional writer operations remove(int),
023: * set(int), add(int), addAll(int). The corresponding listIterators
024: * and are similarly extended.
025: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
026: * @see SyncCollection
027: **/
028:
029: public class SyncList extends SyncCollection implements List {
030:
031: /**
032: * Create a new SyncList protecting the given collection,
033: * and using the given sync to control both reader and writer methods.
034: * Common, reasonable choices for the sync argument include
035: * Mutex, ReentrantLock, and Semaphores initialized to 1.
036: **/
037: public SyncList(List list, Sync sync) {
038: super (list, sync);
039: }
040:
041: /**
042: * Create a new SyncList protecting the given list,
043: * and using the given ReadWriteLock to control reader and writer methods.
044: **/
045: public SyncList(List list, ReadWriteLock rwl) {
046: super (list, rwl.readLock(), rwl.writeLock());
047: }
048:
049: /**
050: * Create a new SyncList protecting the given list,
051: * and using the given pair of locks to control reader and writer methods.
052: **/
053: public SyncList(List list, Sync readLock, Sync writeLock) {
054: super (list, readLock, writeLock);
055: }
056:
057: protected List baseList() {
058: return (List) c_;
059: }
060:
061: public int hashCode() {
062: boolean wasInterrupted = beforeRead();
063: try {
064: return c_.hashCode();
065: } finally {
066: afterRead(wasInterrupted);
067: }
068: }
069:
070: public boolean equals(Object o) {
071: boolean wasInterrupted = beforeRead();
072: try {
073: return c_.equals(o);
074: } finally {
075: afterRead(wasInterrupted);
076: }
077: }
078:
079: public Object get(int index) {
080: boolean wasInterrupted = beforeRead();
081: try {
082: return baseList().get(index);
083: } finally {
084: afterRead(wasInterrupted);
085: }
086: }
087:
088: public int indexOf(Object o) {
089: boolean wasInterrupted = beforeRead();
090: try {
091: return baseList().indexOf(o);
092: } finally {
093: afterRead(wasInterrupted);
094: }
095: }
096:
097: public int lastIndexOf(Object o) {
098: boolean wasInterrupted = beforeRead();
099: try {
100: return baseList().lastIndexOf(o);
101: } finally {
102: afterRead(wasInterrupted);
103: }
104: }
105:
106: public List subList(int fromIndex, int toIndex) {
107: boolean wasInterrupted = beforeRead();
108: try {
109: return new SyncList(baseList().subList(fromIndex, toIndex),
110: rd_, wr_);
111: } finally {
112: afterRead(wasInterrupted);
113: }
114: }
115:
116: public Object set(int index, Object o) {
117: try {
118: wr_.acquire();
119: try {
120: return baseList().set(index, o);
121: } finally {
122: wr_.release();
123: }
124: } catch (InterruptedException ex) {
125: Thread.currentThread().interrupt();
126: throw new UnsupportedOperationException();
127: }
128: }
129:
130: public Object remove(int index) {
131: try {
132: wr_.acquire();
133: try {
134: return baseList().remove(index);
135: } finally {
136: wr_.release();
137: }
138: } catch (InterruptedException ex) {
139: Thread.currentThread().interrupt();
140: throw new UnsupportedOperationException();
141: }
142: }
143:
144: public void add(int index, Object o) {
145: try {
146: wr_.acquire();
147: try {
148: baseList().add(index, o);
149: } finally {
150: wr_.release();
151: }
152: } catch (InterruptedException ex) {
153: Thread.currentThread().interrupt();
154: throw new UnsupportedOperationException();
155: }
156: }
157:
158: public boolean addAll(int index, Collection coll) {
159: try {
160: wr_.acquire();
161: try {
162: return baseList().addAll(index, coll);
163: } finally {
164: wr_.release();
165: }
166: } catch (InterruptedException ex) {
167: Thread.currentThread().interrupt();
168: throw new UnsupportedOperationException();
169: }
170: }
171:
172: public ListIterator unprotectedListIterator() {
173: boolean wasInterrupted = beforeRead();
174: try {
175: return baseList().listIterator();
176: } finally {
177: afterRead(wasInterrupted);
178: }
179: }
180:
181: public ListIterator listIterator() {
182: boolean wasInterrupted = beforeRead();
183: try {
184: return new SyncCollectionListIterator(baseList()
185: .listIterator());
186: } finally {
187: afterRead(wasInterrupted);
188: }
189: }
190:
191: public ListIterator unprotectedListIterator(int index) {
192: boolean wasInterrupted = beforeRead();
193: try {
194: return baseList().listIterator(index);
195: } finally {
196: afterRead(wasInterrupted);
197: }
198: }
199:
200: public ListIterator listIterator(int index) {
201: boolean wasInterrupted = beforeRead();
202: try {
203: return new SyncCollectionListIterator(baseList()
204: .listIterator(index));
205: } finally {
206: afterRead(wasInterrupted);
207: }
208: }
209:
210: public class SyncCollectionListIterator extends
211: SyncCollectionIterator implements ListIterator {
212:
213: SyncCollectionListIterator(Iterator baseIterator) {
214: super (baseIterator);
215: }
216:
217: protected ListIterator baseListIterator() {
218: return (ListIterator) (baseIterator_);
219: }
220:
221: public boolean hasPrevious() {
222: boolean wasInterrupted = beforeRead();
223: try {
224: return baseListIterator().hasPrevious();
225: } finally {
226: afterRead(wasInterrupted);
227: }
228: }
229:
230: public Object previous() {
231: boolean wasInterrupted = beforeRead();
232: try {
233: return baseListIterator().previous();
234: } finally {
235: afterRead(wasInterrupted);
236: }
237: }
238:
239: public int nextIndex() {
240: boolean wasInterrupted = beforeRead();
241: try {
242: return baseListIterator().nextIndex();
243: } finally {
244: afterRead(wasInterrupted);
245: }
246: }
247:
248: public int previousIndex() {
249: boolean wasInterrupted = beforeRead();
250: try {
251: return baseListIterator().previousIndex();
252: } finally {
253: afterRead(wasInterrupted);
254: }
255: }
256:
257: public void set(Object o) {
258: try {
259: wr_.acquire();
260: try {
261: baseListIterator().set(o);
262: } finally {
263: wr_.release();
264: }
265: } catch (InterruptedException ex) {
266: Thread.currentThread().interrupt();
267: throw new UnsupportedOperationException();
268: }
269: }
270:
271: public void add(Object o) {
272: try {
273: wr_.acquire();
274: try {
275: baseListIterator().add(o);
276: } finally {
277: wr_.release();
278: }
279: } catch (InterruptedException ex) {
280: Thread.currentThread().interrupt();
281: throw new UnsupportedOperationException();
282: }
283: }
284:
285: }
286:
287: }
|