1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.multiset;
18
19 import java.util.Set;
20
21 import org.apache.commons.collections4.MultiSet;
22 import org.apache.commons.collections4.collection.SynchronizedCollection;
23
24
25
26
27
28
29
30
31
32
33
34
35 public class SynchronizedMultiSet<E> extends SynchronizedCollection<E> implements MultiSet<E> {
36
37
38
39
40 static class SynchronizedSet<T> extends SynchronizedCollection<T> implements Set<T> {
41
42 private static final long serialVersionUID = 20150629L;
43
44
45
46
47
48
49 SynchronizedSet(final Set<T> set, final Object lock) {
50 super(set, lock);
51 }
52 }
53
54
55 private static final long serialVersionUID = 20150629L;
56
57
58
59
60
61
62
63
64
65 public static <E> SynchronizedMultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
66 return new SynchronizedMultiSet<>(multiset);
67 }
68
69
70
71
72
73
74
75 protected SynchronizedMultiSet(final MultiSet<E> multiset) {
76 super(multiset);
77 }
78
79
80
81
82
83
84
85
86 protected SynchronizedMultiSet(final MultiSet<E> multiset, final Object lock) {
87 super(multiset, lock);
88 }
89
90 @Override
91 public int add(final E object, final int count) {
92 synchronized (lock) {
93 return decorated().add(object, count);
94 }
95 }
96
97
98
99
100
101
102 @Override
103 protected MultiSet<E> decorated() {
104 return (MultiSet<E>) super.decorated();
105 }
106
107 @Override
108 public Set<Entry<E>> entrySet() {
109 synchronized (lock) {
110 final Set<MultiSet.Entry<E>> set = decorated().entrySet();
111 return new SynchronizedSet<>(set, lock);
112 }
113 }
114
115 @Override
116 public boolean equals(final Object object) {
117 if (object == this) {
118 return true;
119 }
120 synchronized (lock) {
121 return decorated().equals(object);
122 }
123 }
124
125 @Override
126 public int getCount(final Object object) {
127 synchronized (lock) {
128 return decorated().getCount(object);
129 }
130 }
131
132 @Override
133 public int hashCode() {
134 synchronized (lock) {
135 return decorated().hashCode();
136 }
137 }
138
139 @Override
140 public int remove(final Object object, final int count) {
141 synchronized (lock) {
142 return decorated().remove(object, count);
143 }
144 }
145
146 @Override
147 public int setCount(final E object, final int count) {
148 synchronized (lock) {
149 return decorated().setCount(object, count);
150 }
151 }
152
153 @Override
154 public Set<E> uniqueSet() {
155 synchronized (lock) {
156 final Set<E> set = decorated().uniqueSet();
157 return new SynchronizedSet<>(set, lock);
158 }
159 }
160
161 }