001: /*
002: File: SyncSortedSet.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: * SyncSortedSets wrap Sync-based control around java.util.SortedSets.
020: * They support the following additional reader operations over
021: * SyncCollection: comparator, subSet, headSet, tailSet, first, last.
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 SyncSortedSet extends SyncSet implements SortedSet {
027:
028: /**
029: * Create a new SyncSortedSet protecting the given collection,
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 SyncSortedSet(SortedSet set, Sync sync) {
035: super (set, sync);
036: }
037:
038: /**
039: * Create a new SyncSortedSet protecting the given set,
040: * and using the given ReadWriteLock to control reader and writer methods.
041: **/
042: public SyncSortedSet(SortedSet set, ReadWriteLock rwl) {
043: super (set, rwl.readLock(), rwl.writeLock());
044: }
045:
046: /**
047: * Create a new SyncSortedSet protecting the given set,
048: * and using the given pair of locks to control reader and writer methods.
049: **/
050: public SyncSortedSet(SortedSet set, Sync readLock, Sync writeLock) {
051: super (set, readLock, writeLock);
052: }
053:
054: protected SortedSet baseSortedSet() {
055: return (SortedSet) c_;
056: }
057:
058: public Comparator comparator() {
059: boolean wasInterrupted = beforeRead();
060: try {
061: return baseSortedSet().comparator();
062: } finally {
063: afterRead(wasInterrupted);
064: }
065: }
066:
067: public Object first() {
068: boolean wasInterrupted = beforeRead();
069: try {
070: return baseSortedSet().first();
071: } finally {
072: afterRead(wasInterrupted);
073: }
074: }
075:
076: public Object last() {
077: boolean wasInterrupted = beforeRead();
078: try {
079: return baseSortedSet().last();
080: } finally {
081: afterRead(wasInterrupted);
082: }
083: }
084:
085: public SortedSet subSet(Object fromElement, Object toElement) {
086: boolean wasInterrupted = beforeRead();
087: try {
088: return new SyncSortedSet(baseSortedSet().subSet(
089: fromElement, toElement), rd_, wr_);
090: } finally {
091: afterRead(wasInterrupted);
092: }
093: }
094:
095: public SortedSet headSet(Object toElement) {
096: boolean wasInterrupted = beforeRead();
097: try {
098: return new SyncSortedSet(
099: baseSortedSet().headSet(toElement), rd_, wr_);
100: } finally {
101: afterRead(wasInterrupted);
102: }
103: }
104:
105: public SortedSet tailSet(Object fromElement) {
106: boolean wasInterrupted = beforeRead();
107: try {
108: return new SyncSortedSet(baseSortedSet().tailSet(
109: fromElement), rd_, wr_);
110: } finally {
111: afterRead(wasInterrupted);
112: }
113: }
114:
115: }
|