View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * <p>
26   * An abstract base class for a {@link NodeHandler} implementation for {@link ImmutableNode} objects.
27   * </p>
28   * <p>
29   * This class already implements all methods which need no other information than the passed in node object.
30   * Functionality which requires additional state (e.g. querying the root node or a parent node) has to be added by
31   * concrete sub classes.
32   * </p>
33   *
34   * @since 2.0
35   */
36  abstract class AbstractImmutableNodeHandler implements NodeHandler<ImmutableNode> {
37      @Override
38      public String nodeName(final ImmutableNode node) {
39          return node.getNodeName();
40      }
41  
42      @Override
43      public Object getValue(final ImmutableNode node) {
44          return node.getValue();
45      }
46  
47      @Override
48      public List<ImmutableNode> getChildren(final ImmutableNode node) {
49          return node.getChildren();
50      }
51  
52      @Override
53      public <C> int getMatchingChildrenCount(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
54          return getMatchingChildren(node, matcher, criterion).size();
55      }
56  
57      /**
58       * {@inheritDoc} This implementation returns an immutable list with all child nodes accepted by the specified matcher.
59       */
60      @Override
61      public <C> List<ImmutableNode> getMatchingChildren(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
62          return Collections.unmodifiableList(node.stream().filter(c -> matcher.matches(c, this, criterion)).collect(Collectors.toList()));
63      }
64  
65      /**
66       * {@inheritDoc} This implementation returns an immutable list with all child nodes that have the specified name.
67       */
68      @Override
69      public List<ImmutableNode> getChildren(final ImmutableNode node, final String name) {
70          return getMatchingChildren(node, NodeNameMatchers.EQUALS, name);
71      }
72  
73      @Override
74      public ImmutableNode getChild(final ImmutableNode node, final int index) {
75          return node.getChildren().get(index);
76      }
77  
78      @Override
79      public int indexOfChild(final ImmutableNode parent, final ImmutableNode child) {
80          return parent.getChildren().indexOf(child);
81      }
82  
83      @Override
84      public int getChildrenCount(final ImmutableNode node, final String name) {
85          if (name == null) {
86              return node.getChildren().size();
87          }
88          return getMatchingChildrenCount(node, NodeNameMatchers.EQUALS, name);
89      }
90  
91      @Override
92      public Set<String> getAttributes(final ImmutableNode node) {
93          return node.getAttributes().keySet();
94      }
95  
96      @Override
97      public boolean hasAttributes(final ImmutableNode node) {
98          return !node.getAttributes().isEmpty();
99      }
100 
101     @Override
102     public Object getAttributeValue(final ImmutableNode node, final String name) {
103         return node.getAttributes().get(name);
104     }
105 
106     /**
107      * {@inheritDoc} This implementation assumes that a node is defined if it has a value or has children or has attributes.
108      */
109     @Override
110     public boolean isDefined(final ImmutableNode node) {
111         return AbstractImmutableNodeHandler.checkIfNodeDefined(node);
112     }
113 
114     /**
115      * Checks if the passed in node is defined. Result is <b>true</b> if the node contains any data.
116      *
117      * @param node the node in question
118      * @return <b>true</b> if the node is defined, <b>false</b> otherwise
119      */
120     static boolean checkIfNodeDefined(final ImmutableNode node) {
121         return node.getValue() != null || !node.getChildren().isEmpty() || !node.getAttributes().isEmpty();
122     }
123 }