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