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 2017/07/12 14:24:43 UTC
svn commit: r1801738 - in /sis/branches/JDK8/core:
sis-feature/src/main/java/org/apache/sis/feature/
sis-feature/src/test/java/org/apache/sis/feature/
sis-utility/src/main/java/org/apache/sis/math/
sis-utility/src/test/java/org/apache/sis/math/
Author: desruisseaux
Date: Wed Jul 12 14:24:43 2017
New Revision: 1801738
URL: http://svn.apache.org/viewvc?rev=1801738&view=rev
Log:
More informative formatting of NaN values in features.
Modified:
sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java?rev=1801738&r1=1801737&r2=1801738&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java [UTF-8] Wed Jul 12 14:24:43 2017
@@ -43,6 +43,7 @@ import org.apache.sis.util.resources.Err
import org.apache.sis.util.resources.Vocabulary;
import org.apache.sis.internal.util.CollectionsExt;
import org.apache.sis.referencing.IdentifiedObjects;
+import org.apache.sis.math.MathFunctions;
// Branch-dependent imports
import java.io.UncheckedIOException;
@@ -508,8 +509,32 @@ public class FeatureFormat extends Tabul
if (value == null) continue;
}
} else if (format != null && valueClass.isInstance(value)) { // Null safe because of getFormat(valueClass) contract.
- value = format.format(value, buffer, dummyFP);
+ /*
+ * Convert numbers, dates, angles, etc. to character sequences before to append them in the table.
+ * Note that DecimalFormat writes Not-a-Number as "NaN" in some locales and as "�" in other locales
+ * (U+FFFD - Unicode replacement character). The "�" seems to be used mostly for historical reasons;
+ * as of 2017 the Unicode Common Locale Data Repository (CLDR) seems to define "NaN" for all locales.
+ * We could configure DecimalFormatSymbols for using "NaN", but (for now) we rather substitute "�" by
+ * "NaN" here for avoiding to change the DecimalFormat configuration and for distinguishing the NaNs.
+ */
+ final StringBuffer t = format.format(value, buffer, dummyFP);
+ if (value instanceof Number) {
+ final float f = ((Number) value).floatValue();
+ if (Float.isNaN(f)) {
+ if ("�".contentEquals(t)) {
+ t.setLength(0);
+ t.append("NaN");
+ }
+ final int n = MathFunctions.toNanOrdinal(f);
+ if (n > 0) buffer.append(" #").append(n);
+ }
+ }
+ value = t;
}
+ /*
+ * All values: the numbers, dates, angles, etc. formatted above, any other character sequences
+ * (e.g. InternationalString), or other kind of values - some of them handled in a special way.
+ */
length = formatValue(value, table.append(separator), length);
buffer.setLength(0);
if (length < 0) break; // Value is too long, abandon remaining iterations.
Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java?rev=1801738&r1=1801737&r2=1801738&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java [UTF-8] Wed Jul 12 14:24:43 2017
@@ -152,6 +152,7 @@ public final strictfp class FeatureForma
feature.setPropertyValue("city", "Tokyo");
feature.setPropertyValue("population", 13185502); // In 2011.
feature.setPropertyValue("universities", Arrays.asList("Waseda", "Keio"));
+ feature.setPropertyValue("temperature", Float.NaN);
final FeatureFormat format = create();
final String text = format.format(feature);
@@ -164,7 +165,7 @@ public final strictfp class FeatureForma
"│ region │ InternationalString │ 0 ∉ [1 … 1] │ │ │\n" +
"│ isGlobal │ Boolean │ 0 ∉ [1 … 1] │ │ │\n" +
"│ universities │ String │ 2 ∈ [0 … ∞] │ Waseda, Keio │ │\n" +
- "│ temperature │ Float │ 0 ∉ [1 … 1] │ │ accuracy, units │\n" +
+ "│ temperature │ Float │ 1 ∈ [1 … 1] │ NaN │ accuracy, units │\n" +
"└──────────────┴─────────────────────┴─────────────┴──────────────┴─────────────────┘\n", text);
}
Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java?rev=1801738&r1=1801737&r2=1801738&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java [UTF-8] Wed Jul 12 14:24:43 2017
@@ -559,8 +559,9 @@ public final class MathFunctions extends
* Some of those bits, named the <cite>payload</cite>, can be used for storing custom information.
* This method maps some of the payload values to each ordinal value.
*
- * <p>The relationship between payload values and ordinal values is implementation dependent and
- * may change in any future version of the SIS library. The current implementation restricts the
+ * <p>This method guarantees that {@code toNanFloat(0)} returns the standard {@link Float#NaN} value.
+ * For all other {@code ordinal} values, the relationship to the payload values is implementation dependent
+ * and may change in any future version of the SIS library. The current implementation restricts the
* range of allowed ordinal values to a smaller one than the range of all possible values.</p>
*
* @param ordinal the NaN ordinal value, from {@code -0x200000} to {@code 0x1FFFFF} inclusive.
@@ -580,6 +581,10 @@ public final class MathFunctions extends
* Returns the ordinal value of the given NaN number.
* This method is the converse of {@link #toNanFloat(int)}.
*
+ * <p>If the given float is the standard {@link Float#NaN} value, then this method returns 0.
+ * For all other values, the relationship between the float payload and the returned ordinal
+ * is implementation dependent and may change in any future Apache SIS version.</p>
+ *
* @param value the value from which to get the NaN ordinal value.
* @return the NaN ordinal value of the given floating point value.
* @throws IllegalArgumentException if the given value is not a NaN value,
Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java?rev=1801738&r1=1801737&r2=1801738&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java [UTF-8] Wed Jul 12 14:24:43 2017
@@ -33,7 +33,7 @@ import static org.apache.sis.internal.ut
*
* @author Martin Desruisseaux (IRD, Geomatys)
* @author Johann Sorel (Geomatys)
- * @version 0.7
+ * @version 0.8
* @since 0.3
* @module
*/
@@ -243,6 +243,7 @@ public final strictfp class MathFunction
* Tests the {@link MathFunctions#toNanFloat(int)} method. This will indirectly test the
* converse {@link MathFunctions#toNanOrdinal(float)} method through Java assertions.
*/
+ @Test
public void testToNanFloat() {
final int standardNaN = Float.floatToRawIntBits(Float.NaN);
for (int ordinal = 0; ordinal < MathFunctions.MAX_NAN_ORDINAL; ordinal += 256) {
@@ -257,6 +258,15 @@ public final strictfp class MathFunction
}
/**
+ * Tests a part of the {@link MathFunctions#toNanOrdinal(float)} method contract.
+ * More extensive tests is performed indirectly by the {@link #testToNanFloat()} method.
+ */
+ @Test
+ public void testToNanOrdinal() {
+ assertEquals(0, toNanOrdinal(Float.NaN));
+ }
+
+ /**
* Tests the {@link MathFunctions#quadrupleToDouble(long, long)} method. Values used in this test are taken from
* <a href="https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format">Quadruple-precision
* floating-point format</a> on Wikipedia.