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;
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 /**
46 * Returns a flag whether the search node was found in the last search operation.
47 *
48 * @return <strong>true</strong> if the search node was found; <strong>false</strong> otherwise
49 */
50 public boolean isFound() {
51 return found;
52 }
53
54 /**
55 * Resets this visitor. This method sets the {@code found} property to <strong>false</strong> again, so that this instance can be
56 * used to inspect another nodes hierarchy.
57 */
58 public void reset() {
59 found = false;
60 }
61
62 /**
63 * {@inheritDoc} This implementation returns <strong>true</strong> as soon as the node was found.
64 */
65 @Override
66 public boolean terminate() {
67 return found;
68 }
69
70 @Override
71 public void visitBeforeChildren(final T node, final NodeHandler<T> handler) {
72 if (node.equals(searchNode)) {
73 found = true;
74 }
75 }
76 }