Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Making guessParser less error-prone
Browse files Browse the repository at this point in the history
When auto detecting the column type of such a column, the previous
guessParser implementation produced exceptions:

-1
0
-1.0
1.0
0.0
infinity
-infinity
+Nan
NaN
-NaN
1.23e7
13.8f
57269d

Therefore, now the only number format which will be guessed by
guessParser is Double. Also, when testing this with imagej-server, the
Strings "infinity" and "Nan" where used which are not compatible with
Double::valueOf, therefore they are replaced with the compatible
capitalization. Maybe there is a better way to  do this..

Co-authored-by: Jan Eglinger <[email protected]>
  • Loading branch information
frauzufall and imagejan committed Aug 14, 2020
1 parent 2cc51f6 commit ac88508
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
18 changes: 7 additions & 11 deletions src/main/java/org/scijava/table/DefaultTableIOPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,14 @@ private GenericTable open(final Location source, TableIOOptions.Values options)
return options.parser();
}

static Function<String, Object> guessParser(String content) {
static Function<String, ?> guessParser(String content) {
try {
Integer.valueOf(content);
return Integer::valueOf;
} catch(NumberFormatException ignored) {}
try {
Long.valueOf(content);
return Long::valueOf;
} catch(NumberFormatException ignored) {}
try {
Double.valueOf(content);
return Double::valueOf;
Function<String, ?> function = s -> Double.valueOf(s
.replace("infinity", "Infinity")
.replace("Nan", "NaN")
);
function.apply(content);
return function;
} catch(NumberFormatException ignored) {}
if(content.equalsIgnoreCase("true")||content.equalsIgnoreCase("false")) {
return Boolean::valueOf;
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/org/scijava/table/DefaultTableIOPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testDefaultOptions() throws IOException {
}

/**
* Tests if quoting works in different senarios.
* Tests if quoting works in different scenarios.
*/
@Test
public void testQuote() {
Expand Down Expand Up @@ -160,7 +160,7 @@ public void testQuote() {
}

/**
* Tests if samll tables could be opened/saved correctly.
* Tests if small tables can be opened/saved correctly.
*/
@Test
public void testSmallTables() {
Expand Down Expand Up @@ -250,11 +250,13 @@ public void testGuessParser() {
assertEquals(false, DefaultTableIOPlugin.guessParser("false").apply("false"));
assertEquals(123.0, DefaultTableIOPlugin.guessParser("123.0").apply("123.0"));
assertEquals(-123.0, DefaultTableIOPlugin.guessParser("-123.0").apply("-123.0"));
assertEquals(3, DefaultTableIOPlugin.guessParser("3").apply("3"));
assertEquals(36564573745634564L, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
assertEquals(3.0, DefaultTableIOPlugin.guessParser("3").apply("3"));
assertEquals(36564573745634564d, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
assertEquals(1234567890.0987654321, DefaultTableIOPlugin.guessParser("1.2345678900987654E9").apply("1.2345678900987654E9"));
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("NaN").apply("NaN"));
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("Nan").apply("Nan"));
assertEquals(Double.NEGATIVE_INFINITY, DefaultTableIOPlugin.guessParser("-Infinity").apply("-Infinity"));
assertEquals(Double.POSITIVE_INFINITY, DefaultTableIOPlugin.guessParser("infinity").apply("infinity"));
assertEquals(0.0, DefaultTableIOPlugin.guessParser("0.0").apply("0.0"));
}

Expand Down

0 comments on commit ac88508

Please sign in to comment.