001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.configuration2.tree; 018 019import java.util.List; 020import java.util.Set; 021 022/** 023 * <p> 024 * Definition of an interface for accessing the data of a configuration node. 025 * </p> 026 * <p> 027 * Hierarchical configurations can deal with arbitrary node structures. In order to obtain information about a specific 028 * node object, a so-called {@code NodeHandler} is used. The handler provides a number of methods for querying the 029 * internal state of a node in a read-only way. 030 * </p> 031 * 032 * @param <T> the type of the nodes this handler deals with 033 */ 034public interface NodeHandler<T> { 035 036 /** 037 * Gets an unmodifiable set with the names of all attributes of the specified node. 038 * 039 * @param node the node 040 * @return a set with the names of all attributes of this node 041 */ 042 Set<String> getAttributes(T node); 043 044 /** 045 * Gets the value of the specified attribute from the given node. If a concrete {@code NodeHandler} supports 046 * attributes with multiple values, result might be a collection. 047 * 048 * @param node the node 049 * @param name the name of the attribute 050 * @return the value of this attribute 051 */ 052 Object getAttributeValue(T node, String name); 053 054 /** 055 * Gets the child with the given index of the specified node. 056 * 057 * @param node the node 058 * @param index the index (0-based) 059 * @return the child with the given index 060 */ 061 T getChild(T node, int index); 062 063 /** 064 * Gets an unmodifiable list with all children of the specified node. 065 * 066 * @param node the node 067 * @return a list with the child nodes of this node 068 */ 069 List<T> getChildren(T node); 070 071 /** 072 * Gets an unmodifiable list of all children of the specified node with the given name. 073 * 074 * @param node the node 075 * @param name the name of the desired child nodes 076 * @return a list with all children with the given name 077 */ 078 List<T> getChildren(T node, String name); 079 080 /** 081 * Gets the number of children of the specified node with the given name. This method exists for performance reasons: 082 * for some node implementations it may be by far more efficient to count the children than to query a list of all 083 * children and determine its size. A concrete implementation can choose the most efficient way to determine the number 084 * of children. If a child name is passed in, only the children with this name are taken into account. If the name 085 * <strong>null</strong> is passed, the total number of children must be returned. 086 * 087 * @param node the node 088 * @param name the name of the children in question (can be <strong>null</strong> for all children) 089 * @return the number of the selected children 090 */ 091 int getChildrenCount(T node, String name); 092 093 /** 094 * Gets an unmodifiable list of all children of the specified node which are matched by the passed in 095 * {@code NodeMatcher} against the provided criterion. This method allows for advanced queries on a node's children. 096 * 097 * @param node the node 098 * @param matcher the {@code NodeMatcher} defining filter criteria 099 * @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 "defined" 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}