Skip to content

Commit

Permalink
better number handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
MammatusPlatypus committed May 4, 2016
1 parent 4e09d0b commit 943a917
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

group 'io.advantageous.konf'
version '1.3.4'
version '1.3.5'

apply plugin: 'java'
apply plugin: 'maven'
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/io/advantageous/config/ConfigFromObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -222,16 +223,23 @@ private List<Number> 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<Object> list = (List<Object>) 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<Number>) value;
}


Expand Down Expand Up @@ -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");
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/advantageous/config/ConfigImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 943a917

Please sign in to comment.