1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.tree;
18
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23
24
25
26
27
28
29
30
31
32
33
34
35
36 abstract class AbstractImmutableNodeHandler implements NodeHandler<ImmutableNode> {
37
38
39
40
41
42
43
44 static boolean checkIfNodeDefined(final ImmutableNode node) {
45 return node.getValue() != null || !node.getChildren().isEmpty() || !node.getAttributes().isEmpty();
46 }
47
48 @Override
49 public Set<String> getAttributes(final ImmutableNode node) {
50 return node.getAttributes().keySet();
51 }
52
53 @Override
54 public Object getAttributeValue(final ImmutableNode node, final String name) {
55 return node.getAttributes().get(name);
56 }
57
58 @Override
59 public ImmutableNode getChild(final ImmutableNode node, final int index) {
60 return node.getChildren().get(index);
61 }
62
63 @Override
64 public List<ImmutableNode> getChildren(final ImmutableNode node) {
65 return node.getChildren();
66 }
67
68
69
70
71 @Override
72 public List<ImmutableNode> getChildren(final ImmutableNode node, final String name) {
73 return getMatchingChildren(node, NodeNameMatchers.EQUALS, name);
74 }
75
76 @Override
77 public int getChildrenCount(final ImmutableNode node, final String name) {
78 if (name == null) {
79 return node.getChildren().size();
80 }
81 return getMatchingChildrenCount(node, NodeNameMatchers.EQUALS, name);
82 }
83
84
85
86
87 @Override
88 public <C> List<ImmutableNode> getMatchingChildren(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
89 return Collections.unmodifiableList(node.stream().filter(c -> matcher.matches(c, this, criterion)).collect(Collectors.toList()));
90 }
91
92 @Override
93 public <C> int getMatchingChildrenCount(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
94 return getMatchingChildren(node, matcher, criterion).size();
95 }
96
97 @Override
98 public Object getValue(final ImmutableNode node) {
99 return node.getValue();
100 }
101
102 @Override
103 public boolean hasAttributes(final ImmutableNode node) {
104 return !node.getAttributes().isEmpty();
105 }
106
107 @Override
108 public int indexOfChild(final ImmutableNode parent, final ImmutableNode child) {
109 return parent.getChildren().indexOf(child);
110 }
111
112
113
114
115 @Override
116 public boolean isDefined(final ImmutableNode node) {
117 return checkIfNodeDefined(node);
118 }
119
120 @Override
121 public String nodeName(final ImmutableNode node) {
122 return node.getNodeName();
123 }
124 }