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.bag;
18  
19  import java.util.Comparator;
20  
21  import org.apache.commons.collections.Bag;
22  import org.apache.commons.collections.SortedBag;
23  
24  /**
25   * Decorates another {@link SortedBag} to synchronize its behaviour
26   * for a multi-threaded environment.
27   * <p>
28   * Methods are synchronized, then forwarded to the decorated bag.
29   * Iterators must be separately synchronized around the loop.
30   * <p>
31   * This class is Serializable from Commons Collections 3.1.
32   *
33   * @since 3.0
34   * @version $Id: SynchronizedSortedBag.java 1429905 2013-01-07 17:15:14Z ggregory $
35   */
36  public class SynchronizedSortedBag<E> extends SynchronizedBag<E> implements SortedBag<E> {
37  
38      /** Serialization version */
39      private static final long serialVersionUID = 722374056718497858L;
40  
41      /**
42       * Factory method to create a synchronized sorted bag.
43       * 
44       * @param <E> the type of the elements in the bag
45       * @param bag  the bag to decorate, must not be null
46       * @return a new synchronized SortedBag
47       * @throws IllegalArgumentException if bag is null
48       */
49      public static <E> SynchronizedSortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) {
50          return new SynchronizedSortedBag<E>(bag);
51      }
52      
53      //-----------------------------------------------------------------------
54      /**
55       * Constructor that wraps (not copies).
56       * 
57       * @param bag  the bag to decorate, must not be null
58       * @throws IllegalArgumentException if bag is null
59       */
60      protected SynchronizedSortedBag(final SortedBag<E> bag) {
61          super(bag);
62      }
63  
64      /**
65       * Constructor that wraps (not copies).
66       * 
67       * @param bag  the bag to decorate, must not be null
68       * @param lock  the lock to use, must not be null
69       * @throws IllegalArgumentException if bag is null
70       */
71      protected SynchronizedSortedBag(final Bag<E> bag, final Object lock) {
72          super(bag, lock);
73      }
74  
75      /**
76       * Gets the bag being decorated.
77       * 
78       * @return the decorated bag
79       */
80      protected SortedBag<E> getSortedBag() {
81          return (SortedBag<E>) collection;
82      }
83      
84      //-----------------------------------------------------------------------
85      
86      public synchronized E first() {
87          synchronized (lock) {
88              return getSortedBag().first();
89          }
90      }
91  
92      public synchronized E last() {
93          synchronized (lock) {
94              return getSortedBag().last();
95          }
96      }
97  
98      public synchronized Comparator<? super E> comparator() {
99          synchronized (lock) {
100             return getSortedBag().comparator();
101         }
102     }
103 
104 }