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

Commit ac88508

Browse files
frauzufallimagejan
andcommitted
Making guessParser less error-prone
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 <jan.eglinger@fmi.ch>
1 parent 2cc51f6 commit ac88508

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/main/java/org/scijava/table/DefaultTableIOPlugin.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,14 @@ private GenericTable open(final Location source, TableIOOptions.Values options)
247247
return options.parser();
248248
}
249249

250-
static Function<String, Object> guessParser(String content) {
250+
static Function<String, ?> guessParser(String content) {
251251
try {
252-
Integer.valueOf(content);
253-
return Integer::valueOf;
254-
} catch(NumberFormatException ignored) {}
255-
try {
256-
Long.valueOf(content);
257-
return Long::valueOf;
258-
} catch(NumberFormatException ignored) {}
259-
try {
260-
Double.valueOf(content);
261-
return Double::valueOf;
252+
Function<String, ?> function = s -> Double.valueOf(s
253+
.replace("infinity", "Infinity")
254+
.replace("Nan", "NaN")
255+
);
256+
function.apply(content);
257+
return function;
262258
} catch(NumberFormatException ignored) {}
263259
if(content.equalsIgnoreCase("true")||content.equalsIgnoreCase("false")) {
264260
return Boolean::valueOf;

src/test/java/org/scijava/table/DefaultTableIOPluginTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void testDefaultOptions() throws IOException {
111111
}
112112

113113
/**
114-
* Tests if quoting works in different senarios.
114+
* Tests if quoting works in different scenarios.
115115
*/
116116
@Test
117117
public void testQuote() {
@@ -160,7 +160,7 @@ public void testQuote() {
160160
}
161161

162162
/**
163-
* Tests if samll tables could be opened/saved correctly.
163+
* Tests if small tables can be opened/saved correctly.
164164
*/
165165
@Test
166166
public void testSmallTables() {
@@ -250,11 +250,13 @@ public void testGuessParser() {
250250
assertEquals(false, DefaultTableIOPlugin.guessParser("false").apply("false"));
251251
assertEquals(123.0, DefaultTableIOPlugin.guessParser("123.0").apply("123.0"));
252252
assertEquals(-123.0, DefaultTableIOPlugin.guessParser("-123.0").apply("-123.0"));
253-
assertEquals(3, DefaultTableIOPlugin.guessParser("3").apply("3"));
254-
assertEquals(36564573745634564L, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
253+
assertEquals(3.0, DefaultTableIOPlugin.guessParser("3").apply("3"));
254+
assertEquals(36564573745634564d, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
255255
assertEquals(1234567890.0987654321, DefaultTableIOPlugin.guessParser("1.2345678900987654E9").apply("1.2345678900987654E9"));
256256
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("NaN").apply("NaN"));
257+
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("Nan").apply("Nan"));
257258
assertEquals(Double.NEGATIVE_INFINITY, DefaultTableIOPlugin.guessParser("-Infinity").apply("-Infinity"));
259+
assertEquals(Double.POSITIVE_INFINITY, DefaultTableIOPlugin.guessParser("infinity").apply("infinity"));
258260
assertEquals(0.0, DefaultTableIOPlugin.guessParser("0.0").apply("0.0"));
259261
}
260262

0 commit comments

Comments
 (0)