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;
18  
19  import org.apache.commons.configuration2.tree.ConfigurationNodeVisitorAdapter;
20  import org.apache.commons.configuration2.tree.NodeHandler;
21  
22  /**
23   * <p>
24   * A specialized {@code NodeVisitor} implementation which searches for a specific node in a hierarchy.
25   * </p>
26   *
27   * @param <T> the type of the nodes to be visited
28   */
29  final class FindNodeVisitor<T> extends ConfigurationNodeVisitorAdapter<T> {
30      /** The node to be searched for. */
31      private final T searchNode;
32  
33      /** A flag whether the node was found. */
34      private boolean found;
35  
36      /**
37       * Creates a new instance of {@code FindNodeVisitor} and sets the node to be searched for.
38       *
39       * @param node the search node
40       */
41      public FindNodeVisitor(final T node) {
42          searchNode = node;
43      }
44  
45      @Override
46      public void visitBeforeChildren(final T node, final NodeHandler<T> handler) {
47          if (node.equals(searchNode)) {
48              found = true;
49          }
50      }
51  
52      /**
53       * {@inheritDoc} This implementation returns <b>true</b> as soon as the node was found.
54       */
55      @Override
56      public boolean terminate() {
57          return found;
58      }
59  
60      /**
61       * Returns a flag whether the search node was found in the last search operation.
62       *
63       * @return <b>true</b> if the search node was found; <b>false</b> otherwise
64       */
65      public boolean isFound() {
66          return found;
67      }
68  
69      /**
70       * Resets this visitor. This method sets the {@code found} property to <b>false</b> again, so that this instance can be
71       * used to inspect another nodes hierarchy.
72       */
73      public void reset() {
74          found = false;
75      }
76  }