author | Stefan Vogel <sv@exept.de> |
Thu, 21 Jul 2016 17:05:54 +0200 | |
changeset 4001 | 70db839403e2 |
parent 3860 | 224a3ad98604 |
child 4148 | 0cdfbd3b63c0 |
permissions | -rw-r--r-- |
4001 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1632 | 3 |
" |
4 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libbasic2' }" |
|
15 |
||
3860 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1632 | 18 |
Collection subclass:#SharedCollection |
19 |
instanceVariableNames:'accessLock realCollection' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
22 |
category:'Collections-Support' |
1632 | 23 |
! |
24 |
||
25 |
!SharedCollection class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
Instances of this class provide synchronized access (of multiple processes) |
|
44 |
to a collection. |
|
1636 | 45 |
|
4001 | 46 |
Notice: the message-forwarding is done by catching subclassResponsibility and |
1636 | 47 |
doesNotUnderstand errors. |
48 |
For performance, and for more complex operation-atomicy, more messages might need |
|
49 |
an explicit handling. See the implementation of #at: / #at:put: and #size for examples. |
|
1632 | 50 |
" |
51 |
! |
|
52 |
||
53 |
examples |
|
54 |
" |
|
55 |
[exBegin] |
|
56 |
|c| |
|
57 |
||
58 |
c := SharedCollection for:(OrderedCollection new). |
|
59 |
c add:1. |
|
60 |
c add:2. |
|
61 |
c add:3. |
|
62 |
c addAll:#(4 5 6). |
|
63 |
c removeFirst. |
|
64 |
c removeLast. |
|
65 |
c inspect. |
|
66 |
[exEnd] |
|
67 |
||
68 |
[exBegin] |
|
69 |
|c| |
|
70 |
||
71 |
c := SharedCollection for:(Array new:10). |
|
72 |
c at:1 put:5. |
|
73 |
c replaceFrom:2 to:5 with:#(20 30 40 50). |
|
74 |
c inspect. |
|
75 |
[exEnd] |
|
76 |
" |
|
77 |
! ! |
|
78 |
||
79 |
!SharedCollection class methodsFor:'instance creation'! |
|
80 |
||
81 |
for:aCollection |
|
82 |
^ self new initializeFor:aCollection |
|
83 |
! ! |
|
84 |
||
1634 | 85 |
!SharedCollection methodsFor:'converting'! |
86 |
||
87 |
asSharedCollection |
|
88 |
^ self. |
|
89 |
! ! |
|
90 |
||
3212 | 91 |
!SharedCollection methodsFor:'copying'! |
92 |
||
93 |
shallowCopy |
|
94 |
"analog to species - copy the real collection" |
|
95 |
||
3213 | 96 |
|rslt| |
97 |
||
98 |
accessLock critical:[ |
|
99 |
"get a consistent copy" |
|
100 |
rslt := realCollection shallowCopy |
|
101 |
]. |
|
102 |
^ rslt. |
|
3212 | 103 |
! ! |
104 |
||
1632 | 105 |
!SharedCollection methodsFor:'initialization'! |
106 |
||
107 |
initializeFor:aCollection |
|
108 |
accessLock := RecursionLock new. |
|
109 |
realCollection := aCollection. |
|
110 |
! ! |
|
111 |
||
112 |
!SharedCollection methodsFor:'message forwarding'! |
|
113 |
||
1929 | 114 |
add:anElement |
2927 | 115 |
"add the argument, anObject to the receiver. |
116 |
Return the added element." |
|
117 |
||
1929 | 118 |
|rslt| |
119 |
||
120 |
accessLock critical:[ |
|
121 |
rslt := realCollection add:anElement |
|
122 |
]. |
|
123 |
^ rslt |
|
124 |
! |
|
125 |
||
1632 | 126 |
at:index |
127 |
|rslt| |
|
128 |
||
129 |
accessLock critical:[ |
|
130 |
rslt := realCollection at:index |
|
131 |
]. |
|
132 |
^ rslt |
|
133 |
! |
|
134 |
||
135 |
at:index put:value |
|
136 |
|rslt| |
|
137 |
||
138 |
accessLock critical:[ |
|
139 |
rslt := realCollection at:index put:value |
|
140 |
]. |
|
141 |
^ rslt |
|
142 |
! |
|
143 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
144 |
do:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
145 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
146 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
147 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
148 |
rslt := realCollection do:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
149 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
150 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
151 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
152 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
153 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
154 |
|
1632 | 155 |
doesNotUnderstand:aMessage |
156 |
"catches everything not understood by the collection protocol" |
|
157 |
||
3022 | 158 |
|rslt| |
1632 | 159 |
|
160 |
accessLock critical:[ |
|
161 |
rslt := aMessage sendTo:realCollection |
|
162 |
]. |
|
163 |
^ rslt |
|
1794 | 164 |
|
165 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
1632 | 166 |
! |
167 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
168 |
isEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
169 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
170 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
171 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
172 |
rslt := realCollection isEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
173 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
174 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
175 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
176 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
177 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
178 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
179 |
notEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
180 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
181 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
182 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
183 |
rslt := realCollection notEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
184 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
185 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
186 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
187 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
188 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
189 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
190 |
removeAllSuchThat:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
191 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
192 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
193 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
194 |
rslt := realCollection removeAllSuchThat:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
195 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
196 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
197 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
198 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
199 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
200 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
201 |
removeIdentical:someElement ifAbsent:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
202 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
203 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
204 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
205 |
rslt := realCollection removeIdentical:someElement ifAbsent:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
206 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
207 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
208 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
209 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
210 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
211 |
|
1635 | 212 |
size |
213 |
|rslt| |
|
214 |
||
215 |
accessLock critical:[ |
|
216 |
rslt := realCollection size |
|
217 |
]. |
|
218 |
^ rslt |
|
219 |
! |
|
220 |
||
1632 | 221 |
subclassResponsibility |
222 |
"catches every required message of the collection protocol" |
|
223 |
||
224 |
|msg rslt| |
|
225 |
||
226 |
msg := thisContext sender message. |
|
227 |
accessLock critical:[ |
|
228 |
rslt := msg sendTo:realCollection |
|
229 |
]. |
|
230 |
^ rslt |
|
231 |
! ! |
|
232 |
||
3212 | 233 |
!SharedCollection methodsFor:'queries'! |
1637 | 234 |
|
235 |
species |
|
3860 | 236 |
"returns non shared collection's species" |
1637 | 237 |
|
238 |
^ realCollection species |
|
239 |
! ! |
|
240 |
||
3013 | 241 |
!SharedCollection methodsFor:'testing'! |
242 |
||
243 |
isFixedSize |
|
244 |
"return true if the receiver cannot grow" |
|
245 |
||
246 |
^ realCollection isFixedSize |
|
247 |
! ! |
|
248 |
||
1632 | 249 |
!SharedCollection class methodsFor:'documentation'! |
250 |
||
251 |
version |
|
3860 | 252 |
^ '$Header$' |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
253 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
254 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
255 |
version_CVS |
3860 | 256 |
^ '$Header$' |
1632 | 257 |
! ! |
2927 | 258 |