You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2015/07/28 11:14:18 UTC

svn commit: r1693044 - in /sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt: AbstractParser.java Formatter.java Symbols.java

Author: desruisseaux
Date: Tue Jul 28 09:14:18 2015
New Revision: 1693044

URL: http://svn.apache.org/r1693044
Log:
WKT formatting: use scientific notation for small or large number, but not at the same threshold than the one used by default in the JDK.
The reason is that ellipsoid axis lengths expressed in feet are around 2.1E+7 but are still commonly formatted in non-scientific format.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Symbols.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java?rev=1693044&r1=1693043&r2=1693044&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java [UTF-8] Tue Jul 28 09:14:18 2015
@@ -36,7 +36,6 @@ import org.opengis.util.InternationalStr
 import org.apache.sis.internal.system.Loggers;
 import org.apache.sis.internal.util.StandardDateFormat;
 import org.apache.sis.measure.Units;
-import org.apache.sis.util.Workaround;
 import org.apache.sis.util.logging.Logging;
 
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
@@ -80,16 +79,6 @@ abstract class AbstractParser implements
     static final int MANDATORY = 2;
 
     /**
-     * Set to {@code true} if parsing of number in scientific notation is allowed.
-     * The way to achieve that is currently a hack, because {@link NumberFormat}
-     * has no API for managing that as of JDK 1.8.
-     *
-     * @todo See if a future version of JDK allows us to get ride of this ugly hack.
-     */
-    @Workaround(library = "JDK", version = "1.8")
-    static final boolean SCIENTIFIC_NOTATION = true;
-
-    /**
      * The logger to use for reporting warnings when this parser is used through the {@link #createFromWKT(String)}.
      * This happen most often when the user invoke the {@link org.apache.sis.referencing.CRS#fromWKT(String)}
      * convenience method.
@@ -175,7 +164,7 @@ abstract class AbstractParser implements
         this.dateFormat  = dateFormat;
         this.unitFormat  = unitFormat;
         this.errorLocale = errorLocale;
-        if (SCIENTIFIC_NOTATION && numberFormat instanceof DecimalFormat) {
+        if (Symbols.SCIENTIFIC_NOTATION && numberFormat instanceof DecimalFormat) {
             final DecimalFormat decimalFormat = (DecimalFormat) ((DecimalFormat) numberFormat).clone();
             exponentSymbol = decimalFormat.getDecimalFormatSymbols().getExponentSeparator();
             String pattern = decimalFormat.toPattern();

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1693044&r1=1693043&r2=1693044&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java [UTF-8] Tue Jul 28 09:14:18 2015
@@ -1099,17 +1099,32 @@ public class Formatter implements Locali
     public void append(final double number) {
         appendSeparator();
         setColor(ElementKind.NUMBER);
+        final double abs = Math.abs(number);
         /*
-         * The 2 below is for using two less fraction digits than the expected number accuracy.
-         * The intend is to give to DecimalFormat a chance to hide rounding errors, keeping in
-         * mind that the number value is not necessarily the original one (we may have applied
-         * a unit conversion). In the case of WGS84 semi-major axis in metres, we still have a
-         * maximum of 8 fraction digits, which is more than enough.
+         * Use scientific notation if the number magnitude is too high or too low. The threshold values used here
+         * may be different than the threshold values used in the standard 'StringBuilder.append(double)' method.
+         * In particular, we use a higher threshold for large numbers because ellipsoid axis lengths are above the
+         * JDK threshold when the axis length is given in feet (about 2.1E+7) while we still want to format them
+         * as usual numbers.
+         *
+         * Note that we perform this special formatting only if the 'NumberFormat' is not localized
+         * (which is the usual case).
          */
-        numberFormat.setMaximumFractionDigits(DecimalFunctions.fractionDigitsForValue(number, 2));
-        numberFormat.setMinimumFractionDigits(1); // Must be after setMaximumFractionDigits(…).
-        numberFormat.setRoundingMode(RoundingMode.HALF_EVEN);
-        numberFormat.format(number, buffer, dummy);
+        if (Symbols.SCIENTIFIC_NOTATION && (abs < 1E-3 || abs >= 1E+9) && symbols.getLocale() == Locale.ROOT) {
+            buffer.append(number);
+        } else {
+            /*
+             * The 2 below is for using two less fraction digits than the expected number accuracy.
+             * The intend is to give to DecimalFormat a chance to hide rounding errors, keeping in
+             * mind that the number value is not necessarily the original one (we may have applied
+             * a unit conversion). In the case of WGS84 semi-major axis in metres, we still have a
+             * maximum of 8 fraction digits, which is more than enough.
+             */
+            numberFormat.setMaximumFractionDigits(DecimalFunctions.fractionDigitsForValue(number, 2));
+            numberFormat.setMinimumFractionDigits(1); // Must be after setMaximumFractionDigits(…).
+            numberFormat.setRoundingMode(RoundingMode.HALF_EVEN);
+            numberFormat.format(number, buffer, dummy);
+        }
         resetColor();
     }
 

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Symbols.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Symbols.java?rev=1693044&r1=1693043&r2=1693044&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Symbols.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Symbols.java [UTF-8] Tue Jul 28 09:14:18 2015
@@ -21,6 +21,7 @@ import java.util.Locale;
 import java.io.Serializable;
 import java.text.NumberFormat;
 import org.apache.sis.util.Localized;
+import org.apache.sis.util.Workaround;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.resources.Errors;
 
@@ -79,6 +80,16 @@ public class Symbols implements Localize
     private static final long serialVersionUID = -1730166945430878916L;
 
     /**
+     * Set to {@code true} if parsing and formatting of number in scientific notation is allowed.
+     * The way to achieve that is currently a hack, because {@link NumberFormat} has no API for
+     * managing that as of JDK 1.8.
+     *
+     * @todo See if a future version of JDK allows us to get ride of this ugly hack.
+     */
+    @Workaround(library = "JDK", version = "1.8")
+    static final boolean SCIENTIFIC_NOTATION = true;
+
+    /**
      * The prefix character for the value of a WKT fragment.
      */
     static final char FRAGMENT_VALUE = '$';