3615
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1998 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 |
"
|
9482
|
12 |
"{ Package: 'stx:libbasic' }"
|
8893
|
13 |
|
3615
|
14 |
Collection subclass:#KeyedCollection
|
|
15 |
instanceVariableNames:''
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Collections-Abstract'
|
|
19 |
!
|
|
20 |
|
|
21 |
!KeyedCollection class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1998 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 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
Abstract superclass for collections which have a key->value mapping.
|
|
41 |
This abstract class provides functionality common to those collections,
|
|
42 |
without knowing how the concrete class implements things. Thus, all
|
|
43 |
methods found here depend on some basic mechanisms to be defined in the
|
|
44 |
concrete class.
|
|
45 |
These basic methods are usually defined as #subclassResponsibility here.
|
|
46 |
Some methods are also redefined for better performance.
|
|
47 |
|
|
48 |
Subclasses should at least implement:
|
|
49 |
at:ifAbsent: - accessing elements
|
|
50 |
removeKey:ifAbsent - removing
|
|
51 |
keysAndValuesDo: - enumerating
|
|
52 |
|
|
53 |
[author:]
|
|
54 |
Claus Gittinger
|
|
55 |
"
|
|
56 |
|
|
57 |
! !
|
|
58 |
|
8893
|
59 |
!KeyedCollection class methodsFor:'queries'!
|
|
60 |
|
|
61 |
isAbstract
|
11222
|
62 |
"Return if this class is an abstract class.
|
|
63 |
True is returned for KeyedCollection here; false for subclasses.
|
|
64 |
Abstract subclasses must redefine again."
|
|
65 |
|
8893
|
66 |
^ self == KeyedCollection
|
|
67 |
! !
|
|
68 |
|
3615
|
69 |
!KeyedCollection methodsFor:'accessing'!
|
|
70 |
|
|
71 |
at:key
|
|
72 |
"return the value stored under akey.
|
|
73 |
Raise an error if not found"
|
|
74 |
|
|
75 |
^ self at:key ifAbsent:[self errorKeyNotFound:key].
|
|
76 |
|
|
77 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
78 |
!
|
|
79 |
|
|
80 |
at:key ifAbsent:exceptionBlock
|
|
81 |
"return the value stored under akey.
|
|
82 |
Return the value from evaluating exceptionBlock if not found"
|
|
83 |
|
|
84 |
^ self subclassResponsibility
|
|
85 |
|
|
86 |
"Created: / 19.6.1998 / 00:48:23 / cg"
|
|
87 |
!
|
|
88 |
|
|
89 |
keyAtValue:value
|
|
90 |
"return the key under which value is stored.
|
|
91 |
Raise an error if not found"
|
|
92 |
|
|
93 |
^ self keyAtValue:value ifAbsent:[self errorValueNotFound:value].
|
|
94 |
|
|
95 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
96 |
"Created: / 19.6.1998 / 00:49:16 / cg"
|
|
97 |
!
|
|
98 |
|
|
99 |
keyAtValue:value ifAbsent:exceptionBlock
|
|
100 |
"return the key under which value is stored.
|
|
101 |
If not found, return the value from evaluating exceptionBlock"
|
|
102 |
|
|
103 |
self keysAndValuesDo:[:elKey :elValue |
|
|
104 |
value == elValue ifTrue:[^ elKey]
|
|
105 |
].
|
|
106 |
^ exceptionBlock value
|
|
107 |
|
|
108 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
109 |
"Created: / 19.6.1998 / 00:50:34 / cg"
|
|
110 |
!
|
|
111 |
|
|
112 |
keys
|
|
113 |
"return a collection containing the keys of the receiver"
|
|
114 |
|
|
115 |
|keyCollection|
|
|
116 |
|
|
117 |
keyCollection := OrderedCollection new.
|
|
118 |
self keysDo:[:aKey |
|
|
119 |
keyCollection add:aKey
|
|
120 |
].
|
|
121 |
^ keyCollection
|
|
122 |
|
|
123 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
124 |
"Created: / 19.6.1998 / 00:51:49 / cg"
|
|
125 |
!
|
|
126 |
|
|
127 |
values
|
|
128 |
"return a collection containing the values of the receiver"
|
|
129 |
|
|
130 |
|valueCollection|
|
|
131 |
|
|
132 |
valueCollection := OrderedCollection new.
|
|
133 |
self do:[:element |
|
|
134 |
valueCollection add:element
|
|
135 |
].
|
|
136 |
^ valueCollection
|
|
137 |
|
|
138 |
"Modified: / 19.6.1998 / 00:48:27 / cg"
|
|
139 |
"Created: / 19.6.1998 / 00:52:16 / cg"
|
|
140 |
! !
|
|
141 |
|
|
142 |
!KeyedCollection methodsFor:'enumerating'!
|
|
143 |
|
|
144 |
do:aBlock
|
|
145 |
"evaluate aBlock for each value"
|
|
146 |
|
|
147 |
self keysAndValuesDo:[:elKey :elValue | aBlock value:elValue]
|
|
148 |
|
|
149 |
"Created: / 19.6.1998 / 00:56:24 / cg"
|
|
150 |
!
|
|
151 |
|
9480
|
152 |
findFirstKey:aBlock
|
|
153 |
"find and return the first key, for which evaluation of the argument, aBlock
|
|
154 |
returns true; return nil if none is detected."
|
|
155 |
|
|
156 |
self keysDo:[:key |
|
|
157 |
(aBlock value:key) ifTrue:[^ key].
|
|
158 |
].
|
|
159 |
^ nil
|
|
160 |
|
|
161 |
"Created: 8.10.1996 / 22:01:31 / cg"
|
|
162 |
"Modified: 8.10.1996 / 22:02:03 / cg"
|
|
163 |
!
|
|
164 |
|
3615
|
165 |
keysAndValuesDo:aBlock
|
|
166 |
"evaluate aBlock for each key and value"
|
|
167 |
|
|
168 |
^ self subclassResponsibility
|
|
169 |
|
|
170 |
"Created: / 19.6.1998 / 00:56:52 / cg"
|
|
171 |
!
|
|
172 |
|
|
173 |
keysDo:aBlock
|
|
174 |
"evaluate aBlock for each key"
|
|
175 |
|
|
176 |
self keysAndValuesDo:[:elKey :elValue | aBlock value:elKey]
|
|
177 |
|
|
178 |
"Created: / 19.6.1998 / 00:56:34 / cg"
|
|
179 |
! !
|
|
180 |
|
|
181 |
!KeyedCollection methodsFor:'removing'!
|
|
182 |
|
|
183 |
removeKey:aKey
|
|
184 |
"remove key (and the value stored under that key) from the
|
|
185 |
receiver; raise an error if no such element is contained"
|
|
186 |
|
|
187 |
^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey]
|
|
188 |
|
|
189 |
"Created: / 19.6.1998 / 00:53:25 / cg"
|
|
190 |
"Modified: / 19.6.1998 / 00:54:02 / cg"
|
|
191 |
!
|
|
192 |
|
|
193 |
removeKey:aKey ifAbsent:exceptionBlock
|
|
194 |
"remove key (and the value stored under that key) from the
|
|
195 |
receiver; if no such element is contained, return the value
|
|
196 |
from evaluating exceptionBlock"
|
|
197 |
|
|
198 |
^ self subclassResponsibility
|
|
199 |
|
|
200 |
"Created: / 19.6.1998 / 00:53:58 / cg"
|
|
201 |
! !
|
|
202 |
|
|
203 |
!KeyedCollection methodsFor:'testing'!
|
|
204 |
|
10533
|
205 |
includesIdenticalKey:aKey
|
|
206 |
"return true, if the argument, aKey is a key in the receiver"
|
|
207 |
|
|
208 |
self keysDo:[:elKey | aKey == elKey ifTrue:[^ true]].
|
|
209 |
^ false
|
|
210 |
|
|
211 |
"Created: / 19.6.1998 / 00:55:05 / cg"
|
|
212 |
!
|
|
213 |
|
3615
|
214 |
includesKey:aKey
|
|
215 |
"return true, if the argument, aKey is a key in the receiver"
|
|
216 |
|
|
217 |
self keysDo:[:elKey | aKey = elKey ifTrue:[^ true]].
|
|
218 |
^ false
|
|
219 |
|
|
220 |
"Created: / 19.6.1998 / 00:55:05 / cg"
|
|
221 |
! !
|
|
222 |
|
|
223 |
!KeyedCollection class methodsFor:'documentation'!
|
|
224 |
|
|
225 |
version
|
17711
|
226 |
^ '$Id: KeyedCollection.st 10447 2009-06-14 13:09:55Z vranyj1 $'
|
3615
|
227 |
! !
|