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


1 /*
2   File: SyncSortedMap.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  * SyncSortedMaps wrap Sync-based control around java.util.SortedMaps.
19  * They support the following additional reader operations over
20  * SyncMap: comparator, subMap, headMap, tailMap, firstKey, lastKey.
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 SyncSortedMap extends SyncMap implements SortedMap {
27
28   /**
29    * Create a new SyncSortedMap protecting the given map,
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 SyncSortedMap(SortedMap map, Sync sync) {
35     this (map, sync, sync);
36   }
37
38   /**
39    * Create a new SyncSortedMap protecting the given map,
40    * and using the given ReadWriteLock to control reader and writer methods.
41    **/

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

50   public SyncSortedMap(SortedMap map, Sync readLock, Sync writeLock) {
51     super(map, readLock, writeLock);
52   }
53
54
55   protected SortedMap baseSortedMap() {
56     return (SortedMap)c_;
57   }
58
59   public Comparator comparator() {
60     boolean wasInterrupted = beforeRead();
61     try {
62       return baseSortedMap().comparator();
63     }
64     finally {
65       afterRead(wasInterrupted);
66     }
67   }
68
69   public Object firstKey() {
70     boolean wasInterrupted = beforeRead();
71     try {
72       return baseSortedMap().firstKey();
73     }
74     finally {
75       afterRead(wasInterrupted);
76     }
77   }
78
79   public Object lastKey() {
80     boolean wasInterrupted = beforeRead();
81     try {
82       return baseSortedMap().lastKey();
83     }
84     finally {
85       afterRead(wasInterrupted);
86     }
87   }
88
89
90   public SortedMap subMap(Object fromElement, Object toElement) {
91     boolean wasInterrupted = beforeRead();
92     try {
93       return new SyncSortedMap(baseSortedMap().subMap(fromElement, toElement),
94                                rd_, wr_);
95     }
96     finally {
97       afterRead(wasInterrupted);
98     }
99   }
100
101   public SortedMap headMap(Object toElement) {
102     boolean wasInterrupted = beforeRead();
103     try {
104       return new SyncSortedMap(baseSortedMap().headMap(toElement),
105                                rd_, wr_);
106     }
107     finally {
108       afterRead(wasInterrupted);
109     }
110   }
111
112   public SortedMap tailMap(Object fromElement) {
113     boolean wasInterrupted = beforeRead();
114     try {
115       return new SyncSortedMap(baseSortedMap().tailMap(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