KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > SynchronizedSortedSet


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.set;
17
18 import java.util.Comparator JavaDoc;
19 import java.util.SortedSet JavaDoc;
20
21 import org.apache.commons.collections.collection.SynchronizedCollection;
22
23 /**
24  * Decorates another <code>SortedSet</code> to synchronize its behaviour
25  * for a multi-threaded environment.
26  * <p>
27  * Methods are synchronized, then forwarded to the decorated set.
28  * <p>
29  * This class is Serializable from Commons Collections 3.1.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
33  *
34  * @author Stephen Colebourne
35  */

36 public class SynchronizedSortedSet extends SynchronizedCollection implements SortedSet JavaDoc {
37
38     /** Serialization version */
39     private static final long serialVersionUID = 2775582861954500111L;
40
41     /**
42      * Factory method to create a synchronized set.
43      *
44      * @param set the set to decorate, must not be null
45      * @throws IllegalArgumentException if set is null
46      */

47     public static SortedSet JavaDoc decorate(SortedSet JavaDoc set) {
48         return new SynchronizedSortedSet(set);
49     }
50     
51     //-----------------------------------------------------------------------
52
/**
53      * Constructor that wraps (not copies).
54      *
55      * @param set the set to decorate, must not be null
56      * @throws IllegalArgumentException if set is null
57      */

58     protected SynchronizedSortedSet(SortedSet JavaDoc set) {
59         super(set);
60     }
61
62     /**
63      * Constructor that wraps (not copies).
64      *
65      * @param set the set to decorate, must not be null
66      * @param lock the lock object to use, must not be null
67      * @throws IllegalArgumentException if set is null
68      */

69     protected SynchronizedSortedSet(SortedSet JavaDoc set, Object JavaDoc lock) {
70         super(set, lock);
71     }
72
73     /**
74      * Gets the decorated set.
75      *
76      * @return the decorated set
77      */

78     protected SortedSet JavaDoc getSortedSet() {
79         return (SortedSet JavaDoc) collection;
80     }
81
82     //-----------------------------------------------------------------------
83
public SortedSet JavaDoc subSet(Object JavaDoc fromElement, Object JavaDoc toElement) {
84         synchronized (lock) {
85             SortedSet JavaDoc set = getSortedSet().subSet(fromElement, toElement);
86             // the lock is passed into the constructor here to ensure that the
87
// subset is synchronized on the same lock as the parent
88
return new SynchronizedSortedSet(set, lock);
89         }
90     }
91
92     public SortedSet JavaDoc headSet(Object JavaDoc toElement) {
93         synchronized (lock) {
94             SortedSet JavaDoc set = getSortedSet().headSet(toElement);
95             // the lock is passed into the constructor here to ensure that the
96
// headset is synchronized on the same lock as the parent
97
return new SynchronizedSortedSet(set, lock);
98         }
99     }
100
101     public SortedSet JavaDoc tailSet(Object JavaDoc fromElement) {
102         synchronized (lock) {
103             SortedSet JavaDoc set = getSortedSet().tailSet(fromElement);
104             // the lock is passed into the constructor here to ensure that the
105
// tailset is synchronized on the same lock as the parent
106
return new SynchronizedSortedSet(set, lock);
107         }
108     }
109
110     public Object JavaDoc first() {
111         synchronized (lock) {
112             return getSortedSet().first();
113         }
114     }
115
116     public Object JavaDoc last() {
117         synchronized (lock) {
118             return getSortedSet().last();
119         }
120     }
121
122     public Comparator JavaDoc comparator() {
123         synchronized (lock) {
124             return getSortedSet().comparator();
125         }
126     }
127
128 }
129
Popular Tags