How do you write your own custom converters and specify the conversion factor using javax.measure packages. (e.g. Pint to gallons)
Asked
Active
Viewed 712 times
1 Answers
1
Well, I don't know the "correct" way to do it, but the obvious way would be to follow the pattern of the NonSI
class; i.e. create your own class containing statics for each unit, with values based on the standard definitions for the non-SI units in terms of SI units.

Hendy Irawan
- 20,498
- 11
- 103
- 114

Stephen C
- 698,415
- 94
- 811
- 1,216
-
But NonSI classes uses the following pattern to add new units public static final Unit
YARD = nonSI(FOOT.times(3)); where nonSI is private method. Can you provide an example how to actually use it? I'm mostly interested if you can add a new unit and define it's symbol. – FrEaKmAn Nov 18 '14 at 08:22 -
@FrEaKmAn - Obviously, you cannot dynamically define symbols for `static final` constants ... or any other Java symbols for that matter! Or at least, not in a way that gives you any benefit. Java is fundamentally a static language. Source code symbols are resolved statically. If you need dynamic "symbols", use a `Map`. – Stephen C Nov 18 '14 at 10:49
-
Thank you for Java lecture. Still this doesn't answer the question, where you mention that solution is obvious, but yet you are not able to add an example. – FrEaKmAn Nov 18 '14 at 11:36
-
I am "able" to add examples, but I don't think it is necessary. As for adding new symbols dynamically, that is hard - you need to use bytecode engineering or Java source code generation. And it is ultimately pointless, IMO. See explanation in comment above. – Stephen C Nov 18 '14 at 21:45