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.LinkedList; 020import java.util.List; 021 022/** 023 * <p> 024 * A class providing different algorithms for traversing a hierarchy of 025 * configuration nodes. 026 * </p> 027 * <p> 028 * The methods provided by this class accept a {@link ConfigurationNodeVisitor} 029 * and visit all nodes in a hierarchy starting from a given root node. Because a 030 * {@link NodeHandler} has to be passed in, too, arbitrary types of nodes can be 031 * processed. The {@code walk()} methods differ in the order in which nodes are 032 * visited. Details can be found in the method documentation. 033 * </p> 034 * <p> 035 * An instance of this class does not define any state; therefore, it can be 036 * shared and used concurrently. The {@code INSTANCE} member field can be used 037 * for accessing a default instance. If desired (e.g. for testing purposes), new 038 * instances can be created. 039 * </p> 040 * 041 * @since 2.0 042 */ 043public class NodeTreeWalker 044{ 045 /** The default instance of this class. */ 046 public static final NodeTreeWalker INSTANCE = new NodeTreeWalker(); 047 048 /** 049 * Visits all nodes in the hierarchy represented by the given root node in 050 * <em>depth first search</em> manner. This means that first 051 * {@link ConfigurationNodeVisitor#visitBeforeChildren(Object, NodeHandler)} 052 * is called on a node, then recursively all of its children are processed, 053 * and eventually 054 * {@link ConfigurationNodeVisitor#visitAfterChildren(Object, NodeHandler)} 055 * gets invoked. 056 * 057 * @param root the root node of the hierarchy to be processed (may be 058 * <b>null</b>, then this call has no effect) 059 * @param visitor the {@code ConfigurationNodeVisitor} (must not be 060 * <b>null</b>) 061 * @param handler the {@code NodeHandler} (must not be <b>null</b>) 062 * @param <T> the type of the nodes involved 063 * @throws IllegalArgumentException if a required parameter is <b>null</b> 064 */ 065 public <T> void walkDFS(final T root, final ConfigurationNodeVisitor<T> visitor, 066 final NodeHandler<T> handler) 067 { 068 if (checkParameters(root, visitor, handler)) 069 { 070 dfs(root, visitor, handler); 071 } 072 } 073 074 /** 075 * Visits all nodes in the hierarchy represented by the given root node in 076 * <em>breadth first search</em> manner. This means that the nodes are 077 * visited in an order corresponding to the distance from the root node: 078 * first the root node is visited, then all direct children of the root 079 * node, then all direct children of the first child of the root node, etc. 080 * In this mode of traversal, there is no direct connection between the 081 * encounter of a node and its children. <strong>Therefore, on the visitor 082 * object only the {@code visitBeforeChildren()} method gets 083 * called!</strong>. 084 * 085 * @param root the root node of the hierarchy to be processed (may be 086 * <b>null</b>, then this call has no effect) 087 * @param visitor the {@code ConfigurationNodeVisitor} (must not be 088 * <b>null</b>) 089 * @param handler the {@code NodeHandler} (must not be <b>null</b>) 090 * @param <T> the type of the nodes involved 091 * @throws IllegalArgumentException if a required parameter is <b>null</b> 092 */ 093 public <T> void walkBFS(final T root, final ConfigurationNodeVisitor<T> visitor, 094 final NodeHandler<T> handler) 095 { 096 if (checkParameters(root, visitor, handler)) 097 { 098 bfs(root, visitor, handler); 099 } 100 } 101 102 /** 103 * Recursive helper method for performing a DFS traversal. 104 * 105 * @param node the current node 106 * @param visitor the visitor 107 * @param handler the handler 108 * @param <T> the type of the nodes involved 109 */ 110 private static <T> void dfs(final T node, final ConfigurationNodeVisitor<T> visitor, 111 final NodeHandler<T> handler) 112 { 113 if (!visitor.terminate()) 114 { 115 visitor.visitBeforeChildren(node, handler); 116 for (final T c : handler.getChildren(node)) 117 { 118 dfs(c, visitor, handler); 119 } 120 if (!visitor.terminate()) 121 { 122 visitor.visitAfterChildren(node, handler); 123 } 124 } 125 } 126 127 /** 128 * Helper method for performing a BFS traversal. Implementation node: This 129 * method organizes the nodes to be visited in structures on the heap. 130 * Therefore, it can deal with larger structures than would be the case in a 131 * recursive approach (where the stack size limits the size of the 132 * structures which can be traversed). 133 * 134 * @param root the root node to be navigated 135 * @param visitor the visitor 136 * @param handler the handler 137 * @param <T> the type of the nodes involved 138 */ 139 private static <T> void bfs(final T root, final ConfigurationNodeVisitor<T> visitor, 140 final NodeHandler<T> handler) 141 { 142 final List<T> pendingNodes = new LinkedList<>(); 143 pendingNodes.add(root); 144 boolean cancel = false; 145 146 while (!pendingNodes.isEmpty() && !cancel) 147 { 148 final T node = pendingNodes.remove(0); 149 visitor.visitBeforeChildren(node, handler); 150 cancel = visitor.terminate(); 151 for (final T c : handler.getChildren(node)) 152 { 153 pendingNodes.add(c); 154 } 155 } 156 } 157 158 /** 159 * Helper method for checking the parameters for the walk() methods. If 160 * mandatory parameters are missing, an exception is thrown. The return 161 * value indicates whether an operation can be performed. 162 * 163 * @param root the root node 164 * @param visitor the visitor 165 * @param handler the handler 166 * @param <T> the type of the nodes involved 167 * @return <b>true</b> if a walk operation can be performed, <b>false</b> 168 * otherwise 169 * @throws IllegalArgumentException if a required parameter is missing 170 */ 171 private static <T> boolean checkParameters(final T root, 172 final ConfigurationNodeVisitor<T> visitor, final NodeHandler<T> handler) 173 { 174 if (visitor == null) 175 { 176 throw new IllegalArgumentException("Visitor must not be null!"); 177 } 178 if (handler == null) 179 { 180 throw new IllegalArgumentException("NodeHandler must not be null!"); 181 } 182 return root != null; 183 } 184}