sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/04: CharSequences.parseDoubles(…) should returns an array of length 0 for empty strings.
Date Sat, 03 Nov 2018 16:42:23 GMT
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 f463a618d3b670172d095eb11e05a58aa381f8c3
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sat Nov 3 17:22:53 2018 +0100

    CharSequences.parseDoubles(…) should returns an array of length 0 for empty strings.
---
 .../main/java/org/apache/sis/util/CharSequences.java  | 19 ++++++++++++++++++-
 .../java/org/apache/sis/util/CharSequencesTest.java   |  8 +++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java b/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
index 66811f1..c84e518 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
@@ -72,7 +72,7 @@ import static java.lang.Character.*;
  * {@code 0} or {@code false} primitive type calculated as if the input was an empty string.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.7
+ * @version 1.0
  *
  * @see StringBuilders
  *
@@ -779,6 +779,17 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
     }
 
     /**
+     * Returns {@code true} if {@link #split(CharSequence, char)} parsed an empty string.
+     */
+    private static boolean isEmpty(final CharSequence[] tokens) {
+        switch (tokens.length) {
+            case 0:  return true;
+            case 1:  return tokens[0].length() == 0;
+            default: return false;
+        }
+    }
+
+    /**
      * {@linkplain #split(CharSequence, char) Splits} the given text around the given character,
      * then {@linkplain Double#parseDouble(String) parses} each item as a {@code double}.
      * Empty sub-sequences are parsed as {@link Double#NaN}.
@@ -793,6 +804,7 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
             throws NumberFormatException
     {
         final CharSequence[] tokens = split(values, separator);
+        if (isEmpty(tokens)) return ArraysExt.EMPTY_DOUBLE;
         final double[] parsed = new double[tokens.length];
         for (int i=0; i<tokens.length; i++) {
             final String token = trimWhitespaces(tokens[i]).toString();
@@ -816,6 +828,7 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
             throws NumberFormatException
     {
         final CharSequence[] tokens = split(values, separator);
+        if (isEmpty(tokens)) return ArraysExt.EMPTY_FLOAT;
         final float[] parsed = new float[tokens.length];
         for (int i=0; i<tokens.length; i++) {
             final String token = trimWhitespaces(tokens[i]).toString();
@@ -839,6 +852,7 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
             throws NumberFormatException
     {
         final CharSequence[] tokens = split(values, separator);
+        if (isEmpty(tokens)) return ArraysExt.EMPTY_LONG;
         final long[] parsed = new long[tokens.length];
         for (int i=0; i<tokens.length; i++) {
             parsed[i] = Long.parseLong(trimWhitespaces(tokens[i]).toString(), radix);
@@ -861,6 +875,7 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
             throws NumberFormatException
     {
         final CharSequence[] tokens = split(values, separator);
+        if (isEmpty(tokens)) return ArraysExt.EMPTY_INT;
         final int[] parsed = new int[tokens.length];
         for (int i=0; i<tokens.length; i++) {
             parsed[i] = Integer.parseInt(trimWhitespaces(tokens[i]).toString(), radix);
@@ -883,6 +898,7 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
             throws NumberFormatException
     {
         final CharSequence[] tokens = split(values, separator);
+        if (isEmpty(tokens)) return ArraysExt.EMPTY_SHORT;
         final short[] parsed = new short[tokens.length];
         for (int i=0; i<tokens.length; i++) {
             parsed[i] = Short.parseShort(trimWhitespaces(tokens[i]).toString(), radix);
@@ -905,6 +921,7 @@ search:     for (; fromIndex <= toIndex; fromIndex++) {
             throws NumberFormatException
     {
         final CharSequence[] tokens = split(values, separator);
+        if (isEmpty(tokens)) return ArraysExt.EMPTY_BYTE;
         final byte[] parsed = new byte[tokens.length];
         for (int i=0; i<tokens.length; i++) {
             parsed[i] = Byte.parseByte(trimWhitespaces(tokens[i]).toString(), radix);
diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/CharSequencesTest.java b/core/sis-utility/src/test/java/org/apache/sis/util/CharSequencesTest.java
index e4b889e..34b164d 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/util/CharSequencesTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/util/CharSequencesTest.java
@@ -33,7 +33,7 @@ import static org.apache.sis.util.CharSequences.*;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Johann Sorel (Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.3
  * @module
  */
@@ -183,6 +183,7 @@ public final strictfp class CharSequencesTest extends TestCase {
     @Test
     @DependsOnMethod("testSplit")
     public void testParseDoubles() {
+        assertEquals(0, parseDoubles("", ',').length);
         assertArrayEquals(new double[] {5, 1.5, Double.NaN, -8}, parseDoubles("5 , 1.5,,
-8 ", ','), 0.0);
     }
 
@@ -192,6 +193,7 @@ public final strictfp class CharSequencesTest extends TestCase {
     @Test
     @DependsOnMethod("testSplit")
     public void testParseFloats() {
+        assertEquals(0, parseFloats("", ',').length);
         assertArrayEquals(new float[] {5, 1.5f, Float.NaN, -8}, parseFloats("5 , 1.5,, -8
", ','), 0f);
     }
 
@@ -201,6 +203,7 @@ public final strictfp class CharSequencesTest extends TestCase {
     @Test
     @DependsOnMethod("testSplit")
     public void testParseLongs() {
+        assertEquals(0, parseLongs("", ',', 10).length);
         assertArrayEquals(new long[] {5, 2, -8}, parseLongs("5 , 2, -8 ", ',', 10));
     }
 
@@ -210,6 +213,7 @@ public final strictfp class CharSequencesTest extends TestCase {
     @Test
     @DependsOnMethod("testSplit")
     public void testParseInts() {
+        assertEquals(0, parseInts("", ',', 10).length);
         assertArrayEquals(new int[] {5, 2, -8}, parseInts("5 , 2, -8 ", ',', 10));
     }
 
@@ -219,6 +223,7 @@ public final strictfp class CharSequencesTest extends TestCase {
     @Test
     @DependsOnMethod("testSplit")
     public void testParseShorts() {
+        assertEquals(0, parseShorts("", ',', 10).length);
         assertArrayEquals(new short[] {5, 2, -8}, parseShorts("5 , 2, -8 ", ',', 10));
     }
 
@@ -228,6 +233,7 @@ public final strictfp class CharSequencesTest extends TestCase {
     @Test
     @DependsOnMethod("testSplit")
     public void testParseBytes() {
+        assertEquals(0, parseBytes("", ',', 10).length);
         assertArrayEquals(new byte[] {5, 2, -8}, parseBytes("5 , 2, -8 ", ',', 10));
     }
 


Mime
View raw message