-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
429: Expose SymbolMap.label and alias via EBNFUnitFormat
Task-Url: unitsofmeasurement/indriya#429
- Loading branch information
Showing
5 changed files
with
104 additions
and
37 deletions.
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
36 changes: 0 additions & 36 deletions
36
console/java17/src/main/java/tech/uom/demo/java17/TemperatureConverterDemo.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
...le/java17/src/main/java/tech/uom/demo/java17/format/TemperatureQuantityConverterDemo.java
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,28 @@ | ||
package tech.uom.demo.java17.format; | ||
|
||
import static javax.measure.Quantity.Scale.ABSOLUTE; | ||
import static tech.units.indriya.unit.Units.CELSIUS; | ||
|
||
import javax.measure.Quantity; | ||
import javax.measure.Unit; | ||
import javax.measure.quantity.Temperature; | ||
|
||
import tech.units.indriya.quantity.Quantities; | ||
import tech.units.indriya.format.EBNFUnitFormat; | ||
import tech.units.indriya.format.SimpleUnitFormat; | ||
|
||
public class TemperatureQuantityConverterDemo { | ||
public static void main(String[] args) { | ||
//Unit<Temperature> cByTen = CELSIUS.divide(10); | ||
//var format = EBNFUnitFormat.getInstance(); | ||
var format = SimpleUnitFormat.getInstance(); | ||
Unit<Temperature> cByTen = format.parse("°C/10").asType(Temperature.class); | ||
Quantity<Temperature> absT = Quantities.getQuantity(0d, CELSIUS, ABSOLUTE); | ||
Quantity<Temperature> absT2 = Quantities.getQuantity(0d, CELSIUS, ABSOLUTE); | ||
Quantity<Temperature> absT3 = Quantities.getQuantity(0d, cByTen, ABSOLUTE); | ||
Quantity<Temperature> sum = absT.add(absT2); | ||
System.out.println(sum); | ||
System.out.println(absT3); | ||
System.out.println(sum.to(cByTen)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
console/java17/src/main/java/tech/uom/demo/java17/format/TemperatureUnitConverterDemo.java
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,49 @@ | ||
package tech.uom.demo.java17.format; | ||
|
||
import java.text.NumberFormat; | ||
|
||
import javax.measure.IncommensurableException; | ||
import javax.measure.Quantity.Scale; | ||
import javax.measure.UnconvertibleException; | ||
import javax.measure.UnitConverter; | ||
import javax.measure.spi.ServiceProvider; | ||
|
||
import tech.units.indriya.format.EBNFUnitFormat; | ||
import tech.units.indriya.format.NumberDelimiterQuantityFormat; | ||
import tech.units.indriya.quantity.Quantities; | ||
|
||
public class TemperatureUnitConverterDemo { | ||
|
||
public static void main(String[] args) { | ||
//var format = ServiceProvider.current().getFormatService().getUnitFormat(); | ||
var format = EBNFUnitFormat.getInstance(); | ||
var format2 = NumberDelimiterQuantityFormat.getInstance(NumberFormat.getInstance(), format); | ||
var source = format.parse("K/10"); | ||
var intermediary = format.parse("K"); | ||
var source2 = format.parse("°C/10"); | ||
var dest = format.parse("°C"); | ||
UnitConverter converter = null; | ||
UnitConverter converter2 = null; | ||
try { | ||
converter = source.getConverterToAny(intermediary); | ||
converter2 = dest.getConverterToAny(intermediary); | ||
} catch (UnconvertibleException | IncommensurableException e) { | ||
e.printStackTrace(); | ||
} | ||
if (converter != null) { | ||
var toK = converter2.convert(1); | ||
var converted = converter.convert(toK); | ||
System.out.println(converted); | ||
} | ||
|
||
var qs = Quantities.getQuantity(10, source, Scale.ABSOLUTE); | ||
System.out.println(format2.format(qs)); | ||
//var qs2 = Quantities.getQuantity(10, source2, Scale.ABSOLUTE); | ||
//System.out.println(format2.format(qs2)); | ||
var qs3 = Quantities.getQuantity(1, intermediary, Scale.ABSOLUTE); | ||
var qs4 = qs.multiply(qs3); | ||
System.out.println(format2.format(qs4)); | ||
//var qs5 = qs.to(dest); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
console/systems/common/src/main/java/tech/uom/demo/systems/common/QuantityFormatDemo.java
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,26 @@ | ||
package tech.uom.demo.systems.common; | ||
|
||
import javax.measure.Quantity; | ||
import javax.measure.spi.ServiceProvider; | ||
|
||
import systems.uom.common.USCustomary; | ||
import tech.units.indriya.format.EBNFUnitFormat; | ||
import tech.units.indriya.format.SimpleUnitFormat; | ||
|
||
public class QuantityFormatDemo { | ||
|
||
public static void main(String[] args) { | ||
SimpleUnitFormat.getInstance().alias(USCustomary.MILE, "mile"); | ||
EBNFUnitFormat.getInstance().label(USCustomary.MILE, "mi"); | ||
EBNFUnitFormat.getInstance().alias(USCustomary.MILE, "mile"); | ||
Quantity<?> q1 = ServiceProvider.current().getFormatService().getQuantityFormat().parse("3 km"); | ||
Quantity<?> q2 = ServiceProvider.current().getFormatService().getQuantityFormat().parse("3 mi"); | ||
Quantity<?> q3 = ServiceProvider.current().getFormatService().getQuantityFormat().parse("3 mile"); | ||
//Quantity<?> q4 = ServiceProvider.current().getFormatService().getQuantityFormat().parse("3 2/km"); // Exception | ||
Quantity<?> q5 = ServiceProvider.current().getFormatService().getQuantityFormat("EBNF").parse("3 1/km"); | ||
Quantity<?> q6 = ServiceProvider.current().getFormatService().getQuantityFormat("EBNF").parse("3 1/mi"); // Exception | ||
Quantity<?> q7 = ServiceProvider.current().getFormatService().getQuantityFormat("EBNF").parse("3 1/mile"); // Exception | ||
Quantity<?> q8 = ServiceProvider.current().getFormatService().getQuantityFormat("EBNF").parse("3 mile"); | ||
} | ||
|
||
} |