Code - Class EDU.oswego.cs.dl.util.concurrent.SyncSortedSet


1 /*
2   File: SyncSortedSet.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11    1Aug1998 dl Create public version
12 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15 import java.util.*;
16
17 /**
18  * SyncSortedSets wrap Sync-based control around java.util.SortedSets.
19  * They support the following additional reader operations over
20  * SyncCollection: comparator, subSet, headSet, tailSet, first, last.
21  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
22  * @see SyncCollection
23 **/

24
25
26 public class SyncSortedSet extends SyncSet implements SortedSet {
27
28   /**
29    * Create a new SyncSortedSet protecting the given collection,
30    * and using the given sync to control both reader and writer methods.
31    * Common, reasonable choices for the sync argument include
32    * Mutex, ReentrantLock, and Semaphores initialized to 1.
33    **/

34   public SyncSortedSet(SortedSet set, Sync sync) {
35     super (set, sync);
36   }
37
38   /**
39    * Create a new SyncSortedSet protecting the given set,
40    * and using the given ReadWriteLock to control reader and writer methods.
41    **/

42   public SyncSortedSet(SortedSet set, ReadWriteLock rwl) {
43     super (set, rwl.readLock(), rwl.writeLock());
44   }
45
46   /**
47    * Create a new SyncSortedSet protecting the given set,
48    * and using the given pair of locks to control reader and writer methods.
49    **/

50   public SyncSortedSet(SortedSet set, Sync readLock, Sync writeLock) {
51     super(set, readLock, writeLock);
52   }
53
54
55   protected SortedSet baseSortedSet() {
56     return (SortedSet)c_;
57   }
58
59   public Comparator comparator() {
60     boolean wasInterrupted = beforeRead();
61     try {
62       return baseSortedSet().comparator();
63     }
64     finally {
65       afterRead(wasInterrupted);
66     }
67   }
68
69   public Object first() {
70     boolean wasInterrupted = beforeRead();
71     try {
72       return baseSortedSet().first();
73     }
74     finally {
75       afterRead(wasInterrupted);
76     }
77   }
78
79   public Object last() {
80     boolean wasInterrupted = beforeRead();
81     try {
82       return baseSortedSet().last();
83     }
84     finally {
85       afterRead(wasInterrupted);
86     }
87   }
88
89
90   public SortedSet subSet(Object fromElement, Object toElement) {
91     boolean wasInterrupted = beforeRead();
92     try {
93       return new SyncSortedSet(baseSortedSet().subSet(fromElement, toElement),
94                                rd_, wr_);
95     }
96     finally {
97       afterRead(wasInterrupted);
98     }
99   }
100
101   public SortedSet headSet(Object toElement) {
102     boolean wasInterrupted = beforeRead();
103     try {
104       return new SyncSortedSet(baseSortedSet().headSet(toElement),
105                                rd_, wr_);
106     }
107     finally {
108       afterRead(wasInterrupted);
109     }
110   }
111
112   public SortedSet tailSet(Object fromElement) {
113     boolean wasInterrupted = beforeRead();
114     try {
115       return new SyncSortedSet(baseSortedSet().tailSet(fromElement),
116                                rd_, wr_);
117     }
118     finally {
119       afterRead(wasInterrupted);
120     }
121   }
122
123 }
124
125
126

Java API By Example, From Geeks To Geeks. | Conditions of Use | About Us © 2002 - 2005, KickJava.com, or its affiliates