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