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 * http://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 * Gets an unmodifiable set with the names of all attributes of the specified node. 037 * 038 * @param node the node 039 * @return a set with the names of all attributes of this node 040 */ 041 Set<String> getAttributes(T node); 042 043 /** 044 * Gets the value of the specified attribute from the given node. If a concrete {@code NodeHandler} supports 045 * attributes with multiple values, result might be a collection. 046 * 047 * @param node the node 048 * @param name the name of the attribute 049 * @return the value of this attribute 050 */ 051 Object getAttributeValue(T node, String name); 052 053 /** 054 * Gets the child with the given index of the specified node. 055 * 056 * @param node the node 057 * @param index the index (0-based) 058 * @return the child with the given index 059 */ 060 T getChild(T node, int index); 061 062 /** 063 * Gets an unmodifiable list with all children of the specified node. 064 * 065 * @param node the node 066 * @return a list with the child nodes of this node 067 */ 068 List<T> getChildren(T node); 069 070 /** 071 * Gets an unmodifiable list of all children of the specified node with the given name. 072 * 073 * @param node the node 074 * @param name the name of the desired child nodes 075 * @return a list with all children with the given name 076 */ 077 List<T> getChildren(T node, String name); 078 079 /** 080 * Gets the number of children of the specified node with the given name. This method exists for performance reasons: 081 * for some node implementations it may be by far more efficient to count the children than to query a list of all 082 * children and determine its size. A concrete implementation can choose the most efficient way to determine the number 083 * of children. If a child name is passed in, only the children with this name are taken into account. If the name 084 * <b>null</b> is passed, the total number of children must be returned. 085 * 086 * @param node the node 087 * @param name the name of the children in question (can be <b>null</b> for all children) 088 * @return the number of the selected children 089 */ 090 int getChildrenCount(T node, String name); 091 092 /** 093 * Gets an unmodifiable list of all children of the specified node which are matched by the passed in 094 * {@code NodeMatcher} against the provided criterion. This method allows for advanced queries on a node's children. 095 * 096 * @param node the node 097 * @param matcher the {@code NodeMatcher} defining filter criteria 098 * @param criterion the criterion to be matched against; this object is passed to the {@code NodeMatcher} 099 * @param <C> the type of the criterion 100 * @return a list with all children matched by the matcher 101 */ 102 <C> List<T> getMatchingChildren(T node, NodeMatcher<C> matcher, C criterion); 103 104 /** 105 * Gets the number of children of the specified node which are matched by the given {@code NodeMatcher}. This is a 106 * more generic version of {@link #getChildrenCount(Object, String)}. It allows checking for arbitrary filter 107 * conditions. 108 * 109 * @param node the node 110 * @param matcher the {@code NodeMatcher} 111 * @param criterion the criterion to be passed to the {@code NodeMatcher} 112 * @param <C> the type of the criterion 113 * @return the number of matched children 114 */ 115 <C> int getMatchingChildrenCount(T node, NodeMatcher<C> matcher, C criterion); 116 117 /** 118 * Gets the parent of the specified node. 119 * 120 * @param node the node 121 * @return the parent node 122 */ 123 T getParent(T node); 124 125 /** 126 * Gets the root node of the underlying hierarchy. 127 * 128 * @return the current root node 129 */ 130 T getRootNode(); 131 132 /** 133 * Gets the value of the specified node. 134 * 135 * @param node the node 136 * @return the value of this node 137 */ 138 Object getValue(T node); 139 140 /** 141 * Returns a flag whether the passed in node has any attributes. 142 * 143 * @param node the node 144 * @return a flag whether this node has any attributes 145 */ 146 boolean hasAttributes(T node); 147 148 /** 149 * Returns the index of the given child node in the list of children of its parent. This method is the opposite 150 * operation of {@link #getChild(Object, int)}. This method returns 0 if the given node is the first child node with 151 * 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 152 * returned. 153 * 154 * @param parent the parent node 155 * @param child a child node whose index is to be retrieved 156 * @return the index of this child node 157 */ 158 int indexOfChild(T parent, T child); 159 160 /** 161 * Checks whether the specified node is defined. Nodes are "defined" if they contain any data, e.g. a value, 162 * or attributes, or defined children. 163 * 164 * @param node the node to test 165 * @return a flag whether the passed in node is defined 166 */ 167 boolean isDefined(T node); 168 169 /** 170 * Returns the name of the specified node 171 * 172 * @param node the node 173 * @return the name of this node 174 */ 175 String nodeName(T node); 176}