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