001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.set;
017:
018: import java.util.Comparator;
019: import java.util.SortedSet;
020:
021: import org.apache.commons.collections.collection.SynchronizedCollection;
022:
023: /**
024: * Decorates another <code>SortedSet</code> to synchronize its behaviour
025: * for a multi-threaded environment.
026: * <p>
027: * Methods are synchronized, then forwarded to the decorated set.
028: * <p>
029: * This class is Serializable from Commons Collections 3.1.
030: *
031: * @since Commons Collections 3.0
032: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
033: *
034: * @author Stephen Colebourne
035: */
036: public class SynchronizedSortedSet extends SynchronizedCollection
037: implements SortedSet {
038:
039: /** Serialization version */
040: private static final long serialVersionUID = 2775582861954500111L;
041:
042: /**
043: * Factory method to create a synchronized set.
044: *
045: * @param set the set to decorate, must not be null
046: * @throws IllegalArgumentException if set is null
047: */
048: public static SortedSet decorate(SortedSet set) {
049: return new SynchronizedSortedSet(set);
050: }
051:
052: //-----------------------------------------------------------------------
053: /**
054: * Constructor that wraps (not copies).
055: *
056: * @param set the set to decorate, must not be null
057: * @throws IllegalArgumentException if set is null
058: */
059: protected SynchronizedSortedSet(SortedSet set) {
060: super (set);
061: }
062:
063: /**
064: * Constructor that wraps (not copies).
065: *
066: * @param set the set to decorate, must not be null
067: * @param lock the lock object to use, must not be null
068: * @throws IllegalArgumentException if set is null
069: */
070: protected SynchronizedSortedSet(SortedSet set, Object lock) {
071: super (set, lock);
072: }
073:
074: /**
075: * Gets the decorated set.
076: *
077: * @return the decorated set
078: */
079: protected SortedSet getSortedSet() {
080: return (SortedSet) collection;
081: }
082:
083: //-----------------------------------------------------------------------
084: public SortedSet subSet(Object fromElement, Object toElement) {
085: synchronized (lock) {
086: SortedSet set = getSortedSet().subSet(fromElement,
087: toElement);
088: // the lock is passed into the constructor here to ensure that the
089: // subset is synchronized on the same lock as the parent
090: return new SynchronizedSortedSet(set, lock);
091: }
092: }
093:
094: public SortedSet headSet(Object toElement) {
095: synchronized (lock) {
096: SortedSet set = getSortedSet().headSet(toElement);
097: // the lock is passed into the constructor here to ensure that the
098: // headset is synchronized on the same lock as the parent
099: return new SynchronizedSortedSet(set, lock);
100: }
101: }
102:
103: public SortedSet tailSet(Object fromElement) {
104: synchronized (lock) {
105: SortedSet set = getSortedSet().tailSet(fromElement);
106: // the lock is passed into the constructor here to ensure that the
107: // tailset is synchronized on the same lock as the parent
108: return new SynchronizedSortedSet(set, lock);
109: }
110: }
111:
112: public Object first() {
113: synchronized (lock) {
114: return getSortedSet().first();
115: }
116: }
117:
118: public Object last() {
119: synchronized (lock) {
120: return getSortedSet().last();
121: }
122: }
123:
124: public Comparator comparator() {
125: synchronized (lock) {
126: return getSortedSet().comparator();
127: }
128: }
129:
130: }
|