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