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
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new 481146f801 Make `QuantityFormat` more compliant with specification.
481146f801 is described below
commit 481146f80157b2e0bfa20a09d7d914d8c3b137dc
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Tue Jul 19 18:11:35 2022 +0200
Make `QuantityFormat` more compliant with specification.
---
.../java/org/apache/sis/measure/QuantityFormat.java | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git
a/core/sis-utility/src/main/java/org/apache/sis/measure/QuantityFormat.java
b/core/sis-utility/src/main/java/org/apache/sis/measure/QuantityFormat.java
index b727122486..847156378e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/QuantityFormat.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/QuantityFormat.java
@@ -23,9 +23,14 @@ import java.text.NumberFormat;
import java.text.ParsePosition;
import javax.measure.Quantity;
import javax.measure.Unit;
+import javax.measure.format.ParserException;
+import org.apache.sis.internal.system.Loggers;
+import org.apache.sis.util.logging.Logging;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.internal.util.FinalFieldSetter;
+import static java.util.logging.Logger.getLogger;
+
/**
* Parses and formats numbers with units of measurement.
@@ -87,7 +92,7 @@ public class QuantityFormat extends Format {
/**
* Formats the specified quantity in the given buffer.
- * The given object shall be an {@link Quantity} instance.
+ * The given object shall be a {@link Quantity} instance.
*
* @param quantity the quantity to format.
* @param toAppendTo where to format the quantity.
@@ -108,16 +113,22 @@ public class QuantityFormat extends Format {
*
* @param source the text, part of which should be parsed.
* @param pos index and error index information.
- * @return a unit parsed from the string, or {@code null} in case of error.
+ * @return a quantity parsed from the string, or {@code null} in case of
error.
*/
@Override
public Object parseObject(final String source, final ParsePosition pos) {
+ final int start = pos.getIndex();
final Number value = numberFormat.parse(source, pos);
if (value != null) {
- final Unit<?> unit = unitFormat.parse(source, pos);
- if (unit != null) {
- return Quantities.create(value.doubleValue(), unit);
+ try {
+ final Unit<?> unit = unitFormat.parse(source, pos);
+ if (unit != null) {
+ return Quantities.create(value.doubleValue(), unit);
+ }
+ } catch (ParserException e) {
+ Logging.ignorableException(getLogger(Loggers.MEASURE),
QuantityFormat.class, "parseObject", e);
}
+ pos.setIndex(start); // By `Format.parseObject(…)` method
contract.
}
return null;
}