Hi,
It's possible I'm way too late with this suggestion (and it may have
been beaten to death in discussions I didn't see), but I think it could
be very useful to have more introspective access into a UnitConverter.
An example use would be: on a full environment with Java and a
comprehensive unit database, select any two obscure units and
construct a UnitConverter between them, then let some application-
supplied code-generator visitor be sent down the resulting UnitConverter
and its children, emitting custom code to perform that conversion on
some much more constrained device.
Doing this nicely would probably entail some attempt to enumerate what
basic kinds of UnitConverter may exist. My guess is there probably are
not very many. Two kinds are already treated specially with the
isIdentity() and isLinear() methods, but I could imagine a structure like:
interface UnitConverter {
interface Polynomial extends UnitConverter { getCoefficients(); }
interface Linear extends Polynomial { getSlope(); getIntercept(); }
interface Log extends UnitConverter { getBase(); }
interface Exp extends UnitConverter { getBase(); }
interface Rational extends UnitConverter {
UnitConverter getDividend(); UnitConverter getDivisor();
}
}
Am I crazy in thinking a small set like that would cover a large
fraction of UnitConverters in practice? Weird things like currencies
might not fit the scheme, so they might not implement any of the more
specific subinterfaces, and not be easily rendered into code for another
environment.
With the current API, if you get a converter and its isLinear() returns
true, you could pump two values through it and determine what it is, but
that's a little hackish and could get you an approximate result.
Another subinterface that might be of interest could be:
interface Inexact extends UnitConverter {
getRelativeError();
getDomainMin();
getDomainMax();
}
... for a converter that is known not to be using an exact conversion, but
has a known relative error for inputs in a certain range. So a converter
from km to [mi_i] could be linear with slope 15625/25146 exactly, or the
often-used approximate slope 5/8 with getRelativeError() returning 6e-3...
Chapman Flack