001: /*
002: File: SyncSortedMap.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: * SyncSortedMaps wrap Sync-based control around java.util.SortedMaps.
020: * They support the following additional reader operations over
021: * SyncMap: comparator, subMap, headMap, tailMap, firstKey, lastKey.
022: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
023: * @see SyncCollection
024: **/
025:
026: public class SyncSortedMap extends SyncMap implements SortedMap {
027:
028: /**
029: * Create a new SyncSortedMap protecting the given map,
030: * and using the given sync to control both reader and writer methods.
031: * Common, reasonable choices for the sync argument include
032: * Mutex, ReentrantLock, and Semaphores initialized to 1.
033: **/
034: public SyncSortedMap(SortedMap map, Sync sync) {
035: this (map, sync, sync);
036: }
037:
038: /**
039: * Create a new SyncSortedMap protecting the given map,
040: * and using the given ReadWriteLock to control reader and writer methods.
041: **/
042: public SyncSortedMap(SortedMap map, ReadWriteLock rwl) {
043: super (map, rwl.readLock(), rwl.writeLock());
044: }
045:
046: /**
047: * Create a new SyncSortedMap protecting the given map,
048: * and using the given pair of locks to control reader and writer methods.
049: **/
050: public SyncSortedMap(SortedMap map, Sync readLock, Sync writeLock) {
051: super (map, readLock, writeLock);
052: }
053:
054: protected SortedMap baseSortedMap() {
055: return (SortedMap) c_;
056: }
057:
058: public Comparator comparator() {
059: boolean wasInterrupted = beforeRead();
060: try {
061: return baseSortedMap().comparator();
062: } finally {
063: afterRead(wasInterrupted);
064: }
065: }
066:
067: public Object firstKey() {
068: boolean wasInterrupted = beforeRead();
069: try {
070: return baseSortedMap().firstKey();
071: } finally {
072: afterRead(wasInterrupted);
073: }
074: }
075:
076: public Object lastKey() {
077: boolean wasInterrupted = beforeRead();
078: try {
079: return baseSortedMap().lastKey();
080: } finally {
081: afterRead(wasInterrupted);
082: }
083: }
084:
085: public SortedMap subMap(Object fromElement, Object toElement) {
086: boolean wasInterrupted = beforeRead();
087: try {
088: return new SyncSortedMap(baseSortedMap().subMap(
089: fromElement, toElement), rd_, wr_);
090: } finally {
091: afterRead(wasInterrupted);
092: }
093: }
094:
095: public SortedMap headMap(Object toElement) {
096: boolean wasInterrupted = beforeRead();
097: try {
098: return new SyncSortedMap(
099: baseSortedMap().headMap(toElement), rd_, wr_);
100: } finally {
101: afterRead(wasInterrupted);
102: }
103: }
104:
105: public SortedMap tailMap(Object fromElement) {
106: boolean wasInterrupted = beforeRead();
107: try {
108: return new SyncSortedMap(baseSortedMap().tailMap(
109: fromElement), rd_, wr_);
110: } finally {
111: afterRead(wasInterrupted);
112: }
113: }
114:
115: }
|