author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 20 Dec 2010 07:13:27 +0000 | |
branch | jv |
changeset 17814 | b75a7f0c346b |
parent 17807 | 06cc6c49e291 |
child 17834 | 04ff72c5039a |
permissions | -rw-r--r-- |
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 |
!KeyedCollection methodsFor:'enumerating'! |
|
128 |
||
129 |
do:aBlock |
|
130 |
"evaluate aBlock for each value" |
|
131 |
||
132 |
self keysAndValuesDo:[:elKey :elValue | aBlock value:elValue] |
|
133 |
||
134 |
"Created: / 19.6.1998 / 00:56:24 / cg" |
|
135 |
! |
|
136 |
||
9480 | 137 |
findFirstKey:aBlock |
138 |
"find and return the first key, for which evaluation of the argument, aBlock |
|
139 |
returns true; return nil if none is detected." |
|
140 |
||
141 |
self keysDo:[:key | |
|
142 |
(aBlock value:key) ifTrue:[^ key]. |
|
143 |
]. |
|
144 |
^ nil |
|
145 |
||
146 |
"Created: 8.10.1996 / 22:01:31 / cg" |
|
147 |
"Modified: 8.10.1996 / 22:02:03 / cg" |
|
148 |
! |
|
149 |
||
3615 | 150 |
keysAndValuesDo:aBlock |
151 |
"evaluate aBlock for each key and value" |
|
152 |
||
153 |
^ self subclassResponsibility |
|
154 |
||
155 |
"Created: / 19.6.1998 / 00:56:52 / cg" |
|
156 |
! ! |
|
157 |
||
158 |
!KeyedCollection methodsFor:'removing'! |
|
159 |
||
160 |
removeKey:aKey |
|
161 |
"remove key (and the value stored under that key) from the |
|
162 |
receiver; raise an error if no such element is contained" |
|
163 |
||
164 |
^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey] |
|
165 |
||
166 |
"Created: / 19.6.1998 / 00:53:25 / cg" |
|
167 |
"Modified: / 19.6.1998 / 00:54:02 / cg" |
|
168 |
! |
|
169 |
||
170 |
removeKey:aKey ifAbsent:exceptionBlock |
|
171 |
"remove key (and the value stored under that key) from the |
|
172 |
receiver; if no such element is contained, return the value |
|
173 |
from evaluating exceptionBlock" |
|
174 |
||
175 |
^ self subclassResponsibility |
|
176 |
||
177 |
"Created: / 19.6.1998 / 00:53:58 / cg" |
|
178 |
! ! |
|
179 |
||
180 |
!KeyedCollection methodsFor:'testing'! |
|
181 |
||
10533 | 182 |
includesIdenticalKey:aKey |
183 |
"return true, if the argument, aKey is a key in the receiver" |
|
184 |
||
185 |
self keysDo:[:elKey | aKey == elKey ifTrue:[^ true]]. |
|
186 |
^ false |
|
187 |
||
188 |
"Created: / 19.6.1998 / 00:55:05 / cg" |
|
189 |
! |
|
190 |
||
3615 | 191 |
includesKey:aKey |
192 |
"return true, if the argument, aKey is a key in the receiver" |
|
193 |
||
194 |
self keysDo:[:elKey | aKey = elKey ifTrue:[^ true]]. |
|
195 |
^ false |
|
196 |
||
197 |
"Created: / 19.6.1998 / 00:55:05 / cg" |
|
198 |
! ! |
|
199 |
||
200 |
!KeyedCollection class methodsFor:'documentation'! |
|
201 |
||
202 |
version |
|
17814 | 203 |
^ '$Id: KeyedCollection.st 10602 2010-12-20 07:13:27Z vranyj1 $' |
17747 | 204 |
! |
205 |
||
206 |
version_CVS |
|
17814 | 207 |
^ 'Header: /cvs/stx/stx/libbasic/KeyedCollection.st,v 1.8 2010/08/24 08:29:08 cg Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
208 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
209 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
210 |
version_SVN |
17814 | 211 |
^ '$Id: KeyedCollection.st 10602 2010-12-20 07:13:27Z vranyj1 $' |
3615 | 212 |
! ! |
17767 | 213 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
214 |
|
17780 | 215 |
|
17797 | 216 |
|
17807 | 217 |
|
17814 | 218 |