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