From e8b8e0ff07778b3cbc6deb2dd0aec755ecbb602a Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Fri, 13 Sep 2024 14:40:12 +0200 Subject: [PATCH] fix for multiple elements Signed-off-by: Jan N. Klug --- .../java/org/mustangproject/util/NodeMap.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/library/src/main/java/org/mustangproject/util/NodeMap.java b/library/src/main/java/org/mustangproject/util/NodeMap.java index 373781f7..6afbb911 100644 --- a/library/src/main/java/org/mustangproject/util/NodeMap.java +++ b/library/src/main/java/org/mustangproject/util/NodeMap.java @@ -24,7 +24,9 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -38,7 +40,7 @@ import java.util.stream.Stream; * It can be constructed either from the children of a single {@link Node} or a {@link NodeList}. */ public class NodeMap { - private final Map map; + private final Map> map = new HashMap<>(); /** * Create a new {@link NodeMap} @@ -53,10 +55,8 @@ public class NodeMap { if (node == null) { throw new IllegalArgumentException("node cannot be null"); } - if (!node.hasChildNodes()) { - map = Map.of(); - } else { - map = mapNodeList(node.getChildNodes()); + if (node.hasChildNodes()) { + mapNodeList(node.getChildNodes()); } } @@ -64,7 +64,7 @@ public class NodeMap { if (nodeList == null) { throw new IllegalArgumentException("nodeList cannot be null"); } - map = mapNodeList(nodeList); + mapNodeList(nodeList); } /** @@ -135,13 +135,13 @@ public class NodeMap { */ public Stream getAllNodes(String... localNames) { List localNamesList = Arrays.asList(localNames); - return map.entrySet().stream().filter(e -> localNamesList.contains(e.getKey())).map(Map.Entry::getValue); + return map.entrySet().stream().filter(e -> localNamesList.contains(e.getKey())).flatMap(e -> e.getValue().stream()); } - private Map mapNodeList(NodeList nodeList) { - return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item) + private void mapNodeList(NodeList nodeList) { + IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item) .filter(node -> node != null && node.getLocalName() != null) - .collect(Collectors.toMap(Node::getLocalName, node -> node)); + .forEach(node -> map.computeIfAbsent(node.getLocalName(), k -> new ArrayList<>()).add(node)); } @Override