From 943a917e86327f261d0a860680e195e48f609fe3 Mon Sep 17 00:00:00 2001 From: RichardHightower Date: Tue, 3 May 2016 21:02:01 -0700 Subject: [PATCH] better number handling. --- build.gradle | 2 +- .../advantageous/config/ConfigFromObject.java | 28 +++++++++++++------ .../advantageous/config/ConfigImplTest.java | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index ac2c03b..753a382 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ */ group 'io.advantageous.konf' -version '1.3.4' +version '1.3.5' apply plugin: 'java' apply plugin: 'maven' diff --git a/src/main/java/io/advantageous/config/ConfigFromObject.java b/src/main/java/io/advantageous/config/ConfigFromObject.java index fecf450..742d441 100644 --- a/src/main/java/io/advantageous/config/ConfigFromObject.java +++ b/src/main/java/io/advantageous/config/ConfigFromObject.java @@ -13,6 +13,7 @@ import java.time.format.DateTimeParseException; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; import java.util.stream.Collectors; import static io.advantageous.boon.core.Maps.map; @@ -222,16 +223,23 @@ private List getNumberList(final String path) { Object value = validatePath(path); if (value instanceof ScriptObjectMirror) { value = extractListFromScriptObjectMirror(path, value, Number.class); - } else if (value instanceof List) { - ((List) value).stream().forEach(o -> { - if (!(o instanceof Number)) { - throw new IllegalArgumentException("Path must equate to list with Numbers," + + } + + if (value instanceof List) { + + List list = (List) value; + return list.stream().filter(o -> { + if (!(o instanceof Number || o instanceof CharSequence)) { + throw new IllegalArgumentException("Path must equate to list with Numbers or Strings" + + " that can be parsed to numbers," + " but found type " + (o == null ? null : o.getClass().getName())); } - }); + return true; + }).map(o->convertObjectToNumber(path, o)).collect(Collectors.toList()); + } else { + throw new IllegalArgumentException("Path must equate to list with Numbers or Strings" + + " that can be parsed to numbers"); } - //noinspection ConstantConditions - return (List) value; } @@ -322,8 +330,11 @@ private Object validatePath(String path) { } private Number validateNumberInPath(String path) { - Object object = findProperty(root, path); + final Object object = findProperty(root, path); + return convertObjectToNumber(path, object); + } + private Number convertObjectToNumber(String path, Object object) { if (object == null) { throw new IllegalArgumentException("Path or property " + path + " does not exist"); } @@ -343,7 +354,6 @@ private Number validateNumberInPath(String path) { throw new IllegalArgumentException("Path or property " + path + " exists but is not a number value =" + object); - } private Duration convertStringToDuration(final String path, final Object value) { diff --git a/src/test/java/io/advantageous/config/ConfigImplTest.java b/src/test/java/io/advantageous/config/ConfigImplTest.java index 4e3a1d2..922c95a 100644 --- a/src/test/java/io/advantageous/config/ConfigImplTest.java +++ b/src/test/java/io/advantageous/config/ConfigImplTest.java @@ -42,7 +42,7 @@ public void setUp() throws Exception { "longs", asList(1.0, 2.0, 3.0), "ints", asList(1, 2, 3), "intsNull", asList(1, null, 3), - "intsWrongType", asList(1, "2", 3) + "intsWrongType", asList(1, true, 3) ); config = new ConfigFromObject(map); }