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