Object subclass:#UnitConverter
instanceVariableNames:''
classVariableNames:'Conversions Scaling Aliases'
poolDictionaries:''
category:'Magnitude-General'
!
!UnitConverter class methodsFor:'documentation'!
documentation
"
In order to collect various unit conversions into one central
manageable (and especially: browsable) place, all previously
spread knowledge about metric (and other) conversions have been
brought into this class.
This class is purely functional, abstract and has no instances;
all queries are via class protocol.
Choosing the Magnitudes category as its home is arbitrary.
[author:]
Claus Gittinger
see also:
examples
/usr/share/lib/unittab (if present on your system)
"
!
examples
"
ever wanted to know, how many floz's are there in a european
Coce bottle ?
[exBegin]
Transcript showCR:
(UnitConverter convert:1 from:#liter to:#floz)
[exEnd]
or, how many square-meters an acre is:
[exBegin]
Transcript showCR:
(UnitConverter convert:1 from:#acre to:#meter2)
[exEnd]
real estate buyers might want to know, how many acres
a german ar is:
[exBegin]
Transcript showCR:
(UnitConverter convert:1 from:#'german-ar' to:#acre)
[exEnd]
"
!
history
"Created: 31.5.1996 / 13:34:29 / cg"
"Modified: 31.5.1996 / 14:54:34 / cg"
! !
!UnitConverter class methodsFor:'initialization'!
initialize
"initialize common conversions"
Scaling := IdentityDictionary new.
Conversions := IdentityDictionary new.
Aliases := IdentityDictionary new.
Scaling at:#tera put:1000000000000.
Scaling at:#giga put:1000000000.
Scaling at:#mega put:1000000.
Scaling at:#kilo put:1000.
Scaling at:#deci put:(1/10).
Scaling at:#centi put:(1/100).
Scaling at:#milli put:(1/1000).
Scaling at:#micro put:(1/1000000).
Scaling at:#nano put:(1/1000000000).
Scaling at:#pico put:(1/1000000000000).
"/ -------------- length -------------
Aliases at:#km put:#kilometer.
Aliases at:#m put:#meter.
Aliases at:#dm put:#decimeter.
Aliases at:#cm put:#centimeter.
Aliases at:#mm put:#millimeter.
Aliases at:#micron put:#micrometer.
Aliases at:#nm put:#nanometer.
Aliases at:#angstrom put:#decinanometer.
"/ US
self addConversion:12 from:#foot to:#inch.
self addConversion:3 from:#yard to:#foot.
self addConversion:5280 from:#mile to:#foot.
Aliases at:#feet put:#foot.
Aliases at:#ft put:#foot.
Aliases at:#yd put:#yard.
Aliases at:#mi put:#mile.
"/ inch to millimeter
self addConversion:(25.4/1000) from:#inch to:#meter.
"/ inch to (roughly) a typesetter point
self addConversion:(1/72) from:#point to:#inch.
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point
self addConversion:(1/20) from:#twip to:#point.
"/ nautic
self addConversion:1852 from:#'nautical-mile' to:#meter.
"/ ---------------- area -------------------
"/ US
self addConversion:4840 from:#acre to:#yard2.
self addConversion:100 from:#are to:#meter2.
self addConversion:100 from:#hectare to:#are.
"/ german area - add your countries, and return to me ...
Aliases at:#'german-ar' put:#are.
Aliases at:#'german-hektar' put:#hectare.
"/ ---------------- liquid ----------------
self addConversion:231 from:#gallon to:#inch3.
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here,
"/ thats also 1/4th of a good wine ;-)
self addConversion:(1/2) from:#pint to:#quart.
self addConversion:(1/16) from:#floz to:#pint.
self addConversion:(1/8) from:#fldr to:#floz.
self addConversion:42 from:#barrel to:#gallon.
Aliases at:#cc put:#cm3.
Aliases at:#liter put:#kilocc.
Aliases at:#ml put:#milliliter.
Aliases at:#gal put:#gallon.
Aliases at:#qt put:#quart.
"/ ---------------- mass ----------------
self addConversion:453.59237 from:#lb to:#gram.
self addConversion:205 from:#carat to:#milligram.
Aliases at:#oz put:#ounce.
Aliases at:#gm put:#gram.
Aliases at:#kg put:#kilogram.
"/ well, a pint of beer is not always:
self addConversion:277.420 from:#'british-gallon' to:#in3.
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'.
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'.
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'.
"/ ---------------- energy ----------------
Aliases at:#cal put:#calorie.
Aliases at:#nt put:#newtom.
self addConversion:(1 / 4.1868) from:#calorie to:#joule.
"/ ---------------- cooking ----------------
self addConversion:4 from:#tablespoon to:#fldr.
self addConversion:(4/3) from:#teaspoon to:#fldr.
"
UnitConverter initialize
"
"Modified: 31.5.1996 / 17:08:43 / cg"
! !
!UnitConverter class methodsFor:'conversions'!
convert:howMany from:sourceUnit to:destUnit
"given a value in sourceUnit (symbolic), try to convert it
to destUnit (symbolic); return nil, if the conversion is
unknown."
|u factor conversions alias rslt sU dU|
"/ somehow, recursion must end ...
sourceUnit == destUnit ifTrue:[
^ howMany
].
"/ first, get rid of scalers ...
u := self unscaled:sourceUnit.
u notNil ifTrue:[
^ self convert:(howMany*(u value)) from:(u key) to:destUnit
].
u := self unscaled:destUnit.
u notNil ifTrue:[
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key)
].
"/ and of aliases ...
alias := Aliases at:sourceUnit ifAbsent:nil.
alias notNil ifTrue:[
^ self convert:howMany from:alias to:destUnit
].
alias := Aliases at:destUnit ifAbsent:nil.
alias notNil ifTrue:[
^ self convert:howMany from:sourceUnit to:alias
].
"/ working with squares or cubics ?
((sourceUnit endsWith:$2) and:[destUnit endsWith:$2]) ifTrue:[
sU := (sourceUnit copyWithoutLast:1) asSymbolIfInterned.
dU := (destUnit copyWithoutLast:1) asSymbolIfInterned.
(sU notNil and:[dU notNil]) ifTrue:[
^ (self convert:(howMany sqrt) from:sU to:dU) squared
].
^ nil.
].
((sourceUnit endsWith:$3) and:[destUnit endsWith:$3]) ifTrue:[
sU := (sourceUnit copyWithoutLast:1) asSymbolIfInterned.
dU := (destUnit copyWithoutLast:1) asSymbolIfInterned.
(sU notNil and:[dU notNil]) ifTrue:[
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3
].
^ nil.
].
"/ the real work comes here ...
"/ is there a direct conversion in the dataBase ?
rslt := self convertDirect:howMany from:sourceUnit to:destUnit.
rslt notNil ifTrue:[^ rslt].
"/ try inverse conversion ...
rslt := self convertDirect:1 from:destUnit to:sourceUnit.
rslt notNil ifTrue:[ ^ howMany / rslt].
"/ heres the deep recursion ...
"/ try indirect conversion from source
conversions := Conversions at:sourceUnit ifAbsent:nil.
conversions notNil ifTrue:[
conversions keysAndValuesDo:[:intermediateUnit :factor1 |
|factor2|
factor2 := self convert:factor1 from:intermediateUnit to:destUnit.
factor2 notNil ifTrue:[^ factor2 * howMany].
].
].
"/ try indirect conversion from dest
conversions := Conversions at:destUnit ifAbsent:nil.
conversions notNil ifTrue:[
conversions keysAndValuesDo:[:intermediateUnit :factor1 |
|factor2|
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit.
factor2 notNil ifTrue:[^ howMany / factor2].
].
].
^ nil
"direct - how many meters are there in two inches:
UnitConverter convert:2 from:#inch to:#meter
reverse - how many inches are there in one meter
UnitConverter convert:1 from:#meter to:#inch
with alias:
UnitConverter convert:1 from:#inch to:#m
UnitConverter convert:1 from:#inch to:#mm
UnitConverter convert:1 from:#inch to:#millimeter
UnitConverter convert:1 from:#inch to:#nanometer
UnitConverter convert:1 from:#mm to:#km
UnitConverter convert:1 from:#km to:#foot
indirect:
UnitConverter convert:1 from:#mm to:#twip
UnitConverter convert:1 from:#inch to:#twip
"
"Created: 31.5.1996 / 16:23:38 / cg"
"Modified: 31.5.1996 / 17:19:39 / cg"
!
inchToMillimeter:inches
"convert inches to mm"
^ inches * 25.4
"Created: 31.5.1996 / 13:37:31 / cg"
!
inchToTwip:inches
"convert inches to twips"
^ inches * 1440
"Created: 31.5.1996 / 13:37:41 / cg"
!
millimeterToInch:mm
"convert mm to inches"
^ mm / 25.4
"Created: 31.5.1996 / 13:37:48 / cg"
!
pointToTwip:points
"convert (typesetting-) points to twips"
^ points * 20
"Created: 31.5.1996 / 13:38:03 / cg"
!
twipToInch:twips
"convert twips to inches"
^ twips / 1440.0
"Created: 31.5.1996 / 13:38:10 / cg"
!
twipToPoint:twips
"convert twips to points"
^ twips / 20.0
"Created: 31.5.1996 / 13:38:18 / cg"
! !
!UnitConverter class methodsFor:'private'!
addConversion:factor from:sourceMetric to:destMetric
"add a conversion"
|conversion|
conversion := Conversions at:sourceMetric ifAbsent:nil.
conversion isNil ifTrue:[
conversion := IdentityDictionary new.
Conversions at:sourceMetric put:conversion
].
conversion at:destMetric put:factor.
^ conversion
"Created: 31.5.1996 / 13:51:25 / cg"
!
convertDirect:howMany from:sourceMetric to:destMetric
"given a value in sourceMetric (symbolic), try to convert it
to destMetric (symbolic);
Only direct conversions are tried; return nil, if the conversion is unknown."
|conversion factor|
conversion := Conversions at:sourceMetric ifAbsent:nil.
conversion isNil ifTrue:[^ nil].
factor := conversion at:destMetric ifAbsent:nil.
factor isNil ifTrue:[^ nil].
^ howMany * factor
"
UnitConverter convertDirect:1 from:#inch to:#meter
UnitConverter convertDirect:1 from:#inch to:#millimeter
"
"Created: 31.5.1996 / 13:54:33 / cg"
"Modified: 31.5.1996 / 14:58:37 / cg"
!
unscaled:what
"given a unit, return the base and a factor as assoc,
or nil if not found"
Scaling keysAndValuesDo:[:name :factor |
|rest|
(what startsWith:name) ifTrue:[
rest := what copyFrom:(name size+1).
rest := rest asSymbolIfInterned.
rest notNil ifTrue:[
^ rest -> factor.
]
].
].
^ nil
"
UnitConverter unscaled:#millimeter
UnitConverter unscaled:#nanometer
UnitConverter unscaled:#kilometer
UnitConverter unscaled:#fuzzymeter
"
"Modified: 31.5.1996 / 14:57:25 / cg"
! !
!UnitConverter class methodsFor:'documentation'!
version
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.1 1996-05-31 15:22:12 cg Exp $'
! !
UnitConverter initialize!