From 97759ba663a23eaf355ec1ff07e5fde5b9722555 Mon Sep 17 00:00:00 2001 From: nice2h8u <36359093+nice2h8u@users.noreply.github.com> Date: Sun, 30 Jan 2022 00:49:05 +0300 Subject: [PATCH] #23 fix number parsing bug (#24) * #23 fix number parsing bug Co-authored-by: Ilya --- .../parser/NoneableDoubleAdapter.java | 2 +- .../parser/NoneableLongAdapter.java | 2 +- .../alphavantage/parser/Parser.java | 11 ++++++ src/test/java/parser/ParserUtilTest.java | 34 +++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/test/java/parser/ParserUtilTest.java diff --git a/src/main/java/com/crazzyghost/alphavantage/parser/NoneableDoubleAdapter.java b/src/main/java/com/crazzyghost/alphavantage/parser/NoneableDoubleAdapter.java index a199055..1a2c409 100644 --- a/src/main/java/com/crazzyghost/alphavantage/parser/NoneableDoubleAdapter.java +++ b/src/main/java/com/crazzyghost/alphavantage/parser/NoneableDoubleAdapter.java @@ -13,7 +13,7 @@ public Double toJson(@NoneableDouble Double l) { @FromJson @NoneableDouble public Double fromJson(String s) { - return s.equalsIgnoreCase("none") ? null: Double.valueOf(s); + return Parser.getNumberFromString(s, Double::parseDouble); } } diff --git a/src/main/java/com/crazzyghost/alphavantage/parser/NoneableLongAdapter.java b/src/main/java/com/crazzyghost/alphavantage/parser/NoneableLongAdapter.java index 9e6c6e0..c54c780 100644 --- a/src/main/java/com/crazzyghost/alphavantage/parser/NoneableLongAdapter.java +++ b/src/main/java/com/crazzyghost/alphavantage/parser/NoneableLongAdapter.java @@ -13,7 +13,7 @@ public Long toJson(@NoneableLong Long l) { @FromJson @NoneableLong public Long fromJson(String s) { - return s.equalsIgnoreCase("none") ? null: Long.valueOf(s); + return Parser.getNumberFromString(s, Long::parseLong); } } diff --git a/src/main/java/com/crazzyghost/alphavantage/parser/Parser.java b/src/main/java/com/crazzyghost/alphavantage/parser/Parser.java index 2a4519d..e5f360e 100644 --- a/src/main/java/com/crazzyghost/alphavantage/parser/Parser.java +++ b/src/main/java/com/crazzyghost/alphavantage/parser/Parser.java @@ -8,6 +8,7 @@ import java.lang.reflect.Type; import java.util.List; import java.util.Map; +import java.util.function.Function; /** * @author crazzyghost @@ -62,4 +63,14 @@ public static String toJSON(Map data) throws IOException { Type type = Types.newParameterizedType(Map.class, String.class, Object.class); return moshi.adapter(type).toJson(data); } + + public static N getNumberFromString(String s, Function parser) { + N result = null; + try { + result = parser.apply(s); + } catch (NumberFormatException ex) { + } + + return result; + } } \ No newline at end of file diff --git a/src/test/java/parser/ParserUtilTest.java b/src/test/java/parser/ParserUtilTest.java new file mode 100644 index 0000000..298f4f8 --- /dev/null +++ b/src/test/java/parser/ParserUtilTest.java @@ -0,0 +1,34 @@ +package parser; + +import com.crazzyghost.alphavantage.parser.Parser; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ParserUtilTest { + + @Test + public void testGetLongFromString() { + long actualNum = Parser.getNumberFromString("1", Long::parseLong); + assertEquals(1L, actualNum); + } + + @Test + public void testGetLongFromNotNumberString() { + Long actualNum = Parser.getNumberFromString("-", Long::parseLong); + assertNull(actualNum); + } + + @Test + public void testGetDoubleFromString() { + double actualNum = Parser.getNumberFromString("-1.2312342342", Double::parseDouble); + assertEquals(-1.2312342342, actualNum, 0.0); + } + + @Test + public void testGetDoubleFromNotNumberString() { + Double actualNum = Parser.getNumberFromString("-", Double::parseDouble); + assertNull(actualNum); + } +} \ No newline at end of file