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
41
42 static class SynchronizedSet<T> extends SynchronizedCollection<T> implements Set<T> {
43
44 private static final long serialVersionUID = 20150629L;
45
46
47
48
49
50
51 SynchronizedSet(final Set<T> set, final Object lock) {
52 super(set, lock);
53 }
54 }
55
56
57 private static final long serialVersionUID = 20150629L;
58
59
60
61
62
63
64
65
66
67 public static <E> SynchronizedMultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
68 return new SynchronizedMultiSet<>(multiset);
69 }
70
71
72
73
74
75
76
77 protected SynchronizedMultiSet(final MultiSet<E> multiset) {
78 super(multiset);
79 }
80
81
82
83
84
85
86
87
88 protected SynchronizedMultiSet(final MultiSet<E> multiset, final Object lock) {
89 super(multiset, lock);
90 }
91
92 @Override
93 public int add(final E object, final int count) {
94 synchronized (lock) {
95 return decorated().add(object, count);
96 }
97 }
98
99
100
101
102
103
104 @Override
105 protected MultiSet<E> decorated() {
106 return (MultiSet<E>) super.decorated();
107 }
108
109 @Override
110 public Set<Entry<E>> entrySet() {
111 synchronized (lock) {
112 final Set<MultiSet.Entry<E>> set = decorated().entrySet();
113 return new SynchronizedSet<>(set, lock);
114 }
115 }
116
117 @Override
118 public boolean equals(final Object object) {
119 if (object == this) {
120 return true;
121 }
122 synchronized (lock) {
123 return decorated().equals(object);
124 }
125 }
126
127 @Override
128 public int getCount(final Object object) {
129 synchronized (lock) {
130 return decorated().getCount(object);
131 }
132 }
133
134 @Override
135 public int hashCode() {
136 synchronized (lock) {
137 return decorated().hashCode();
138 }
139 }
140
141 @Override
142 public int remove(final Object object, final int count) {
143 synchronized (lock) {
144 return decorated().remove(object, count);
145 }
146 }
147
148 @Override
149 public int setCount(final E object, final int count) {
150 synchronized (lock) {
151 return decorated().setCount(object, count);
152 }
153 }
154
155 @Override
156 public Set<E> uniqueSet() {
157 synchronized (lock) {
158 final Set<E> set = decorated().uniqueSet();
159 return new SynchronizedSet<>(set, lock);
160 }
161 }
162
163 }