Skip to content

Commit aceb501

Browse files
committed
Small cleanup.
1 parent 8147ae5 commit aceb501

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/main/java/net/finmath/util/config/ConfigTree.java

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.finmath.util.config;
22

3+
import java.util.Arrays;
34
import java.util.List;
45
import java.util.Map;
56
import java.util.stream.Collectors;
@@ -23,7 +24,7 @@
2324
public class ConfigTree {
2425

2526
private final Node root;
26-
27+
2728
/**
2829
* Construct the tree.
2930
*
@@ -37,13 +38,18 @@ public ConfigTree(List<String> keyOrder, List<Map<String, Object>> configs) {
3738
/**
3839
* Get the configuration for a given specification of the properties (selector).
3940
*
41+
* The configutation tree is traversed by selecting each route though the value of a specific key in the selector,
42+
* until the lead node is reached.
43+
* If keys are missing in the selector of if values do not match a predefined route, a default route is used.
44+
*
4045
* @param selector Maps the name (String) of a property to its value (Object).
4146
* @return The configuration value for the given selector.
4247
*/
4348
public Object getConfig(Map<String, Object> selector) {
44-
49+
4550
Node node = this.root;
46-
51+
52+
// Traverse the tree where each route is selected though the value of a specific key in the selector.
4753
while(node instanceof ConfigNode) {
4854
ConfigNode configNode = (ConfigNode)node;
4955
if(selector.containsKey(configNode.getKey()) && configNode.getValueToConfig().keySet().contains(selector.get(configNode.getKey()))) {
@@ -53,7 +59,8 @@ public Object getConfig(Map<String, Object> selector) {
5359
node = configNode.getValueToConfig().get(SpecialNodes.DEFAULT_VALUE);
5460
}
5561
}
56-
62+
63+
// Having reached the value node, return it.
5764
if(node instanceof ValueNode) {
5865
ValueNode valueNode = (ValueNode)node;
5966
return valueNode.getValue();
@@ -62,7 +69,7 @@ public Object getConfig(Map<String, Object> selector) {
6269
throw new IllegalArgumentException("Unable to resolve configuration from the given properties.");
6370
}
6471
}
65-
72+
6673
/**
6774
* Helper for the constructor. Recursive contruction of the tree.
6875
*
@@ -71,27 +78,29 @@ public Object getConfig(Map<String, Object> selector) {
7178
* @return Node of the (sub-)tree for the given config key.
7279
*/
7380
private Node group(List<String> keyOrder, List<Map<String, Object>> configs) {
74-
if(keyOrder.size() == 0) {
81+
if(keyOrder.size() > 0) {
82+
// Group all elements by the first key in keyOrder....
83+
String key = keyOrder.get(0);
84+
Map<Object, List<Map<String, Object>>> grouped = configs.stream().collect(Collectors.groupingBy(map -> map.get(key)));
85+
86+
// ...call group (recursive) for all values below this key taking the remainder of keyOrder...
87+
List<String> keyOrderRemain = keyOrder.subList(1, keyOrder.size());
88+
Map<Object, Node> valueToConfig = grouped.entrySet().stream().collect(Collectors.toMap(
89+
Map.Entry::getKey, entry -> group(keyOrderRemain, entry.getValue())));
90+
91+
// ...create a ConfigNode for this key.
92+
return new ConfigNode(key, valueToConfig);
93+
}
94+
else {
95+
// If no keys are left in key order, create the leaf node
7596
if(configs.size() == 1) {
7697
Map<String, Object> config = configs.get(0);
7798
Object value = config.get("value");
7899
return new ValueNode( value);
79100
}
80101
else {
81-
throw new IllegalArgumentException("Multiple configs for same values.");
102+
throw new IllegalArgumentException("Multiple configs for the same selector values. " + Arrays.deepToString(configs.toArray()));
82103
}
83104
}
84-
85-
// Group all elements by the first key....
86-
String key = keyOrder.get(0);
87-
Map<Object, List<Map<String, Object>>> grouped = configs.stream().collect(Collectors.groupingBy(map -> map.get(key)));
88-
89-
// ...call group (recursive) for all values below this key...
90-
List<String> keyOrderRemain = keyOrder.subList(1, keyOrder.size());
91-
Map<Object, Node> valueToConfig = grouped.entrySet().stream().collect(Collectors.toMap(
92-
Map.Entry::getKey, entry -> group(keyOrderRemain, entry.getValue())));
93-
94-
// ...create a ConfigNode for this key.
95-
return new ConfigNode(key, valueToConfig);
96105
}
97106
}

0 commit comments

Comments
 (0)