363
|
1 |
Object subclass:#UnitConverter
|
|
2 |
instanceVariableNames:''
|
|
3 |
classVariableNames:'Conversions Scaling Aliases'
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Magnitude-General'
|
|
6 |
!
|
|
7 |
|
|
8 |
!UnitConverter class methodsFor:'documentation'!
|
|
9 |
|
|
10 |
documentation
|
|
11 |
"
|
|
12 |
In order to collect various unit conversions into one central
|
|
13 |
manageable (and especially: browsable) place, all previously
|
|
14 |
spread knowledge about metric (and other) conversions have been
|
|
15 |
brought into this class.
|
|
16 |
|
|
17 |
This class is purely functional, abstract and has no instances;
|
|
18 |
all queries are via class protocol.
|
|
19 |
Choosing the Magnitudes category as its home is arbitrary.
|
|
20 |
|
364
|
21 |
The units supported are setup in the classes initialize method;
|
|
22 |
supported are:
|
|
23 |
meter - inch - feet - foot - yard
|
|
24 |
meter2 - acre - 'german-ar' 'german-hektar' are
|
|
25 |
liter - gallon - barrel - floz
|
|
26 |
and many others.
|
|
27 |
|
363
|
28 |
[author:]
|
|
29 |
Claus Gittinger
|
|
30 |
|
|
31 |
see also:
|
|
32 |
examples
|
|
33 |
/usr/share/lib/unittab (if present on your system)
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
examples
|
|
38 |
"
|
|
39 |
ever wanted to know, how many floz's are there in a european
|
|
40 |
Coce bottle ?
|
|
41 |
[exBegin]
|
|
42 |
Transcript showCR:
|
|
43 |
(UnitConverter convert:1 from:#liter to:#floz)
|
|
44 |
[exEnd]
|
|
45 |
or, how many square-meters an acre is:
|
|
46 |
[exBegin]
|
|
47 |
Transcript showCR:
|
|
48 |
(UnitConverter convert:1 from:#acre to:#meter2)
|
|
49 |
[exEnd]
|
|
50 |
|
|
51 |
real estate buyers might want to know, how many acres
|
|
52 |
a german ar is:
|
|
53 |
[exBegin]
|
|
54 |
Transcript showCR:
|
|
55 |
(UnitConverter convert:1 from:#'german-ar' to:#acre)
|
|
56 |
[exEnd]
|
364
|
57 |
or how many square-feet are there in your living room:
|
|
58 |
[exBegin]
|
|
59 |
Transcript showCR:
|
|
60 |
(UnitConverter convert:100 from:#meter2 to:#foot2)
|
|
61 |
[exEnd]
|
365
|
62 |
how wide is a US page in inches:
|
|
63 |
[exBegin]
|
|
64 |
Transcript showCR:
|
|
65 |
(UnitConverter convert:1 from:#'letterW' to:#inch)
|
|
66 |
[exEnd]
|
|
67 |
in millimeter:
|
|
68 |
[exBegin]
|
|
69 |
Transcript showCR:
|
|
70 |
(UnitConverter convert:1 from:#'letterW' to:#mm)
|
|
71 |
[exEnd]
|
|
72 |
the height of a US page in inches:
|
|
73 |
[exBegin]
|
|
74 |
Transcript showCR:
|
|
75 |
(UnitConverter convert:1 from:#'letterH' to:#inch)
|
|
76 |
[exEnd]
|
|
77 |
in millimeter:
|
|
78 |
[exBegin]
|
|
79 |
Transcript showCR:
|
|
80 |
(UnitConverter convert:1 from:#'letterH' to:#mm)
|
|
81 |
[exEnd]
|
|
82 |
the same for european A4 standard page:
|
|
83 |
[exBegin]
|
|
84 |
Transcript showCR:
|
|
85 |
(UnitConverter convert:1 from:#'a4H' to:#mm)
|
|
86 |
[exEnd]
|
363
|
87 |
"
|
|
88 |
! !
|
|
89 |
|
|
90 |
!UnitConverter class methodsFor:'initialization'!
|
|
91 |
|
|
92 |
initialize
|
|
93 |
"initialize common conversions"
|
|
94 |
|
|
95 |
Scaling := IdentityDictionary new.
|
|
96 |
Conversions := IdentityDictionary new.
|
|
97 |
Aliases := IdentityDictionary new.
|
|
98 |
|
|
99 |
Scaling at:#tera put:1000000000000.
|
|
100 |
Scaling at:#giga put:1000000000.
|
|
101 |
Scaling at:#mega put:1000000.
|
|
102 |
Scaling at:#kilo put:1000.
|
|
103 |
Scaling at:#deci put:(1/10).
|
|
104 |
Scaling at:#centi put:(1/100).
|
|
105 |
Scaling at:#milli put:(1/1000).
|
|
106 |
Scaling at:#micro put:(1/1000000).
|
|
107 |
Scaling at:#nano put:(1/1000000000).
|
|
108 |
Scaling at:#pico put:(1/1000000000000).
|
|
109 |
|
|
110 |
"/ -------------- length -------------
|
|
111 |
|
|
112 |
Aliases at:#km put:#kilometer.
|
|
113 |
Aliases at:#m put:#meter.
|
|
114 |
Aliases at:#dm put:#decimeter.
|
|
115 |
Aliases at:#cm put:#centimeter.
|
|
116 |
Aliases at:#mm put:#millimeter.
|
|
117 |
Aliases at:#micron put:#micrometer.
|
|
118 |
Aliases at:#nm put:#nanometer.
|
|
119 |
Aliases at:#angstrom put:#decinanometer.
|
|
120 |
|
|
121 |
"/ US
|
|
122 |
self addConversion:12 from:#foot to:#inch.
|
|
123 |
self addConversion:3 from:#yard to:#foot.
|
|
124 |
self addConversion:5280 from:#mile to:#foot.
|
|
125 |
|
|
126 |
Aliases at:#feet put:#foot.
|
|
127 |
Aliases at:#ft put:#foot.
|
|
128 |
Aliases at:#yd put:#yard.
|
|
129 |
Aliases at:#mi put:#mile.
|
|
130 |
|
|
131 |
"/ inch to millimeter
|
|
132 |
self addConversion:(25.4/1000) from:#inch to:#meter.
|
|
133 |
|
365
|
134 |
"/ nautic
|
|
135 |
self addConversion:1852 from:#'nautical-mile' to:#meter.
|
|
136 |
|
385
|
137 |
|
|
138 |
"/ ---------- printing -------------
|
365
|
139 |
|
363
|
140 |
"/ inch to (roughly) a typesetter point
|
|
141 |
self addConversion:(1/72) from:#point to:#inch.
|
|
142 |
|
|
143 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point
|
|
144 |
self addConversion:(1/20) from:#twip to:#point.
|
|
145 |
|
365
|
146 |
"/ US paper
|
|
147 |
self addConversion:11 from:#'letter-page-height' to:#inch.
|
|
148 |
self addConversion:8.5 from:#'letter-page-width' to:#inch.
|
|
149 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch.
|
|
150 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch.
|
|
151 |
|
385
|
152 |
self addConversion:14 from:#'legal-page-height' to:#inch.
|
|
153 |
self addConversion:8.5 from:#'legal-page-width' to:#inch.
|
|
154 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch.
|
|
155 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch.
|
|
156 |
|
|
157 |
self addConversion:11 from:#'ledger-page-height' to:#inch.
|
|
158 |
self addConversion:17 from:#'ledger-page-width' to:#inch.
|
|
159 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch.
|
|
160 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch.
|
|
161 |
|
365
|
162 |
"/ European paper
|
|
163 |
self addConversion:840 from:#'a1-page-height' to:#millimeter.
|
|
164 |
self addConversion:592 from:#'a1-page-width' to:#millimeter.
|
|
165 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter.
|
|
166 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter.
|
|
167 |
|
|
168 |
self addConversion:592 from:#'a2-page-height' to:#millimeter.
|
|
169 |
self addConversion:420 from:#'a2-page-width' to:#millimeter.
|
|
170 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter.
|
|
171 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter.
|
|
172 |
|
|
173 |
self addConversion:420 from:#'a3-page-height' to:#millimeter.
|
|
174 |
self addConversion:296 from:#'a3-page-width' to:#millimeter.
|
|
175 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter.
|
|
176 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter.
|
|
177 |
|
|
178 |
self addConversion:296 from:#'a4-page-height' to:#millimeter.
|
|
179 |
self addConversion:210 from:#'a4-page-width' to:#millimeter.
|
|
180 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter.
|
|
181 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter.
|
|
182 |
|
|
183 |
self addConversion:210 from:#'a5-page-height' to:#millimeter.
|
|
184 |
self addConversion:148 from:#'a5-page-width' to:#millimeter.
|
|
185 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter.
|
|
186 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter.
|
|
187 |
|
|
188 |
self addConversion:148 from:#'a6-page-height' to:#millimeter.
|
|
189 |
self addConversion:105 from:#'a6-page-width' to:#millimeter.
|
|
190 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter.
|
|
191 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter.
|
|
192 |
|
385
|
193 |
self addConversion:257 from:#'b5-page-height' to:#millimeter.
|
|
194 |
self addConversion:182 from:#'b5-page-width' to:#millimeter.
|
|
195 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter.
|
|
196 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter.
|
|
197 |
|
|
198 |
|
365
|
199 |
Aliases at:#letterW put:#'letter-page-width'.
|
|
200 |
Aliases at:#letterH put:#'letter-page-height'.
|
385
|
201 |
Aliases at:#legalW put:#'legal-page-width'.
|
|
202 |
Aliases at:#legalH put:#'legal-page-height'.
|
|
203 |
Aliases at:#ledgerW put:#'ledger-page-width'.
|
|
204 |
Aliases at:#ledgerH put:#'ledger-page-height'.
|
365
|
205 |
Aliases at:#a1W put:#'a1-page-width'.
|
|
206 |
Aliases at:#a1H put:#'a1-page-height'.
|
|
207 |
Aliases at:#a2W put:#'a2-page-width'.
|
|
208 |
Aliases at:#a2H put:#'a2-page-height'.
|
|
209 |
Aliases at:#a3W put:#'a3-page-width'.
|
|
210 |
Aliases at:#a3H put:#'a3-page-height'.
|
|
211 |
Aliases at:#a4W put:#'a4-page-width'.
|
|
212 |
Aliases at:#a4H put:#'a4-page-height'.
|
|
213 |
Aliases at:#a5W put:#'a5-page-width'.
|
|
214 |
Aliases at:#a5H put:#'a5-page-height'.
|
|
215 |
Aliases at:#a6W put:#'a6-page-width'.
|
|
216 |
Aliases at:#a6H put:#'a6-page-height'.
|
385
|
217 |
Aliases at:#b5W put:#'b5-page-width'.
|
|
218 |
Aliases at:#b5H put:#'b5-page-height'.
|
365
|
219 |
|
|
220 |
Aliases at:#letterlW put:#'letter-landscape-page-width'.
|
|
221 |
Aliases at:#letterlH put:#'letter-landscape-page-height'.
|
385
|
222 |
Aliases at:#legallW put:#'legal-landscape-page-width'.
|
|
223 |
Aliases at:#legallH put:#'legal-landscape-page-height'.
|
|
224 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'.
|
|
225 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'.
|
365
|
226 |
Aliases at:#a1lW put:#'a1-landscape-page-width'.
|
|
227 |
Aliases at:#a1lH put:#'a1-landscape-page-height'.
|
|
228 |
Aliases at:#a2lW put:#'a2-landscape-page-width'.
|
|
229 |
Aliases at:#a2lH put:#'a2-landscape-page-height'.
|
|
230 |
Aliases at:#a3lW put:#'a3-landscape-page-width'.
|
|
231 |
Aliases at:#a3lH put:#'a3-landscape-page-height'.
|
|
232 |
Aliases at:#a4lW put:#'a4-landscape-page-width'.
|
|
233 |
Aliases at:#a4lH put:#'a4-landscape-page-height'.
|
|
234 |
Aliases at:#a5lW put:#'a5-landscape-page-width'.
|
|
235 |
Aliases at:#a5lH put:#'a5-landscape-page-height'.
|
|
236 |
Aliases at:#a6lW put:#'a6-landscape-page-width'.
|
|
237 |
Aliases at:#a6lH put:#'a6-landscape-page-height'.
|
385
|
238 |
Aliases at:#b5lW put:#'b5-landscape-page-width'.
|
|
239 |
Aliases at:#b5lH put:#'b5-landscape-page-height'.
|
363
|
240 |
|
|
241 |
"/ ---------------- area -------------------
|
|
242 |
|
|
243 |
"/ US
|
|
244 |
self addConversion:4840 from:#acre to:#yard2.
|
|
245 |
|
|
246 |
self addConversion:100 from:#are to:#meter2.
|
|
247 |
self addConversion:100 from:#hectare to:#are.
|
|
248 |
|
|
249 |
"/ german area - add your countries, and return to me ...
|
|
250 |
Aliases at:#'german-ar' put:#are.
|
|
251 |
Aliases at:#'german-hektar' put:#hectare.
|
|
252 |
|
|
253 |
"/ ---------------- liquid ----------------
|
|
254 |
|
|
255 |
self addConversion:231 from:#gallon to:#inch3.
|
|
256 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here,
|
|
257 |
"/ thats also 1/4th of a good wine ;-)
|
|
258 |
self addConversion:(1/2) from:#pint to:#quart.
|
|
259 |
self addConversion:(1/16) from:#floz to:#pint.
|
|
260 |
self addConversion:(1/8) from:#fldr to:#floz.
|
|
261 |
|
|
262 |
self addConversion:42 from:#barrel to:#gallon.
|
|
263 |
|
|
264 |
Aliases at:#cc put:#cm3.
|
|
265 |
Aliases at:#liter put:#kilocc.
|
|
266 |
Aliases at:#ml put:#milliliter.
|
|
267 |
Aliases at:#gal put:#gallon.
|
|
268 |
Aliases at:#qt put:#quart.
|
|
269 |
|
|
270 |
"/ ---------------- mass ----------------
|
|
271 |
self addConversion:453.59237 from:#lb to:#gram.
|
|
272 |
self addConversion:205 from:#carat to:#milligram.
|
|
273 |
|
|
274 |
Aliases at:#oz put:#ounce.
|
|
275 |
|
|
276 |
Aliases at:#gm put:#gram.
|
|
277 |
Aliases at:#kg put:#kilogram.
|
|
278 |
|
|
279 |
"/ well, a pint of beer is not always:
|
|
280 |
self addConversion:277.420 from:#'british-gallon' to:#in3.
|
|
281 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'.
|
|
282 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'.
|
|
283 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'.
|
|
284 |
|
|
285 |
"/ ---------------- energy ----------------
|
|
286 |
|
|
287 |
Aliases at:#cal put:#calorie.
|
|
288 |
Aliases at:#nt put:#newtom.
|
|
289 |
|
|
290 |
self addConversion:(1 / 4.1868) from:#calorie to:#joule.
|
|
291 |
|
|
292 |
"/ ---------------- cooking ----------------
|
|
293 |
|
|
294 |
self addConversion:4 from:#tablespoon to:#fldr.
|
|
295 |
self addConversion:(4/3) from:#teaspoon to:#fldr.
|
|
296 |
|
|
297 |
"
|
|
298 |
UnitConverter initialize
|
|
299 |
"
|
|
300 |
|
385
|
301 |
"Modified: 4.6.1996 / 17:14:46 / cg"
|
363
|
302 |
! !
|
|
303 |
|
|
304 |
!UnitConverter class methodsFor:'conversions'!
|
|
305 |
|
|
306 |
convert:howMany from:sourceUnit to:destUnit
|
|
307 |
"given a value in sourceUnit (symbolic), try to convert it
|
|
308 |
to destUnit (symbolic); return nil, if the conversion is
|
|
309 |
unknown."
|
|
310 |
|
450
|
311 |
|u conversions alias rslt sU dU|
|
363
|
312 |
|
|
313 |
"/ somehow, recursion must end ...
|
|
314 |
sourceUnit == destUnit ifTrue:[
|
|
315 |
^ howMany
|
|
316 |
].
|
|
317 |
|
369
|
318 |
sourceUnit isSymbol ifFalse:[
|
|
319 |
sU := sourceUnit asSymbolIfInterned.
|
|
320 |
sU isNil ifTrue:[^ nil].
|
|
321 |
^ self convert:howMany from:sU to:destUnit
|
|
322 |
].
|
|
323 |
destUnit isSymbol ifFalse:[
|
|
324 |
dU := destUnit asSymbolIfInterned.
|
|
325 |
dU isNil ifTrue:[^ nil].
|
|
326 |
^ self convert:howMany from:sourceUnit to:dU
|
|
327 |
].
|
|
328 |
|
363
|
329 |
"/ first, get rid of scalers ...
|
|
330 |
|
|
331 |
u := self unscaled:sourceUnit.
|
|
332 |
u notNil ifTrue:[
|
|
333 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit
|
|
334 |
].
|
|
335 |
|
|
336 |
u := self unscaled:destUnit.
|
|
337 |
u notNil ifTrue:[
|
|
338 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key)
|
|
339 |
].
|
|
340 |
|
|
341 |
"/ and of aliases ...
|
|
342 |
|
|
343 |
alias := Aliases at:sourceUnit ifAbsent:nil.
|
|
344 |
alias notNil ifTrue:[
|
|
345 |
^ self convert:howMany from:alias to:destUnit
|
|
346 |
].
|
|
347 |
alias := Aliases at:destUnit ifAbsent:nil.
|
|
348 |
alias notNil ifTrue:[
|
|
349 |
^ self convert:howMany from:sourceUnit to:alias
|
|
350 |
].
|
|
351 |
|
|
352 |
"/ working with squares or cubics ?
|
|
353 |
|
|
354 |
((sourceUnit endsWith:$2) and:[destUnit endsWith:$2]) ifTrue:[
|
|
355 |
sU := (sourceUnit copyWithoutLast:1) asSymbolIfInterned.
|
|
356 |
dU := (destUnit copyWithoutLast:1) asSymbolIfInterned.
|
|
357 |
(sU notNil and:[dU notNil]) ifTrue:[
|
|
358 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared
|
|
359 |
].
|
|
360 |
^ nil.
|
|
361 |
].
|
|
362 |
((sourceUnit endsWith:$3) and:[destUnit endsWith:$3]) ifTrue:[
|
|
363 |
sU := (sourceUnit copyWithoutLast:1) asSymbolIfInterned.
|
|
364 |
dU := (destUnit copyWithoutLast:1) asSymbolIfInterned.
|
|
365 |
(sU notNil and:[dU notNil]) ifTrue:[
|
|
366 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3
|
|
367 |
].
|
|
368 |
^ nil.
|
|
369 |
].
|
|
370 |
|
|
371 |
"/ the real work comes here ...
|
|
372 |
|
|
373 |
"/ is there a direct conversion in the dataBase ?
|
|
374 |
|
|
375 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit.
|
|
376 |
rslt notNil ifTrue:[^ rslt].
|
|
377 |
|
|
378 |
"/ try inverse conversion ...
|
|
379 |
|
|
380 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit.
|
|
381 |
rslt notNil ifTrue:[ ^ howMany / rslt].
|
|
382 |
|
|
383 |
"/ heres the deep recursion ...
|
|
384 |
|
|
385 |
|
|
386 |
"/ try indirect conversion from source
|
|
387 |
|
|
388 |
conversions := Conversions at:sourceUnit ifAbsent:nil.
|
|
389 |
conversions notNil ifTrue:[
|
|
390 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 |
|
|
391 |
|factor2|
|
|
392 |
|
|
393 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit.
|
|
394 |
factor2 notNil ifTrue:[^ factor2 * howMany].
|
|
395 |
].
|
|
396 |
].
|
|
397 |
|
|
398 |
"/ try indirect conversion from dest
|
|
399 |
|
|
400 |
conversions := Conversions at:destUnit ifAbsent:nil.
|
|
401 |
conversions notNil ifTrue:[
|
|
402 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 |
|
|
403 |
|factor2|
|
|
404 |
|
|
405 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit.
|
|
406 |
factor2 notNil ifTrue:[^ howMany / factor2].
|
|
407 |
].
|
|
408 |
].
|
|
409 |
|
|
410 |
^ nil
|
|
411 |
|
|
412 |
"direct - how many meters are there in two inches:
|
|
413 |
UnitConverter convert:2 from:#inch to:#meter
|
|
414 |
|
|
415 |
reverse - how many inches are there in one meter
|
|
416 |
UnitConverter convert:1 from:#meter to:#inch
|
|
417 |
|
|
418 |
with alias:
|
|
419 |
UnitConverter convert:1 from:#inch to:#m
|
|
420 |
UnitConverter convert:1 from:#inch to:#mm
|
|
421 |
UnitConverter convert:1 from:#inch to:#millimeter
|
|
422 |
UnitConverter convert:1 from:#inch to:#nanometer
|
|
423 |
UnitConverter convert:1 from:#mm to:#km
|
|
424 |
UnitConverter convert:1 from:#km to:#foot
|
|
425 |
|
|
426 |
indirect:
|
|
427 |
UnitConverter convert:1 from:#mm to:#twip
|
|
428 |
UnitConverter convert:1 from:#inch to:#twip
|
369
|
429 |
UnitConverter convert:1 from:'letterH' to:#point
|
|
430 |
UnitConverter convert:1 from:'letterlH' to:#point
|
364
|
431 |
|
|
432 |
UnitConverter convert:5 from:#barrel to:#liter
|
|
433 |
UnitConverter convert:10 from:#kilogram to:#carat
|
|
434 |
|
|
435 |
UnitConverter convert:1 from:#liter to:#floz
|
363
|
436 |
"
|
|
437 |
|
|
438 |
"Created: 31.5.1996 / 16:23:38 / cg"
|
369
|
439 |
"Modified: 31.5.1996 / 23:11:50 / cg"
|
363
|
440 |
!
|
|
441 |
|
|
442 |
inchToMillimeter:inches
|
364
|
443 |
"convert inches to mm. Made this an extra method, since its so common."
|
363
|
444 |
|
|
445 |
^ inches * 25.4
|
|
446 |
|
|
447 |
"Created: 31.5.1996 / 13:37:31 / cg"
|
364
|
448 |
"Modified: 31.5.1996 / 18:08:14 / cg"
|
363
|
449 |
!
|
|
450 |
|
|
451 |
millimeterToInch:mm
|
364
|
452 |
"convert mm to inches. Made this an extra method, since its so common."
|
363
|
453 |
|
|
454 |
^ mm / 25.4
|
|
455 |
|
|
456 |
"Created: 31.5.1996 / 13:37:48 / cg"
|
364
|
457 |
"Modified: 31.5.1996 / 18:08:20 / cg"
|
363
|
458 |
! !
|
|
459 |
|
|
460 |
!UnitConverter class methodsFor:'private'!
|
|
461 |
|
|
462 |
addConversion:factor from:sourceMetric to:destMetric
|
|
463 |
"add a conversion"
|
|
464 |
|
|
465 |
|conversion|
|
|
466 |
|
|
467 |
conversion := Conversions at:sourceMetric ifAbsent:nil.
|
|
468 |
conversion isNil ifTrue:[
|
|
469 |
conversion := IdentityDictionary new.
|
|
470 |
Conversions at:sourceMetric put:conversion
|
|
471 |
].
|
|
472 |
conversion at:destMetric put:factor.
|
|
473 |
^ conversion
|
|
474 |
|
|
475 |
"Created: 31.5.1996 / 13:51:25 / cg"
|
|
476 |
!
|
|
477 |
|
|
478 |
convertDirect:howMany from:sourceMetric to:destMetric
|
|
479 |
"given a value in sourceMetric (symbolic), try to convert it
|
|
480 |
to destMetric (symbolic);
|
|
481 |
Only direct conversions are tried; return nil, if the conversion is unknown."
|
|
482 |
|
|
483 |
|conversion factor|
|
|
484 |
|
|
485 |
conversion := Conversions at:sourceMetric ifAbsent:nil.
|
|
486 |
conversion isNil ifTrue:[^ nil].
|
|
487 |
|
|
488 |
factor := conversion at:destMetric ifAbsent:nil.
|
|
489 |
factor isNil ifTrue:[^ nil].
|
|
490 |
|
364
|
491 |
factor isNumber ifTrue:[
|
|
492 |
^ howMany * factor
|
|
493 |
].
|
|
494 |
^ factor value * howMany
|
363
|
495 |
|
|
496 |
"
|
|
497 |
UnitConverter convertDirect:1 from:#inch to:#meter
|
|
498 |
UnitConverter convertDirect:1 from:#inch to:#millimeter
|
|
499 |
"
|
|
500 |
|
|
501 |
"Created: 31.5.1996 / 13:54:33 / cg"
|
364
|
502 |
"Modified: 31.5.1996 / 18:05:39 / cg"
|
363
|
503 |
!
|
|
504 |
|
|
505 |
unscaled:what
|
|
506 |
"given a unit, return the base and a factor as assoc,
|
|
507 |
or nil if not found"
|
|
508 |
|
|
509 |
Scaling keysAndValuesDo:[:name :factor |
|
|
510 |
|rest|
|
|
511 |
|
|
512 |
(what startsWith:name) ifTrue:[
|
|
513 |
rest := what copyFrom:(name size+1).
|
|
514 |
rest := rest asSymbolIfInterned.
|
|
515 |
rest notNil ifTrue:[
|
|
516 |
^ rest -> factor.
|
|
517 |
]
|
|
518 |
].
|
|
519 |
].
|
|
520 |
^ nil
|
|
521 |
|
|
522 |
"
|
|
523 |
UnitConverter unscaled:#millimeter
|
|
524 |
UnitConverter unscaled:#nanometer
|
|
525 |
UnitConverter unscaled:#kilometer
|
|
526 |
UnitConverter unscaled:#fuzzymeter
|
|
527 |
"
|
|
528 |
|
|
529 |
"Modified: 31.5.1996 / 14:57:25 / cg"
|
|
530 |
! !
|
|
531 |
|
|
532 |
!UnitConverter class methodsFor:'documentation'!
|
|
533 |
|
|
534 |
version
|
453
|
535 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.7 1996-10-22 19:34:57 cg Exp $'
|
363
|
536 |
! !
|
|
537 |
UnitConverter initialize!
|