Skip to content

Commit

Permalink
429: Expose SymbolMap.label and alias via EBNFUnitFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Oct 2, 2024
1 parent 8cb0a72 commit 6a95709
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tech.uom.seshat.UnitServices;
import tech.uom.seshat.Units;

public class TemperatureConverterDemo {
public class TemperatureUnitConverterDemo {

public static void main(String[] args) {
var format = UnitServices.current().getFormatService().getUnitFormat();
Expand Down

This file was deleted.

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));
}
}
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);
}

}
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");
}

}

0 comments on commit 6a95709

Please sign in to comment.