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