author | Claus Gittinger <cg@exept.de> |
Mon, 22 Nov 2010 21:09:26 +0100 | |
changeset 2515 | 104080163067 |
parent 1929 | 5271aca614c2 |
child 2927 | b7b7931cb32d |
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 |
||
1632 | 87 |
!SharedCollection methodsFor:'initialization'! |
88 |
||
89 |
initializeFor:aCollection |
|
90 |
accessLock := RecursionLock new. |
|
91 |
realCollection := aCollection. |
|
92 |
! ! |
|
93 |
||
94 |
!SharedCollection methodsFor:'message forwarding'! |
|
95 |
||
1929 | 96 |
add:anElement |
97 |
|rslt| |
|
98 |
||
99 |
accessLock critical:[ |
|
100 |
rslt := realCollection add:anElement |
|
101 |
]. |
|
102 |
^ rslt |
|
103 |
! |
|
104 |
||
1632 | 105 |
at:index |
106 |
|rslt| |
|
107 |
||
108 |
accessLock critical:[ |
|
109 |
rslt := realCollection at:index |
|
110 |
]. |
|
111 |
^ rslt |
|
112 |
! |
|
113 |
||
114 |
at:index put:value |
|
115 |
|rslt| |
|
116 |
||
117 |
accessLock critical:[ |
|
118 |
rslt := realCollection at:index put:value |
|
119 |
]. |
|
120 |
^ rslt |
|
121 |
! |
|
122 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
123 |
do:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
124 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
125 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
126 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
127 |
rslt := realCollection do:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
128 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
129 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
130 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
131 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
132 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
133 |
|
1632 | 134 |
doesNotUnderstand:aMessage |
135 |
"catches everything not understood by the collection protocol" |
|
136 |
||
137 |
|msg rslt| |
|
138 |
||
1794 | 139 |
"/ msg := thisContext sender message. |
1632 | 140 |
accessLock critical:[ |
141 |
rslt := aMessage sendTo:realCollection |
|
142 |
]. |
|
143 |
^ rslt |
|
1794 | 144 |
|
145 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
1632 | 146 |
! |
147 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
148 |
isEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
149 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
150 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
151 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
152 |
rslt := realCollection isEmpty |
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 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
155 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
156 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
157 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
158 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
159 |
notEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
160 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
161 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
162 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
163 |
rslt := realCollection notEmpty |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
164 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
165 |
^ rslt |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
166 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
167 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
170 |
removeAllSuchThat:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
171 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
172 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
173 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
174 |
rslt := realCollection removeAllSuchThat:aBlock |
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 |
^ rslt |
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 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
181 |
removeIdentical:someElement ifAbsent:aBlock |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
182 |
|rslt| |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
183 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
184 |
accessLock critical:[ |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
185 |
rslt := realCollection removeIdentical:someElement ifAbsent:aBlock |
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 |
^ rslt |
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 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
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 |
|
1635 | 192 |
size |
193 |
|rslt| |
|
194 |
||
195 |
accessLock critical:[ |
|
196 |
rslt := realCollection size |
|
197 |
]. |
|
198 |
^ rslt |
|
199 |
! |
|
200 |
||
1632 | 201 |
subclassResponsibility |
202 |
"catches every required message of the collection protocol" |
|
203 |
||
204 |
|msg rslt| |
|
205 |
||
206 |
msg := thisContext sender message. |
|
207 |
accessLock critical:[ |
|
208 |
rslt := msg sendTo:realCollection |
|
209 |
]. |
|
210 |
^ rslt |
|
211 |
! ! |
|
212 |
||
1637 | 213 |
!SharedCollection methodsFor:'special'! |
214 |
||
215 |
species |
|
216 |
"returns non shared collections" |
|
217 |
||
218 |
^ realCollection species |
|
219 |
! ! |
|
220 |
||
1632 | 221 |
!SharedCollection class methodsFor:'documentation'! |
222 |
||
223 |
version |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
224 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedCollection.st,v 1.8 2010-11-22 20:09:26 cg Exp $' |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
225 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
226 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
227 |
version_CVS |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
228 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedCollection.st,v 1.8 2010-11-22 20:09:26 cg Exp $' |
1632 | 229 |
! ! |