Skip to content

Commit

Permalink
167: Make UCUMFormat Jigsaw compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Jun 13, 2020
1 parent b2c9aec commit ce6cbff
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.
34 changes: 34 additions & 0 deletions console/systems/ucum-java10/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Units of Measurement Console Demos
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-385, Unit-API nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
module tech.uom.demo.systems.ucum {
requires transitive java.measure;
requires transitive si.uom.quantity;
requires transitive tech.units.indriya;
requires transitive systems.uom.quantity;
requires systems.uom.ucum;

exports tech.uom.demo.systems.ucum;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Units of Measurement Console Demos
* Copyright (c) 2005-2017, Werner Keil and others.
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
Expand All @@ -10,7 +10,7 @@
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-363, Unit-API nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* 3. Neither the name of JSR-385, Unit-API nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
Expand All @@ -25,22 +25,19 @@
*/
package tech.uom.demo.systems.ucum;

import javax.measure.Unit;
import javax.measure.format.UnitFormat;

import systems.uom.ucum.format.UCUMFormat;
import systems.uom.ucum.format.UCUMFormat.Variant;

public class ConversionError {

public static void main(String[] args) {

final UnitFormat ucumFormat = UCUMFormat.getInstance(Variant.CASE_SENSITIVE);
final UnitFormat ucumFormatPrint = UCUMFormat.getInstance(Variant.PRINT);
Unit<?> glomerular = ucumFormat.parse("mL/min/((173/100).m2)");
System.out.println(glomerular);
System.out.println(ucumFormat.format(glomerular));
System.out.println(ucumFormatPrint.format(glomerular));
final var ucumFormat = UCUMFormat.getInstance(Variant.CASE_SENSITIVE);
final var ucumFormatPrint = UCUMFormat.getInstance(Variant.PRINT);
var glomerular = ucumFormat.parse("mL/min/((173/100).m2)");
System.out.println(glomerular);
System.out.println(ucumFormat.format(glomerular));
System.out.println(ucumFormatPrint.format(glomerular));
ucumFormat.parse(ucumFormat.format(glomerular));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Units of Measurement Console Demos
* Copyright (c) 2005-2017, Werner Keil and others.
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
Expand All @@ -10,7 +10,7 @@
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-363, Unit-API nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* 3. Neither the name of JSR-385, Unit-API nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
Expand All @@ -28,14 +28,11 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.measure.Quantity;
import javax.measure.Unit;
import javax.measure.quantity.Area;
import javax.measure.quantity.Length;
import javax.measure.quantity.Volume;

import tech.units.indriya.unit.Units;
import systems.uom.ucum.UCUM;
import tech.units.indriya.unit.BaseUnit;

public class ReflectionDemo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
import static systems.uom.ucum.UCUM.*;
import static javax.measure.MetricPrefix.KILO;

import javax.measure.Quantity;
import javax.measure.Unit;
import javax.measure.format.UnitFormat;
import javax.measure.quantity.Frequency;
import javax.measure.quantity.Mass;
import javax.measure.quantity.Volume;

import systems.uom.ucum.UCUM;
import systems.uom.ucum.format.UCUMFormat;
import systems.uom.ucum.format.UCUMFormat.Variant;
Expand All @@ -47,41 +40,36 @@ public static void main(String[] args) {
var atomicMassUnit = ATOMIC_MASS_UNIT;
System.out.println(atomicMassUnit.getSymbol());

var mass = (Quantity<Mass>) Quantities.getQuantity(10, atomicMassUnit);
var mass = Quantities.getQuantity(10, atomicMassUnit);
System.out.println(mass);

Quantity<Mass> massInKg = mass.to(Units.KILOGRAM);
var massInKg = mass.to(Units.KILOGRAM);
System.out.println(massInKg);

UnitFormat cs = UCUMFormat.getInstance(Variant.CASE_SENSITIVE);
var cs = UCUMFormat.getInstance(Variant.CASE_SENSITIVE);
var unit = cs.parse("m/s");
System.out.println(unit);

// unit = format.parse("m^1*s^-1");
// System.out.println(unit);

System.out.println(UCUM.PARSEC);
UnitFormat print = UCUMFormat.getInstance(Variant.PRINT);
var print = UCUMFormat.getInstance(Variant.PRINT);
System.out.println(print.format(UCUM.PARSEC));

Unit<Frequency> hz = UCUM.HERTZ;
var hz = UCUM.HERTZ;
System.out.println(hz);
System.out.println(hz.getBaseUnits());
System.out.println(print.format(UCUM.HERTZ));

Unit<Frequency> khz = KILO(hz);
var khz = KILO(hz);
System.out.println(khz.getBaseUnits());

unit = cs.parse("Hz");
System.out.println(unit);
unit = cs.parse("kHz");
System.out.println(unit);

// UnitFormat ebnf = EBNFUnitFormat.getInstance();
// unit = ebnf.parse("MHz");
// System.out.println(unit);

Quantity<Volume> oneLiter = Quantities.getQuantity(1, LITER);
var oneLiter = Quantities.getQuantity(1, LITER);
System.out.println(oneLiter.to(LITER_DM3).getValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
/*
* Units of Measurement Console Demos
* Copyright (c) 2005-2020, Werner Keil and others.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-385, Unit-API nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.uom.demo.systems.ucum;

import javax.measure.Unit;
import javax.measure.format.UnitFormat;
import static javax.measure.spi.FormatService.FormatType.UNIT_FORMAT;

import javax.measure.spi.ServiceProvider;

public class UCUMServiceDemo {
Expand All @@ -14,27 +39,35 @@ public static void main(String[] args) {
System.out.println();
System.out.println(ServiceProvider.current());

for (String formatName : ServiceProvider.current().getUnitFormatService().getAvailableFormatNames()) {
for (String formatName : ServiceProvider.current().getFormatService().
getAvailableFormatNames(UNIT_FORMAT)) {
System.out.println(formatName);
}

UnitFormat unitFormat = ServiceProvider.current().getUnitFormatService().getUnitFormat("UCUM");
var unitFormat = ServiceProvider.current().getFormatService().getUnitFormat("UCUM");
System.out.println(unitFormat);

UnitFormat cs = ServiceProvider.current().getUnitFormatService().getUnitFormat("CS");
var cs = ServiceProvider.current().getFormatService().getUnitFormat("CS");
System.out.println(cs);
Unit<?> unit = cs.parse("m/s");
var unit = cs.parse("m/s");
System.out.println(unit);

//System.out.println("Square m (EBNF): " + ebnf.parse("m^2"));
ServiceProvider defaultProvider = ServiceProvider.current();
for (ServiceProvider provider : ServiceProvider.available()) {
if ("DefaultServiceProvider".equals(provider.getClass().getSimpleName())) {
defaultProvider = provider;
break;
}
}
final UnitFormat f = defaultProvider.getUnitFormatService().getUnitFormat();
final var f = defaultProvider.getFormatService().getUnitFormat();
System.out.println("Square m: " + f.parse("m^2"));

var providers = ServiceProvider.available();
var ucumProvider = providers.get(0);
var ucumFormatService = ucumProvider.getFormatService();
var ucumFormatter = ucumFormatService.getUnitFormat("CS");
System.out.println("m3 dimension =" + ucumFormatter.parse("m3").getDimension());
System.out.println("ft3 (wrong) dimension =" + ucumFormatter.parse("ft3").getDimension());
System.out.println("ft3 dimension =" + ucumFormatter.parse("[cft_i]").getDimension());
}
}

0 comments on commit ce6cbff

Please sign in to comment.