This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 5aab5187bb35ecc492abd9610b75ebf9b51d0200 Author: Martin Desruisseaux <[email protected]> AuthorDate: Fri Nov 9 16:28:54 2018 +0100 Accept exponents in unit name (e.g. "metre2"). --- .../java/org/apache/sis/measure/UnitFormat.java | 21 ++++++++++++++++++++- .../java/org/apache/sis/measure/UnitFormatTest.java | 14 +++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java b/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java index c2857df..dcace05 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java +++ b/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java @@ -554,7 +554,26 @@ public class UnitFormat extends Format implements javax.measure.format.UnitForma "meters", "meter"), "metres", "metre"), DEGREES, "degree").toString(); - return map.get(uom); + /* + * Returns the unit with application of the power if it is part of the name. + * For example this method interprets "meter2" as "meter" raised to power 2. + */ + Unit<?> unit = map.get(uom); +appPow: if (unit == null) { + int s = uom.length(); + if (--s > 0 && isDigit(uom.charAt(s))) { + do if (--s < 0) break appPow; + while (isDigit(uom.charAt(s))); + if (uom.charAt(s) == '-') { + if (--s < 0) break appPow; + } + unit = map.get(uom.substring(0, ++s)); + if (unit != null) { + unit = unit.pow(Integer.parseInt(uom.substring(s))); + } + } + } + return unit; } /** diff --git a/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java b/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java index be982bf..c781c45 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java @@ -332,7 +332,7 @@ public final strictfp class UnitFormatTest extends TestCase { } /** - * Tests parsing of names. + * Tests parsing of names, for example {@code "meter"}. */ @Test public void testParseName() { @@ -397,6 +397,16 @@ public final strictfp class UnitFormatTest extends TestCase { } /** + * Tests parsing of names raised to some power, for example {@code "meter2"}. + */ + @Test + public void testParseNameRaisedToPower() { + final UnitFormat f = new UnitFormat(Locale.UK); + assertSame(Units.SQUARE_METRE, f.parse("meter2")); + assertSame(Units.HERTZ, f.parse("second-1")); + } + + /** * Tests parsing a unit defined by a URI in OGC namespace. * Example: {@code "urn:ogc:def:uom:EPSG::1026"} is for metres per second. */ @@ -485,9 +495,11 @@ public final strictfp class UnitFormatTest extends TestCase { * Tests parsing of symbols containing terms separated by spaces. * This is valid only when using {@link UnitFormat#parse(CharSequence)}. */ + @Test public void testParseTermsSeparatedBySpace() { final UnitFormat f = new UnitFormat(Locale.UK); assertSame(Units.METRES_PER_SECOND, f.parse("m s**-1")); + assertEqualsIgnoreSymbol(Units.KILOGRAM.divide(Units.SQUARE_METRE), f.parse("kg m**-2")); try { f.parse("degree minute"); fail("Should not accept unknown sentence even if each individual word is known.");
