11
11
import java .nio .file .Paths ;
12
12
import java .util .*;
13
13
import java .util .Map .Entry ;
14
+ import java .util .stream .Collectors ;
15
+ import java .util .stream .StreamSupport ;
14
16
15
17
public class JsonSettingsFile implements ISettingsFile {
16
18
@@ -34,9 +36,9 @@ public Object getValue(String jsonPath) {
34
36
private Object getEnvValueOrDefault (String jsonPath , boolean throwIfEmpty ) {
35
37
String envVar = getEnvValue (jsonPath );
36
38
JsonNode node = getJsonNode (jsonPath , throwIfEmpty && envVar == null );
37
- return ! node .isMissingNode ()
38
- ? castEnvOrDefaulValue ( node , envVar )
39
- : envVar ;
39
+ return node .isMissingNode ()
40
+ ? envVar
41
+ : castEnvOrDefaultValue ( node , envVar ) ;
40
42
}
41
43
42
44
/**
@@ -46,7 +48,7 @@ private Object getEnvValueOrDefault(String jsonPath, boolean throwIfEmpty) {
46
48
* @param envVar value got from environment variable
47
49
* @return Value, casted to specific type.
48
50
*/
49
- private Object castEnvOrDefaulValue (JsonNode node , String envVar ) {
51
+ private Object castEnvOrDefaultValue (JsonNode node , String envVar ) {
50
52
if (node .isBoolean ()) {
51
53
return envVar == null ? node .asBoolean () : Boolean .parseBoolean (envVar );
52
54
} else if (node .isLong ()) {
@@ -55,6 +57,8 @@ private Object castEnvOrDefaulValue(JsonNode node, String envVar) {
55
57
return envVar == null ? node .asInt () : Integer .parseInt (envVar );
56
58
} else if (node .isDouble ()) {
57
59
return envVar == null ? node .asDouble () : Double .parseDouble (envVar );
60
+ } else if (node .isObject ()) {
61
+ return envVar == null ? node .toString () : envVar ;
58
62
} else {
59
63
return envVar == null ? node .asText () : envVar ;
60
64
}
@@ -66,29 +70,31 @@ private String getEnvValue(String jsonPath) {
66
70
if (envVar != null ) {
67
71
Logger .getInstance ().debug (String .format ("***** Using variable passed from environment %1$s=%2$s" , key , envVar ));
68
72
}
69
-
70
73
return envVar ;
71
74
}
72
75
73
76
@ Override
74
77
public List <String > getList (String jsonPath ) {
75
- List <String > list = new ArrayList <>() ;
78
+ List <String > list ;
76
79
String envVar = getEnvValue (jsonPath );
77
80
if (envVar != null ) {
78
- Arrays .stream (envVar .split ("," )).forEach (element -> list .add (element .trim ()));
81
+ list = Arrays .stream (envVar .split ("," ))
82
+ .map (String ::trim )
83
+ .collect (Collectors .toList ());
79
84
} else {
80
- getJsonNode (jsonPath ).elements ().forEachRemaining (node -> list .add (node .asText ()));
85
+ Spliterator <JsonNode > spliterator = Spliterators .spliteratorUnknownSize (getJsonNode (jsonPath ).elements (), Spliterator .ORDERED );
86
+ list = StreamSupport .stream (spliterator , false )
87
+ .map (JsonNode ::asText )
88
+ .collect (Collectors .toList ());
81
89
}
82
-
83
90
return list ;
84
91
}
85
92
86
93
@ Override
87
94
public Map <String , Object > getMap (String jsonPath ) {
88
- Iterator <Entry <String , JsonNode >> iterator = getJsonNode (jsonPath ).fields ();
89
- final Map <String , Object > result = new HashMap <>();
90
- iterator .forEachRemaining (entry -> result .put (entry .getKey (), getValue (jsonPath + "/" + entry .getKey ())));
91
- return result ;
95
+ Spliterator <Entry <String , JsonNode >> spliterator = Spliterators .spliteratorUnknownSize (getJsonNode (jsonPath ).fields (), Spliterator .ORDERED );
96
+ return StreamSupport .stream (spliterator , false )
97
+ .collect (Collectors .toMap (Entry ::getKey , entry -> getValue (jsonPath + "/" + entry .getKey ())));
92
98
}
93
99
94
100
private JsonNode getJsonNode (String jsonPath ) {
0 commit comments