363
|
1 |
Object subclass:#UnitConverter
|
|
2 |
instanceVariableNames:''
|
516
|
3 |
classVariableNames:'Conversions Scaling Aliases Constants'
|
363
|
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 |
|
515
|
21 |
The supported units are setup in the classes initialize method;
|
364
|
22 |
supported are:
|
|
23 |
meter - inch - feet - foot - yard
|
|
24 |
meter2 - acre - 'german-ar' 'german-hektar' are
|
|
25 |
liter - gallon - barrel - floz
|
515
|
26 |
celsius - fahrenheit
|
364
|
27 |
and many others.
|
|
28 |
|
515
|
29 |
The converter does a recursive search for a conversion;
|
|
30 |
thus, if there is a conversion from #inch to #millimeter and another
|
|
31 |
from #millimeter to #kilometer, conversion from #inch to #kilometer
|
|
32 |
is found automatically.
|
|
33 |
|
363
|
34 |
[author:]
|
|
35 |
Claus Gittinger
|
|
36 |
|
|
37 |
see also:
|
|
38 |
examples
|
|
39 |
/usr/share/lib/unittab (if present on your system)
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
examples
|
|
44 |
"
|
|
45 |
ever wanted to know, how many floz's are there in a european
|
|
46 |
Coce bottle ?
|
|
47 |
[exBegin]
|
|
48 |
Transcript showCR:
|
|
49 |
(UnitConverter convert:1 from:#liter to:#floz)
|
|
50 |
[exEnd]
|
|
51 |
or, how many square-meters an acre is:
|
|
52 |
[exBegin]
|
|
53 |
Transcript showCR:
|
516
|
54 |
(UnitConverter convert:1 from:#acre to:#'meter^2')
|
363
|
55 |
[exEnd]
|
|
56 |
|
516
|
57 |
|
515
|
58 |
or europeans might want to know, what those
|
|
59 |
fahrenheit numbers mean in an US weather report ;-):
|
|
60 |
[exBegin]
|
|
61 |
Transcript showCR:(
|
|
62 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius)
|
|
63 |
asFixedPoint:1)
|
|
64 |
[exEnd]
|
|
65 |
|
516
|
66 |
|
|
67 |
how fast do I drive ?
|
|
68 |
[exBegin]
|
|
69 |
Transcript showCR:(
|
|
70 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr')
|
|
71 |
[exEnd]
|
|
72 |
[exBegin]
|
|
73 |
Transcript showCR:(
|
|
74 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr')
|
|
75 |
[exEnd]
|
|
76 |
|
|
77 |
|
|
78 |
calories or joule ?
|
|
79 |
[exBegin]
|
|
80 |
Transcript showCR:(
|
|
81 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule')
|
|
82 |
[exEnd]
|
|
83 |
[exBegin]
|
|
84 |
Transcript showCR:(
|
|
85 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule')
|
|
86 |
[exEnd]
|
|
87 |
|
|
88 |
|
363
|
89 |
real estate buyers might want to know, how many acres
|
|
90 |
a german ar is:
|
|
91 |
[exBegin]
|
|
92 |
Transcript showCR:
|
|
93 |
(UnitConverter convert:1 from:#'german-ar' to:#acre)
|
|
94 |
[exEnd]
|
516
|
95 |
- or how many square-feet are there in your living room:
|
364
|
96 |
[exBegin]
|
|
97 |
Transcript showCR:
|
516
|
98 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2')
|
364
|
99 |
[exEnd]
|
516
|
100 |
|
|
101 |
|
365
|
102 |
how wide is a US page in inches:
|
|
103 |
[exBegin]
|
|
104 |
Transcript showCR:
|
|
105 |
(UnitConverter convert:1 from:#'letterW' to:#inch)
|
|
106 |
[exEnd]
|
516
|
107 |
- in millimeter:
|
365
|
108 |
[exBegin]
|
|
109 |
Transcript showCR:
|
|
110 |
(UnitConverter convert:1 from:#'letterW' to:#mm)
|
|
111 |
[exEnd]
|
516
|
112 |
|
|
113 |
|
365
|
114 |
the height of a US page in inches:
|
|
115 |
[exBegin]
|
|
116 |
Transcript showCR:
|
|
117 |
(UnitConverter convert:1 from:#'letterH' to:#inch)
|
|
118 |
[exEnd]
|
516
|
119 |
- in millimeter:
|
365
|
120 |
[exBegin]
|
|
121 |
Transcript showCR:
|
|
122 |
(UnitConverter convert:1 from:#'letterH' to:#mm)
|
|
123 |
[exEnd]
|
516
|
124 |
|
|
125 |
|
365
|
126 |
the same for european A4 standard page:
|
|
127 |
[exBegin]
|
|
128 |
Transcript showCR:
|
|
129 |
(UnitConverter convert:1 from:#'a4H' to:#mm)
|
|
130 |
[exEnd]
|
516
|
131 |
[exBegin]
|
|
132 |
Transcript showCR:
|
|
133 |
(UnitConverter convert:1 from:#'a4H' to:#inch)
|
|
134 |
[exEnd]
|
363
|
135 |
"
|
|
136 |
! !
|
|
137 |
|
|
138 |
!UnitConverter class methodsFor:'initialization'!
|
|
139 |
|
|
140 |
initialize
|
|
141 |
"initialize common conversions"
|
|
142 |
|
|
143 |
Scaling := IdentityDictionary new.
|
|
144 |
Conversions := IdentityDictionary new.
|
|
145 |
Aliases := IdentityDictionary new.
|
516
|
146 |
Constants := IdentityDictionary new.
|
363
|
147 |
|
515
|
148 |
Scaling at:#exa put:1000000000000000000.
|
|
149 |
Scaling at:#peta put:1000000000000000.
|
363
|
150 |
Scaling at:#tera put:1000000000000.
|
|
151 |
Scaling at:#giga put:1000000000.
|
|
152 |
Scaling at:#mega put:1000000.
|
515
|
153 |
Scaling at:#myria put:10000.
|
363
|
154 |
Scaling at:#kilo put:1000.
|
515
|
155 |
Scaling at:#hecto put:100.
|
363
|
156 |
Scaling at:#deci put:(1/10).
|
|
157 |
Scaling at:#centi put:(1/100).
|
|
158 |
Scaling at:#milli put:(1/1000).
|
|
159 |
Scaling at:#micro put:(1/1000000).
|
|
160 |
Scaling at:#nano put:(1/1000000000).
|
|
161 |
Scaling at:#pico put:(1/1000000000000).
|
515
|
162 |
Scaling at:#femto put:(1/1000000000000000).
|
|
163 |
Scaling at:#atto put:(1/1000000000000000000).
|
363
|
164 |
|
516
|
165 |
|
363
|
166 |
"/ -------------- length -------------
|
|
167 |
|
|
168 |
Aliases at:#km put:#kilometer.
|
|
169 |
Aliases at:#m put:#meter.
|
|
170 |
Aliases at:#dm put:#decimeter.
|
|
171 |
Aliases at:#cm put:#centimeter.
|
|
172 |
Aliases at:#mm put:#millimeter.
|
|
173 |
Aliases at:#micron put:#micrometer.
|
|
174 |
Aliases at:#nm put:#nanometer.
|
|
175 |
Aliases at:#angstrom put:#decinanometer.
|
|
176 |
|
|
177 |
"/ US
|
|
178 |
self addConversion:12 from:#foot to:#inch.
|
|
179 |
self addConversion:3 from:#yard to:#foot.
|
|
180 |
self addConversion:5280 from:#mile to:#foot.
|
515
|
181 |
self addConversion:0.001 from:#inch to:#mil.
|
363
|
182 |
|
|
183 |
Aliases at:#feet put:#foot.
|
|
184 |
Aliases at:#ft put:#foot.
|
|
185 |
Aliases at:#yd put:#yard.
|
|
186 |
Aliases at:#mi put:#mile.
|
|
187 |
|
|
188 |
"/ inch to millimeter
|
|
189 |
self addConversion:(25.4/1000) from:#inch to:#meter.
|
|
190 |
|
365
|
191 |
"/ nautic
|
|
192 |
self addConversion:1852 from:#'nautical-mile' to:#meter.
|
|
193 |
|
385
|
194 |
|
516
|
195 |
"/ ---------- time -------------
|
|
196 |
|
|
197 |
self addConversion:60 from:#min to:#s.
|
|
198 |
self addConversion:60 from:#hr to:#min.
|
|
199 |
self addConversion:24 from:#dy to:#hr.
|
|
200 |
self addConversion:365.24219879 from:#yr to:#dy.
|
|
201 |
|
|
202 |
|
|
203 |
"/ ---------- velocity -------------
|
|
204 |
|
|
205 |
Constants at:#lightSpeed put:#(2.997925E8 #'m/s').
|
|
206 |
|
|
207 |
|
385
|
208 |
"/ ---------- printing -------------
|
365
|
209 |
|
363
|
210 |
"/ inch to (roughly) a typesetter point
|
|
211 |
self addConversion:(1/72) from:#point to:#inch.
|
|
212 |
|
|
213 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point
|
|
214 |
self addConversion:(1/20) from:#twip to:#point.
|
|
215 |
|
365
|
216 |
"/ US paper
|
|
217 |
self addConversion:11 from:#'letter-page-height' to:#inch.
|
|
218 |
self addConversion:8.5 from:#'letter-page-width' to:#inch.
|
|
219 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch.
|
|
220 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch.
|
|
221 |
|
385
|
222 |
self addConversion:14 from:#'legal-page-height' to:#inch.
|
|
223 |
self addConversion:8.5 from:#'legal-page-width' to:#inch.
|
|
224 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch.
|
|
225 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch.
|
|
226 |
|
|
227 |
self addConversion:11 from:#'ledger-page-height' to:#inch.
|
|
228 |
self addConversion:17 from:#'ledger-page-width' to:#inch.
|
|
229 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch.
|
|
230 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch.
|
|
231 |
|
365
|
232 |
"/ European paper
|
|
233 |
self addConversion:840 from:#'a1-page-height' to:#millimeter.
|
|
234 |
self addConversion:592 from:#'a1-page-width' to:#millimeter.
|
|
235 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter.
|
|
236 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter.
|
|
237 |
|
|
238 |
self addConversion:592 from:#'a2-page-height' to:#millimeter.
|
|
239 |
self addConversion:420 from:#'a2-page-width' to:#millimeter.
|
|
240 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter.
|
|
241 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter.
|
|
242 |
|
|
243 |
self addConversion:420 from:#'a3-page-height' to:#millimeter.
|
|
244 |
self addConversion:296 from:#'a3-page-width' to:#millimeter.
|
|
245 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter.
|
|
246 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter.
|
|
247 |
|
|
248 |
self addConversion:296 from:#'a4-page-height' to:#millimeter.
|
|
249 |
self addConversion:210 from:#'a4-page-width' to:#millimeter.
|
|
250 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter.
|
|
251 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter.
|
|
252 |
|
|
253 |
self addConversion:210 from:#'a5-page-height' to:#millimeter.
|
|
254 |
self addConversion:148 from:#'a5-page-width' to:#millimeter.
|
|
255 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter.
|
|
256 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter.
|
|
257 |
|
|
258 |
self addConversion:148 from:#'a6-page-height' to:#millimeter.
|
|
259 |
self addConversion:105 from:#'a6-page-width' to:#millimeter.
|
|
260 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter.
|
|
261 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter.
|
|
262 |
|
385
|
263 |
self addConversion:257 from:#'b5-page-height' to:#millimeter.
|
|
264 |
self addConversion:182 from:#'b5-page-width' to:#millimeter.
|
|
265 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter.
|
|
266 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter.
|
|
267 |
|
|
268 |
|
365
|
269 |
Aliases at:#letterW put:#'letter-page-width'.
|
|
270 |
Aliases at:#letterH put:#'letter-page-height'.
|
385
|
271 |
Aliases at:#legalW put:#'legal-page-width'.
|
|
272 |
Aliases at:#legalH put:#'legal-page-height'.
|
|
273 |
Aliases at:#ledgerW put:#'ledger-page-width'.
|
|
274 |
Aliases at:#ledgerH put:#'ledger-page-height'.
|
365
|
275 |
Aliases at:#a1W put:#'a1-page-width'.
|
|
276 |
Aliases at:#a1H put:#'a1-page-height'.
|
|
277 |
Aliases at:#a2W put:#'a2-page-width'.
|
|
278 |
Aliases at:#a2H put:#'a2-page-height'.
|
|
279 |
Aliases at:#a3W put:#'a3-page-width'.
|
|
280 |
Aliases at:#a3H put:#'a3-page-height'.
|
|
281 |
Aliases at:#a4W put:#'a4-page-width'.
|
|
282 |
Aliases at:#a4H put:#'a4-page-height'.
|
|
283 |
Aliases at:#a5W put:#'a5-page-width'.
|
|
284 |
Aliases at:#a5H put:#'a5-page-height'.
|
|
285 |
Aliases at:#a6W put:#'a6-page-width'.
|
|
286 |
Aliases at:#a6H put:#'a6-page-height'.
|
385
|
287 |
Aliases at:#b5W put:#'b5-page-width'.
|
|
288 |
Aliases at:#b5H put:#'b5-page-height'.
|
365
|
289 |
|
|
290 |
Aliases at:#letterlW put:#'letter-landscape-page-width'.
|
|
291 |
Aliases at:#letterlH put:#'letter-landscape-page-height'.
|
385
|
292 |
Aliases at:#legallW put:#'legal-landscape-page-width'.
|
|
293 |
Aliases at:#legallH put:#'legal-landscape-page-height'.
|
|
294 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'.
|
|
295 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'.
|
365
|
296 |
Aliases at:#a1lW put:#'a1-landscape-page-width'.
|
|
297 |
Aliases at:#a1lH put:#'a1-landscape-page-height'.
|
|
298 |
Aliases at:#a2lW put:#'a2-landscape-page-width'.
|
|
299 |
Aliases at:#a2lH put:#'a2-landscape-page-height'.
|
|
300 |
Aliases at:#a3lW put:#'a3-landscape-page-width'.
|
|
301 |
Aliases at:#a3lH put:#'a3-landscape-page-height'.
|
|
302 |
Aliases at:#a4lW put:#'a4-landscape-page-width'.
|
|
303 |
Aliases at:#a4lH put:#'a4-landscape-page-height'.
|
|
304 |
Aliases at:#a5lW put:#'a5-landscape-page-width'.
|
|
305 |
Aliases at:#a5lH put:#'a5-landscape-page-height'.
|
|
306 |
Aliases at:#a6lW put:#'a6-landscape-page-width'.
|
|
307 |
Aliases at:#a6lH put:#'a6-landscape-page-height'.
|
385
|
308 |
Aliases at:#b5lW put:#'b5-landscape-page-width'.
|
|
309 |
Aliases at:#b5lH put:#'b5-landscape-page-height'.
|
363
|
310 |
|
516
|
311 |
|
363
|
312 |
"/ ---------------- area -------------------
|
|
313 |
|
|
314 |
"/ US
|
516
|
315 |
self addConversion:4840 from:#acre to:#'yard^2'.
|
363
|
316 |
|
516
|
317 |
self addConversion:100 from:#are to:#'meter^2'.
|
363
|
318 |
self addConversion:100 from:#hectare to:#are.
|
|
319 |
|
|
320 |
"/ german area - add your countries, and return to me ...
|
|
321 |
Aliases at:#'german-ar' put:#are.
|
|
322 |
Aliases at:#'german-hektar' put:#hectare.
|
|
323 |
|
516
|
324 |
|
363
|
325 |
"/ ---------------- liquid ----------------
|
|
326 |
|
516
|
327 |
self addConversion:231 from:#gallon to:#'inch^3'.
|
363
|
328 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here,
|
|
329 |
"/ thats also 1/4th of a good wine ;-)
|
|
330 |
self addConversion:(1/2) from:#pint to:#quart.
|
|
331 |
self addConversion:(1/16) from:#floz to:#pint.
|
|
332 |
self addConversion:(1/8) from:#fldr to:#floz.
|
|
333 |
|
|
334 |
self addConversion:42 from:#barrel to:#gallon.
|
|
335 |
|
516
|
336 |
Aliases at:#cc put:#'cm^3'.
|
363
|
337 |
Aliases at:#liter put:#kilocc.
|
|
338 |
Aliases at:#ml put:#milliliter.
|
|
339 |
Aliases at:#gal put:#gallon.
|
|
340 |
Aliases at:#qt put:#quart.
|
|
341 |
|
516
|
342 |
|
363
|
343 |
"/ ---------------- mass ----------------
|
515
|
344 |
|
363
|
345 |
self addConversion:453.59237 from:#lb to:#gram.
|
|
346 |
self addConversion:205 from:#carat to:#milligram.
|
|
347 |
|
|
348 |
Aliases at:#oz put:#ounce.
|
|
349 |
|
|
350 |
Aliases at:#gm put:#gram.
|
|
351 |
Aliases at:#kg put:#kilogram.
|
|
352 |
|
|
353 |
"/ well, a pint of beer is not always:
|
516
|
354 |
self addConversion:277.420 from:#'british-gallon' to:#'in^3'.
|
363
|
355 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'.
|
|
356 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'.
|
|
357 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'.
|
|
358 |
|
516
|
359 |
|
363
|
360 |
"/ ---------------- energy ----------------
|
|
361 |
|
|
362 |
Aliases at:#cal put:#calorie.
|
516
|
363 |
Aliases at:#nt put:#newton.
|
|
364 |
Aliases at:#joule put:#'nt*m'.
|
363
|
365 |
|
516
|
366 |
self addConversion:4.1868 from:#calorie to:#joule.
|
|
367 |
|
363
|
368 |
|
|
369 |
"/ ---------------- cooking ----------------
|
|
370 |
|
|
371 |
self addConversion:4 from:#tablespoon to:#fldr.
|
|
372 |
self addConversion:(4/3) from:#teaspoon to:#fldr.
|
|
373 |
|
516
|
374 |
|
515
|
375 |
"/ ---------------- temperature ----------------
|
|
376 |
|
|
377 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit.
|
|
378 |
self addConversion:[:f | f - 32 / 1.8] from:#fahrenheit to:#celsius.
|
|
379 |
|
363
|
380 |
"
|
|
381 |
UnitConverter initialize
|
|
382 |
"
|
|
383 |
|
516
|
384 |
"Modified: 29.3.1997 / 19:11:52 / cg"
|
363
|
385 |
! !
|
|
386 |
|
|
387 |
!UnitConverter class methodsFor:'conversions'!
|
|
388 |
|
|
389 |
convert:howMany from:sourceUnit to:destUnit
|
|
390 |
"given a value in sourceUnit (symbolic), try to convert it
|
|
391 |
to destUnit (symbolic); return nil, if the conversion is
|
|
392 |
unknown."
|
|
393 |
|
516
|
394 |
|u conversions alias rslt sU dU const val unit
|
|
395 |
i suNumerator suDenominator duNumerator duDenominator uN uD
|
|
396 |
sF1 sF2 dF1 dF2
|
|
397 |
s d|
|
363
|
398 |
|
|
399 |
"/ somehow, recursion must end ...
|
|
400 |
sourceUnit == destUnit ifTrue:[
|
|
401 |
^ howMany
|
|
402 |
].
|
|
403 |
|
369
|
404 |
sourceUnit isSymbol ifFalse:[
|
516
|
405 |
s := sourceUnit withoutSeparators.
|
|
406 |
sU := s asSymbolIfInterned.
|
|
407 |
sU isNil ifTrue:[
|
|
408 |
(s startsWith:$() ifTrue:[
|
|
409 |
(s endsWith:$)) ifTrue:[
|
|
410 |
s := s copyFrom:2 to:(s size - 1).
|
|
411 |
^ self convert:howMany from:s to:destUnit
|
|
412 |
]
|
|
413 |
].
|
|
414 |
^ nil
|
|
415 |
].
|
369
|
416 |
^ self convert:howMany from:sU to:destUnit
|
|
417 |
].
|
|
418 |
destUnit isSymbol ifFalse:[
|
516
|
419 |
d := destUnit withoutSeparators.
|
|
420 |
dU := d asSymbolIfInterned.
|
|
421 |
dU isNil ifTrue:[
|
|
422 |
(d startsWith:$() ifTrue:[
|
|
423 |
(d endsWith:$)) ifTrue:[
|
|
424 |
d := d copyFrom:2 to:(d size - 1).
|
|
425 |
^ self convert:howMany from:sourceUnit to:d
|
|
426 |
]
|
|
427 |
].
|
|
428 |
^ nil
|
|
429 |
].
|
369
|
430 |
^ self convert:howMany from:sourceUnit to:dU
|
|
431 |
].
|
|
432 |
|
363
|
433 |
"/ first, get rid of scalers ...
|
|
434 |
|
|
435 |
u := self unscaled:sourceUnit.
|
|
436 |
u notNil ifTrue:[
|
|
437 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit
|
|
438 |
].
|
|
439 |
|
|
440 |
u := self unscaled:destUnit.
|
|
441 |
u notNil ifTrue:[
|
|
442 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key)
|
|
443 |
].
|
|
444 |
|
|
445 |
"/ and of aliases ...
|
|
446 |
|
|
447 |
alias := Aliases at:sourceUnit ifAbsent:nil.
|
|
448 |
alias notNil ifTrue:[
|
|
449 |
^ self convert:howMany from:alias to:destUnit
|
|
450 |
].
|
|
451 |
alias := Aliases at:destUnit ifAbsent:nil.
|
|
452 |
alias notNil ifTrue:[
|
|
453 |
^ self convert:howMany from:sourceUnit to:alias
|
|
454 |
].
|
|
455 |
|
516
|
456 |
"/ any constants ?
|
|
457 |
|
|
458 |
(Constants includesKey:sourceUnit) ifTrue:[
|
|
459 |
const := Constants at:sourceUnit.
|
|
460 |
val := const at:1.
|
|
461 |
unit := const at:2.
|
|
462 |
^ self convert:(howMany*val) from:unit to:destUnit
|
|
463 |
].
|
|
464 |
|
363
|
465 |
"/ working with squares or cubics ?
|
|
466 |
|
516
|
467 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[
|
|
468 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned.
|
|
469 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned.
|
363
|
470 |
(sU notNil and:[dU notNil]) ifTrue:[
|
|
471 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared
|
|
472 |
].
|
516
|
473 |
^ self noConversionFrom:sourceUnit to:destUnit.
|
363
|
474 |
].
|
516
|
475 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[
|
|
476 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned.
|
|
477 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned.
|
363
|
478 |
(sU notNil and:[dU notNil]) ifTrue:[
|
|
479 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3
|
|
480 |
].
|
516
|
481 |
^ self noConversionFrom:sourceUnit to:destUnit.
|
|
482 |
].
|
|
483 |
|
|
484 |
"/ working with fractions ?
|
|
485 |
|
|
486 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[
|
|
487 |
"/ look for a constant conversion factor
|
|
488 |
i := sourceUnit indexOf:$/.
|
|
489 |
suNumerator := sourceUnit copyTo:(i - 1).
|
|
490 |
suDenominator := sourceUnit copyFrom:(i + 1).
|
|
491 |
i := destUnit indexOf:$/.
|
|
492 |
duNumerator := destUnit copyTo:(i - 1).
|
|
493 |
duDenominator := destUnit copyFrom:(i + 1).
|
|
494 |
|
|
495 |
uN := self convert:howMany from:suNumerator to:duNumerator.
|
|
496 |
uN notNil ifTrue:[
|
|
497 |
uD := self convert:1 from:suDenominator to:duDenominator.
|
|
498 |
uD notNil ifTrue:[
|
|
499 |
^ uN / uD
|
|
500 |
]
|
|
501 |
].
|
|
502 |
].
|
|
503 |
|
|
504 |
"/ working with products ?
|
|
505 |
|
|
506 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[
|
|
507 |
i := sourceUnit indexOf:$*.
|
|
508 |
sF1 := sourceUnit copyTo:(i - 1).
|
|
509 |
sF2 := sourceUnit copyFrom:(i + 1).
|
|
510 |
i := destUnit indexOf:$*.
|
|
511 |
dF1 := destUnit copyTo:(i - 1).
|
|
512 |
dF2 := destUnit copyFrom:(i + 1).
|
|
513 |
|
|
514 |
u := self convert:howMany from:sF1 to:dF1.
|
|
515 |
u notNil ifTrue:[
|
|
516 |
u := self convert:u from:sF2 to:dF2.
|
|
517 |
u notNil ifTrue:[
|
|
518 |
^ u
|
|
519 |
]
|
|
520 |
].
|
363
|
521 |
].
|
|
522 |
|
|
523 |
"/ the real work comes here ...
|
|
524 |
|
|
525 |
"/ is there a direct conversion in the dataBase ?
|
|
526 |
|
|
527 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit.
|
|
528 |
rslt notNil ifTrue:[^ rslt].
|
|
529 |
|
|
530 |
"/ try inverse conversion ...
|
|
531 |
|
|
532 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit.
|
|
533 |
rslt notNil ifTrue:[ ^ howMany / rslt].
|
|
534 |
|
516
|
535 |
"/ here's the deep recursion ...
|
363
|
536 |
|
|
537 |
|
|
538 |
"/ try indirect conversion from source
|
|
539 |
|
|
540 |
conversions := Conversions at:sourceUnit ifAbsent:nil.
|
|
541 |
conversions notNil ifTrue:[
|
|
542 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 |
|
|
543 |
|factor2|
|
|
544 |
|
|
545 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit.
|
|
546 |
factor2 notNil ifTrue:[^ factor2 * howMany].
|
|
547 |
].
|
|
548 |
].
|
|
549 |
|
|
550 |
"/ try indirect conversion from dest
|
|
551 |
|
|
552 |
conversions := Conversions at:destUnit ifAbsent:nil.
|
|
553 |
conversions notNil ifTrue:[
|
|
554 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 |
|
|
555 |
|factor2|
|
|
556 |
|
|
557 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit.
|
|
558 |
factor2 notNil ifTrue:[^ howMany / factor2].
|
|
559 |
].
|
|
560 |
].
|
|
561 |
|
516
|
562 |
^ self noConversionFrom:sourceUnit to:destUnit.
|
363
|
563 |
|
|
564 |
"direct - how many meters are there in two inches:
|
|
565 |
UnitConverter convert:2 from:#inch to:#meter
|
515
|
566 |
UnitConverter convert:37 from:#degrees to:#fahrenheit
|
363
|
567 |
|
|
568 |
reverse - how many inches are there in one meter
|
|
569 |
UnitConverter convert:1 from:#meter to:#inch
|
|
570 |
|
|
571 |
with alias:
|
|
572 |
UnitConverter convert:1 from:#inch to:#m
|
|
573 |
UnitConverter convert:1 from:#inch to:#mm
|
|
574 |
UnitConverter convert:1 from:#inch to:#millimeter
|
|
575 |
UnitConverter convert:1 from:#inch to:#nanometer
|
|
576 |
UnitConverter convert:1 from:#mm to:#km
|
|
577 |
UnitConverter convert:1 from:#km to:#foot
|
|
578 |
|
|
579 |
indirect:
|
|
580 |
UnitConverter convert:1 from:#mm to:#twip
|
|
581 |
UnitConverter convert:1 from:#inch to:#twip
|
369
|
582 |
UnitConverter convert:1 from:'letterH' to:#point
|
|
583 |
UnitConverter convert:1 from:'letterlH' to:#point
|
364
|
584 |
|
|
585 |
UnitConverter convert:5 from:#barrel to:#liter
|
|
586 |
UnitConverter convert:10 from:#kilogram to:#carat
|
|
587 |
|
|
588 |
UnitConverter convert:1 from:#liter to:#floz
|
363
|
589 |
"
|
|
590 |
|
|
591 |
"Created: 31.5.1996 / 16:23:38 / cg"
|
516
|
592 |
"Modified: 29.3.1997 / 19:04:40 / cg"
|
363
|
593 |
!
|
|
594 |
|
|
595 |
inchToMillimeter:inches
|
364
|
596 |
"convert inches to mm. Made this an extra method, since its so common."
|
363
|
597 |
|
|
598 |
^ inches * 25.4
|
|
599 |
|
|
600 |
"Created: 31.5.1996 / 13:37:31 / cg"
|
364
|
601 |
"Modified: 31.5.1996 / 18:08:14 / cg"
|
363
|
602 |
!
|
|
603 |
|
|
604 |
millimeterToInch:mm
|
364
|
605 |
"convert mm to inches. Made this an extra method, since its so common."
|
363
|
606 |
|
|
607 |
^ mm / 25.4
|
|
608 |
|
|
609 |
"Created: 31.5.1996 / 13:37:48 / cg"
|
364
|
610 |
"Modified: 31.5.1996 / 18:08:20 / cg"
|
363
|
611 |
! !
|
|
612 |
|
516
|
613 |
!UnitConverter class methodsFor:'missing conversion'!
|
|
614 |
|
|
615 |
noConversionFrom:sourceUnit to:destUnit
|
|
616 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString.
|
|
617 |
^ nil
|
|
618 |
|
|
619 |
"Created: 29.3.1997 / 17:56:03 / cg"
|
|
620 |
! !
|
|
621 |
|
363
|
622 |
!UnitConverter class methodsFor:'private'!
|
|
623 |
|
|
624 |
addConversion:factor from:sourceMetric to:destMetric
|
|
625 |
"add a conversion"
|
|
626 |
|
|
627 |
|conversion|
|
|
628 |
|
|
629 |
conversion := Conversions at:sourceMetric ifAbsent:nil.
|
|
630 |
conversion isNil ifTrue:[
|
|
631 |
conversion := IdentityDictionary new.
|
|
632 |
Conversions at:sourceMetric put:conversion
|
|
633 |
].
|
|
634 |
conversion at:destMetric put:factor.
|
|
635 |
^ conversion
|
|
636 |
|
|
637 |
"Created: 31.5.1996 / 13:51:25 / cg"
|
515
|
638 |
"Modified: 29.3.1997 / 17:19:31 / cg"
|
363
|
639 |
!
|
|
640 |
|
|
641 |
convertDirect:howMany from:sourceMetric to:destMetric
|
|
642 |
"given a value in sourceMetric (symbolic), try to convert it
|
|
643 |
to destMetric (symbolic);
|
|
644 |
Only direct conversions are tried; return nil, if the conversion is unknown."
|
|
645 |
|
|
646 |
|conversion factor|
|
|
647 |
|
|
648 |
conversion := Conversions at:sourceMetric ifAbsent:nil.
|
|
649 |
conversion isNil ifTrue:[^ nil].
|
|
650 |
|
|
651 |
factor := conversion at:destMetric ifAbsent:nil.
|
|
652 |
factor isNil ifTrue:[^ nil].
|
|
653 |
|
364
|
654 |
factor isNumber ifTrue:[
|
|
655 |
^ howMany * factor
|
|
656 |
].
|
515
|
657 |
^ factor value:howMany
|
363
|
658 |
|
|
659 |
"
|
|
660 |
UnitConverter convertDirect:1 from:#inch to:#meter
|
|
661 |
UnitConverter convertDirect:1 from:#inch to:#millimeter
|
515
|
662 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit
|
363
|
663 |
"
|
|
664 |
|
|
665 |
"Created: 31.5.1996 / 13:54:33 / cg"
|
515
|
666 |
"Modified: 29.3.1997 / 17:13:27 / cg"
|
363
|
667 |
!
|
|
668 |
|
|
669 |
unscaled:what
|
|
670 |
"given a unit, return the base and a factor as assoc,
|
|
671 |
or nil if not found"
|
|
672 |
|
|
673 |
Scaling keysAndValuesDo:[:name :factor |
|
|
674 |
|rest|
|
|
675 |
|
|
676 |
(what startsWith:name) ifTrue:[
|
|
677 |
rest := what copyFrom:(name size+1).
|
|
678 |
rest := rest asSymbolIfInterned.
|
|
679 |
rest notNil ifTrue:[
|
|
680 |
^ rest -> factor.
|
|
681 |
]
|
|
682 |
].
|
|
683 |
].
|
|
684 |
^ nil
|
|
685 |
|
|
686 |
"
|
|
687 |
UnitConverter unscaled:#millimeter
|
|
688 |
UnitConverter unscaled:#nanometer
|
|
689 |
UnitConverter unscaled:#kilometer
|
|
690 |
UnitConverter unscaled:#fuzzymeter
|
|
691 |
"
|
|
692 |
|
|
693 |
"Modified: 31.5.1996 / 14:57:25 / cg"
|
|
694 |
! !
|
|
695 |
|
|
696 |
!UnitConverter class methodsFor:'documentation'!
|
|
697 |
|
|
698 |
version
|
516
|
699 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.9 1997-03-29 18:13:14 cg Exp $'
|
363
|
700 |
! !
|
|
701 |
UnitConverter initialize!
|