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.Set;
20  
21  import org.apache.commons.collections.collection.SynchronizedCollection;
22  
23  /**
24   * Decorates another <code>Set</code> to synchronize its behaviour for a
25   * 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 3.0
32   * @version $Id: SynchronizedSet.java 1429905 2013-01-07 17:15:14Z ggregory $
33   */
34  public class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> {
35  
36      /** Serialization version */
37      private static final long serialVersionUID = -8304417378626543635L;
38  
39      /**
40       * Factory method to create a synchronized set.
41       * 
42       * @param <E> the element type
43       * @param set  the set to decorate, must not be null
44       * @return a new synchronized set
45       * @throws IllegalArgumentException if set is null
46       */
47      public static <E> SynchronizedSet<E> synchronizedSet(final Set<E> set) {
48          return new SynchronizedSet<E>(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 SynchronizedSet(final Set<E> 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 SynchronizedSet(final Set<E> set, final Object lock) {
70          super(set, lock);
71      }
72  
73      /**
74       * Gets the decorated set.
75       * 
76       * @return the decorated set
77       */
78      protected Set<E> getSet() {
79          return (Set<E>) collection;
80      }
81  
82  }