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 static boolean checkIfNodeDefined(final ImmutableNode node) {
44 return node.getValue() != null || !node.getChildren().isEmpty() || !node.getAttributes().isEmpty();
45 }
46
47 @Override
48 public Set<String> getAttributes(final ImmutableNode node) {
49 return node.getAttributes().keySet();
50 }
51
52 @Override
53 public Object getAttributeValue(final ImmutableNode node, final String name) {
54 return node.getAttributes().get(name);
55 }
56
57 @Override
58 public ImmutableNode getChild(final ImmutableNode node, final int index) {
59 return node.getChildren().get(index);
60 }
61
62 @Override
63 public List<ImmutableNode> getChildren(final ImmutableNode node) {
64 return node.getChildren();
65 }
66
67
68
69
70 @Override
71 public List<ImmutableNode> getChildren(final ImmutableNode node, final String name) {
72 return getMatchingChildren(node, NodeNameMatchers.EQUALS, name);
73 }
74
75 @Override
76 public int getChildrenCount(final ImmutableNode node, final String name) {
77 if (name == null) {
78 return node.getChildren().size();
79 }
80 return getMatchingChildrenCount(node, NodeNameMatchers.EQUALS, name);
81 }
82
83
84
85
86 @Override
87 public <C> List<ImmutableNode> getMatchingChildren(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
88 return Collections.unmodifiableList(node.stream().filter(c -> matcher.matches(c, this, criterion)).collect(Collectors.toList()));
89 }
90
91 @Override
92 public <C> int getMatchingChildrenCount(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
93 return getMatchingChildren(node, matcher, criterion).size();
94 }
95
96 @Override
97 public Object getValue(final ImmutableNode node) {
98 return node.getValue();
99 }
100
101 @Override
102 public boolean hasAttributes(final ImmutableNode node) {
103 return !node.getAttributes().isEmpty();
104 }
105
106 @Override
107 public int indexOfChild(final ImmutableNode parent, final ImmutableNode child) {
108 return parent.getChildren().indexOf(child);
109 }
110
111
112
113
114 @Override
115 public boolean isDefined(final ImmutableNode node) {
116 return checkIfNodeDefined(node);
117 }
118
119 @Override
120 public String nodeName(final ImmutableNode node) {
121 return node.getNodeName();
122 }
123 }