Skip to content

Commit

Permalink
Allow numeric factors for conversion #19
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Jun 20, 2022
1 parent 8e54346 commit 32be445
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
24 changes: 17 additions & 7 deletions src/main/java/org/icatproject/utils/IcatUnits.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
public class IcatUnits {

/**
* Holds the SI units and value for a quantity. If the units provided at construction could not be parsed, then these will be null.
* Holds the SI units and value for a quantity. If the units provided at
* construction could not be parsed, then these will be null.
*/
public class SystemValue {
public String units = null;
Expand Down Expand Up @@ -53,24 +54,33 @@ public IcatUnits() {

/**
* In addition to the standard names and prefixes, allows aliasing other terms
* for a unit. Note that Unit should be refered to by the symbol specified in
* for a unit. Note that Unit should be referred to by the symbol specified in
* the
* <a href=
* "https://javadoc.io/doc/tech.units/indriya/latest/tech/units/indriya/unit/Units.html">Indriya
* documentation</a>, but the aliases can be any string. However, OoM prefixes
* will not be applied to aliases. For example, "mK" would be understood as
* 0.001K, but if "Kelvin" is aliased than "mKelvin" will not be understood.
*
* @param aliasOptions String with the format "<symbolA>: <aliasA1> <aliasA2>,
* <symbolB>: <aliasB2> ..."
* If needed, a conversion factor can be provided alongside an alias, for
* example to alias "eV" as a unit of energy, a factor of 1.602176634e-19 should
* be applied to convert to the SI unit J.
*
* @param aliasOptions String with the format
* <code>symbolA: aliasA1, aliasA2 factorA2; symbolB: aliasB1 ...</code>
*/
public IcatUnits(String aliasOptions) {
if (!aliasOptions.equals("")) {
for (String unitAliases : aliasOptions.split(",")) {
for (String unitAliases : aliasOptions.split(";")) {
String[] splitUnitAliases = unitAliases.split(":");
Unit<?> unit = unitFormat.parse(splitUnitAliases[0].trim());
for (String alias : splitUnitAliases[1].trim().split("\\s+")) {
unitFormat.alias(unit, alias);
for (String alias : splitUnitAliases[1].trim().split(",")) {
String[] aliasSplit = alias.trim().split("\\s+");
if (aliasSplit.length == 2) {
unitFormat.alias(unit.multiply(new Double(aliasSplit[1])), aliasSplit[0]);
} else {
unitFormat.alias(unit, aliasSplit[0]);
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/org/icatproject/utils/TestIcatUnits.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ public void testAliasing() {
assertEquals(null, systemValue.units);
systemValue = icatUnits.new SystemValue(null, "kelvin");
assertEquals(null, systemValue.units);
systemValue = icatUnits.new SystemValue(null, "eV");
assertEquals(null, systemValue.units);

icatUnits = new IcatUnits("\u2103: celsius degC, K: kelvin");
icatUnits = new IcatUnits("\u2103: celsius, degC; K: kelvin; J: eV 1.602176634e-19");
systemValue = icatUnits.new SystemValue(null, "celsius");
assertEquals("Kelvin", systemValue.units);
systemValue = icatUnits.new SystemValue(null, "degC");
assertEquals("Kelvin", systemValue.units);
systemValue = icatUnits.new SystemValue(null, "kelvin");
assertEquals("Kelvin", systemValue.units);
systemValue = icatUnits.new SystemValue(1., "eV");
assertEquals("Joule", systemValue.units);
assertEquals(new Double(1.602176634e-19), systemValue.value);
}
}

0 comments on commit 32be445

Please sign in to comment.