https://customsolvers.com/unit_parser_java/ (ES: https://customsolvers.com/unit_parser_java_es/)
The main class is called UnitP (UnitParser package). It can be instantiated in many different ways.
//1 N.
UnitP unitP = new UnitP("1 N");
//1 N.
unitP = new UnitP(1.0, UnitSymbols.Newton);
//1 N.
unitP = new UnitP(1.0, "nEwTon");
//1 N.
unitP = new UnitP(1.0, Units.Newton);UnitP can be seen as an abstract concept including many specific types (full list). Same-type variables can be added/subtracted. Different-type variables can be multiplied/divided, but only in case of generating a valid-type output.
//2 N.
unitP = UnitP.Addition(new UnitP("1 N"), new UnitP(1.0, Units.Newton));
//1 J.
unitP = UnitP.Multiplication(new UnitP("1 N"), new UnitP("1 m"));
//Error not triggering an exception.
//The output unit N*m^2 doesn't match any supported type.
unitP = UnitP.Multiplication
(
UnitP.Multiplication
(
new UnitP("1 N"), new UnitP("1 m")
),
new UnitP("1 m")
); UnitP variables are defined according to various final fields populated at instantiation.
Unit - Corresponding Units member.
UnitType - Corresponding UnitTypes member.
UnitSystem - Corresponding UnitSystems member.
UnitParts - Defining parts of the given unit.
UnitPrefix - Supported prefix affecting all the unit parts.
BaseTenExponent - Base-ten exponent used when dealing with too small/big values.
Error - Variable storing all the error- and exception-related information.
All the functionalities are based upon the following ideas:
- In case of incompatibility, the first element is always preferred.
- By default, the formally-correct alternative is preferred. Some required modifications might be performed.
- By default, all the errors are managed internally.
//1.3048.
unitP = UnitP.Addition(new UnitP("1 m"), new UnitP("1 ft"));
//Error not triggering an exception.
//The parser expects "km" or a full-name-based version like "KiLom".
unitP = new UnitP("1 Km");
//999999.9999999001*10^19 YSt.
unitP = UnitP.Multiplication
(
999999999999999999999999999999999999.9,
new UnitP("9999999999999 St")
); The unit string parsing part is quite flexible, but there are some basic rules.
- String multi-part units are expected to be exclusively formed by units, multiplication/division signs and integer exponents.
- Only one division sign is expected. The parser understands that all what lies before/after it is the numerator/denominator.
//1 W.
unitP = new UnitP("1 J*J/s*J2*J-1*s*s-1");
//Error not triggering an exception.
//The parser understands "J*J/(s*J2*s*J*s)", what doesn't represent a supported type.
unitP = new UnitP("1 J*J/(s*J2*s)*J*s");Formally, only the double type is supported. Practically, UnitP variables implement a mixed system delivering beyond-double-range support.
//7.891011 ft.
unitP = UnitP.Multiplication(new UnitP("1 ft"), 7.891011);
//1.213141516 Gs.
unitP = UnitP.Multiplication(new UnitP("1 s"), 1213141516.0);
//0.0003094346047382587*10^-752 ym.
unitP = UnitP.Division
(
UnitP.Division
(
UnitP.Division
(
UnitP.Multiplication
(
0.0000000000000000000000000000000000000000000000001,
new UnitP(0.000000000000000000001, "ym2")
),
new UnitP("999999999999999999999 Ym")
),
Double.MAX_VALUE
),
Double.MAX_VALUE
);The test application includes a relevant number of descriptive code samples.
I, Alvaro Carballo Garcia (varocarbas), am the sole author of each single bit of this code.
Equivalently to what happens with all my other online contributions, this code can be considered public domain. For more information about my copyright/authorship attribution ideas, visit https://customsolvers.com/copyright/.