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.tree;
18
19 /**
20 * <p>
21 * A special {@code NodeHandler} implementation for tracked nodes.
22 * </p>
23 * <p>
24 * While basic access to a tracked node works in the same way as for usual immutable nodes, there are differences for
25 * other operations. For instance, the root node of the hierarchy is always the tracked node itself. Also the parent
26 * mapping requires some special attention: as long as the node is not detached, the parent mapping of the model to
27 * which the node belongs can be used.
28 * </p>
29 * <p>
30 * This class inherits the major part of the {@code NodeHandler} implementation from its base class. In order to
31 * implement the parent mapping, an underlying {@code NodeHandler} object has to be passed at construction time which
32 * contains this information; requests for a node's parent are delegated to this handler. Further, the root node has to
33 * be provided explicitly.
34 * </p>
35 *
36 * @since 2.0
37 */
38 final class TrackedNodeHandler extends AbstractImmutableNodeHandler {
39 /** The root node. */
40 private final ImmutableNode rootNode;
41
42 /** The handler for querying the parent mapping. */
43 private final NodeHandler<ImmutableNode> parentHandler;
44
45 /**
46 * Creates a new instance of {@code TrackedNodeHandler} and initializes it with all required information.
47 *
48 * @param root the root node of the represented hierarchy
49 * @param handler an underlying handler for delegation
50 */
51 public TrackedNodeHandler(final ImmutableNode root, final NodeHandler<ImmutableNode> handler) {
52 rootNode = root;
53 parentHandler = handler;
54 }
55
56 /**
57 * {@inheritDoc} This implementation delegates to the handler with the parent mapping.
58 */
59 @Override
60 public ImmutableNode getParent(final ImmutableNode node) {
61 return getParentHandler().getParent(node);
62 }
63
64 /**
65 * Gets the parent handler. This is the {@code NodeHandler} which is consulted for determining a node's parent node.
66 *
67 * @return the parent {@code NodeHandler}
68 */
69 public NodeHandler<ImmutableNode> getParentHandler() {
70 return parentHandler;
71 }
72
73 /**
74 * {@inheritDoc} This implementation returns the root node passed at construction time.
75 */
76 @Override
77 public ImmutableNode getRootNode() {
78 return rootNode;
79 }
80 }