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    *     https://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.List;
20  import java.util.Set;
21  
22  /**
23   * <p>
24   * Definition of an interface for accessing the data of a configuration node.
25   * </p>
26   * <p>
27   * Hierarchical configurations can deal with arbitrary node structures. In order to obtain information about a specific
28   * node object, a so-called {@code NodeHandler} is used. The handler provides a number of methods for querying the
29   * internal state of a node in a read-only way.
30   * </p>
31   *
32   * @param <T> the type of the nodes this handler deals with
33   */
34  public interface NodeHandler<T> {
35  
36      /**
37       * Gets an unmodifiable set with the names of all attributes of the specified node.
38       *
39       * @param node the node
40       * @return a set with the names of all attributes of this node
41       */
42      Set<String> getAttributes(T node);
43  
44      /**
45       * Gets the value of the specified attribute from the given node. If a concrete {@code NodeHandler} supports
46       * attributes with multiple values, result might be a collection.
47       *
48       * @param node the node
49       * @param name the name of the attribute
50       * @return the value of this attribute
51       */
52      Object getAttributeValue(T node, String name);
53  
54      /**
55       * Gets the child with the given index of the specified node.
56       *
57       * @param node the node
58       * @param index the index (0-based)
59       * @return the child with the given index
60       */
61      T getChild(T node, int index);
62  
63      /**
64       * Gets an unmodifiable list with all children of the specified node.
65       *
66       * @param node the node
67       * @return a list with the child nodes of this node
68       */
69      List<T> getChildren(T node);
70  
71      /**
72       * Gets an unmodifiable list of all children of the specified node with the given name.
73       *
74       * @param node the node
75       * @param name the name of the desired child nodes
76       * @return a list with all children with the given name
77       */
78      List<T> getChildren(T node, String name);
79  
80      /**
81       * Gets the number of children of the specified node with the given name. This method exists for performance reasons:
82       * for some node implementations it may be by far more efficient to count the children than to query a list of all
83       * children and determine its size. A concrete implementation can choose the most efficient way to determine the number
84       * of children. If a child name is passed in, only the children with this name are taken into account. If the name
85       * <strong>null</strong> is passed, the total number of children must be returned.
86       *
87       * @param node the node
88       * @param name the name of the children in question (can be <strong>null</strong> for all children)
89       * @return the number of the selected children
90       */
91      int getChildrenCount(T node, String name);
92  
93      /**
94       * Gets an unmodifiable list of all children of the specified node which are matched by the passed in
95       * {@code NodeMatcher} against the provided criterion. This method allows for advanced queries on a node's children.
96       *
97       * @param node the node
98       * @param matcher the {@code NodeMatcher} defining filter criteria
99       * @param criterion the criterion to be matched against; this object is passed to the {@code NodeMatcher}
100      * @param <C> the type of the criterion
101      * @return a list with all children matched by the matcher
102      */
103     <C> List<T> getMatchingChildren(T node, NodeMatcher<C> matcher, C criterion);
104 
105     /**
106      * Gets the number of children of the specified node which are matched by the given {@code NodeMatcher}. This is a
107      * more generic version of {@link #getChildrenCount(Object, String)}. It allows checking for arbitrary filter
108      * conditions.
109      *
110      * @param node the node
111      * @param matcher the {@code NodeMatcher}
112      * @param criterion the criterion to be passed to the {@code NodeMatcher}
113      * @param <C> the type of the criterion
114      * @return the number of matched children
115      */
116     <C> int getMatchingChildrenCount(T node, NodeMatcher<C> matcher, C criterion);
117 
118     /**
119      * Gets the parent of the specified node.
120      *
121      * @param node the node
122      * @return the parent node
123      */
124     T getParent(T node);
125 
126     /**
127      * Gets the root node of the underlying hierarchy.
128      *
129      * @return the current root node
130      */
131     T getRootNode();
132 
133     /**
134      * Gets the value of the specified node.
135      *
136      * @param node the node
137      * @return the value of this node
138      */
139     Object getValue(T node);
140 
141     /**
142      * Returns a flag whether the passed in node has any attributes.
143      *
144      * @param node the node
145      * @return a flag whether this node has any attributes
146      */
147     boolean hasAttributes(T node);
148 
149     /**
150      * Returns the index of the given child node in the list of children of its parent. This method is the opposite
151      * operation of {@link #getChild(Object, int)}. This method returns 0 if the given node is the first child node with
152      * this name, 1 for the second child node and so on. If the node has no parent node or if it is an attribute, -1 is
153      * returned.
154      *
155      * @param parent the parent node
156      * @param child a child node whose index is to be retrieved
157      * @return the index of this child node
158      */
159     int indexOfChild(T parent, T child);
160 
161     /**
162      * Checks whether the specified node is defined. Nodes are &quot;defined&quot; if they contain any data, for example a value,
163      * or attributes, or defined children.
164      *
165      * @param node the node to test
166      * @return a flag whether the passed in node is defined
167      */
168     boolean isDefined(T node);
169 
170     /**
171      * Returns the name of the specified node
172      *
173      * @param node the node
174      * @return the name of this node
175      */
176     String nodeName(T node);
177 }