-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Point discovery to correctly map
units
facets
- Loading branch information
Showing
4 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// Copyright (c) 2021, Novant LLC | ||
// Licensed under the MIT License | ||
// | ||
// History: | ||
// 21 Jul 2021 Andy Frank Creation | ||
// | ||
|
||
package io.novant.util; | ||
|
||
import java.util.*; | ||
import javax.baja.units.*; | ||
|
||
/** | ||
* NovantUnits provides unit conversion to BUnit. | ||
*/ | ||
public final class NovantUnits | ||
{ | ||
/** Lookup a BUnit by symbol, or create a new BUnit if not found. */ | ||
public static BUnit get(String symbol) | ||
{ | ||
if (map == null) loadMap(); | ||
|
||
BUnit unit = (BUnit)map.get(symbol); | ||
if (unit == null) | ||
{ | ||
unit = BUnit.make(symbol, symbol, BDimension.DEFAULT); | ||
map.put(symbol, unit); | ||
} | ||
|
||
return unit; | ||
} | ||
|
||
/** Load reverse lookup map via UnitDatabase. */ | ||
private static void loadMap() | ||
{ | ||
map = new HashMap(); | ||
UnitDatabase.Quantity[] quants = UnitDatabase.getDefault().getQuantities(); | ||
for (int i=0; i<quants.length; i++) | ||
{ | ||
UnitDatabase.Quantity q = quants[i]; | ||
BUnit[] units = q.getUnits(); | ||
for (int j=0; j<units.length; j++) | ||
{ | ||
BUnit u = units[j]; | ||
map.put(u.getSymbol(), u); | ||
} | ||
} | ||
} | ||
|
||
private static HashMap map; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright (c) 2021, Novant LLC | ||
// Licensed under the MIT License | ||
// | ||
// History: | ||
// 22 Jul 2021 Andy Frank Creation | ||
// | ||
|
||
package test.io.novant; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
import javax.baja.nre.annotations.*; | ||
import javax.baja.sys.*; | ||
import javax.baja.test.BTestNg; | ||
import javax.baja.units.*; | ||
|
||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.AfterTest; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import io.novant.util.*; | ||
|
||
/** BUnitsTest */ | ||
@NiagaraType | ||
public class BUnitsTest extends BTestNg | ||
{ | ||
|
||
/*+ ------------ BEGIN BAJA AUTO GENERATED CODE ------------ +*/ | ||
|
||
//////////////////////////////////////////////////////////////// | ||
// Type | ||
//////////////////////////////////////////////////////////////// | ||
|
||
@Override | ||
public Type getType() { return TYPE; } | ||
public static final Type TYPE = Sys.loadType(BUnitsTest.class); | ||
|
||
/*+ ------------ END BAJA AUTO GENERATED CODE -------------- +*/ | ||
|
||
@BeforeMethod public void beforeMethod() {} | ||
@AfterMethod public void afterMethod() {} | ||
|
||
@Test public void test() throws IOException | ||
{ | ||
// test existing | ||
verifyEq(NovantUnits.get("kW"), BUnit.getUnit("kilowatt")); | ||
verifyEq(NovantUnits.get("Pa"), BUnit.getUnit("pascal")); | ||
verifyEq(NovantUnits.get("N-s"), BUnit.getUnit("newton second")); | ||
|
||
// test make new | ||
verifyEq(NovantUnits.get("foo"), BUnit.make("foo", "foo", BDimension.DEFAULT)); | ||
verifyEq(NovantUnits.get("bar"), BUnit.make("bar", "bar", BDimension.DEFAULT)); | ||
} | ||
} |