View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.set;
18  
19  import java.util.Comparator;
20  import java.util.SortedSet;
21  
22  import org.apache.commons.collections.collection.SynchronizedCollection;
23  
24  /**
25   * Decorates another <code>SortedSet</code> to synchronize its behaviour
26   * for a multi-threaded environment.
27   * <p>
28   * Methods are synchronized, then forwarded to the decorated set.
29   * <p>
30   * This class is Serializable from Commons Collections 3.1.
31   *
32   * @since 3.0
33   * @version $Id: SynchronizedSortedSet.java 1429905 2013-01-07 17:15:14Z ggregory $
34   */
35  public class SynchronizedSortedSet<E> extends SynchronizedCollection<E> implements SortedSet<E> {
36  
37      /** Serialization version */
38      private static final long serialVersionUID = 2775582861954500111L;
39  
40      /**
41       * Factory method to create a synchronized set.
42       *
43       * @param <E> the element type
44       * @param set  the set to decorate, must not be null
45       * @return a new synchronized sorted set
46       * @throws IllegalArgumentException if set is null
47       */
48      public static <E> SynchronizedSortedSet<E> synchronizedSortedSet(final SortedSet<E> set) {
49          return new SynchronizedSortedSet<E>(set);
50      }
51  
52      //-----------------------------------------------------------------------
53      /**
54       * Constructor that wraps (not copies).
55       *
56       * @param set  the set to decorate, must not be null
57       * @throws IllegalArgumentException if set is null
58       */
59      protected SynchronizedSortedSet(final SortedSet<E> set) {
60          super(set);
61      }
62  
63      /**
64       * Constructor that wraps (not copies).
65       *
66       * @param set  the set to decorate, must not be null
67       * @param lock  the lock object to use, must not be null
68       * @throws IllegalArgumentException if set is null
69       */
70      protected SynchronizedSortedSet(final SortedSet<E> set, final Object lock) {
71          super(set, lock);
72      }
73  
74      /**
75       * Gets the decorated set.
76       *
77       * @return the decorated set
78       */
79      protected SortedSet<E> getSortedSet() {
80          return (SortedSet<E>) collection;
81      }
82  
83      //-----------------------------------------------------------------------
84      public SortedSet<E> subSet(final E fromElement, final E toElement) {
85          synchronized (lock) {
86              final SortedSet<E> set = getSortedSet().subSet(fromElement, toElement);
87              // the lock is passed into the constructor here to ensure that the
88              // subset is synchronized on the same lock as the parent
89              return new SynchronizedSortedSet<E>(set, lock);
90          }
91      }
92  
93      public SortedSet<E> headSet(final E toElement) {
94          synchronized (lock) {
95              final SortedSet<E> set = getSortedSet().headSet(toElement);
96              // the lock is passed into the constructor here to ensure that the
97              // headset is synchronized on the same lock as the parent
98              return new SynchronizedSortedSet<E>(set, lock);
99          }
100     }
101 
102     public SortedSet<E> tailSet(final E fromElement) {
103         synchronized (lock) {
104             final SortedSet<E> set = getSortedSet().tailSet(fromElement);
105             // the lock is passed into the constructor here to ensure that the
106             // tailset is synchronized on the same lock as the parent
107             return new SynchronizedSortedSet<E>(set, lock);
108         }
109     }
110 
111     public E first() {
112         synchronized (lock) {
113             return getSortedSet().first();
114         }
115     }
116 
117     public E last() {
118         synchronized (lock) {
119             return getSortedSet().last();
120         }
121     }
122 
123     public Comparator<? super E> comparator() {
124         synchronized (lock) {
125             return getSortedSet().comparator();
126         }
127     }
128 
129 }